Skip to content

Instantly share code, notes, and snippets.

View joshlong's full-sized avatar
🍃
i'm on Twitter @starbuxman

Josh Long joshlong

🍃
i'm on Twitter @starbuxman
View GitHub Profile
@joshlong
joshlong / main.kts
Created November 12, 2024 20:54
an example demonstrating how nice Kotlin can be
data class Point(val x: Int, val y: Int)
data class Rectangle(val width: Int, val height: Int)
data class Circle(val radius: Int)
fun describePoint(point: Point) = when (point) {
Point(0, 0) -> "Origin"
Point(1, 2) -> "1,2"
else -> throw IllegalStateException("Unexpected value: $point")
}
@joshlong
joshlong / Main.java
Last active November 15, 2024 19:44
a simple Java 23 program demonstrating some of the things showing how nice modern Java can be, based on the C# equivalent shown in https://youtu.be/1I3mWSiWNsI?feature=shared&t=374
// sdk install java 23.0.1-librca && sdk use java 23.0.1-librca
// java --enable-preview Main.java
record Point(int x, int y) {}
sealed interface Shape permits Rectangle, Circle {}
record Rectangle(int width, int height) implements Shape {}
record Circle(int radius) implements Shape {}
void main() {
var point = new Point(1, 2);
println(describePoint(point));
@joshlong
joshlong / DynamicDurationClientHttpRequestFactory.java
Created September 19, 2024 11:34
dynamically set the timeout for each request
package org.springframework.http.client;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.http.HttpMethod;
import org.springframework.util.Assert;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.time.Duration;
@joshlong
joshlong / ConvertAuthorities.java
Created September 4, 2024 09:50
demonstrates how to change the authorities of the incoming token to reflect reconciled user state
package com.example.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.simple.JdbcClient;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper;
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestClient;
import java.util.Map;
@joshlong
joshlong / UAO.java
Last active September 25, 2024 02:27
this does the same thing as my existing unzip-and-open.py, but in less code and faster, with a compiler to help me.
// I'd suggest you create an alias somewhere:
// alias uao=java --enable-preview --source 21 $HOME/bin/UAO.java
// RUN: uao $HOME/Downloads/demo.zip
import java.io.*;
import java.util.function.*;
import java.util.* ;
void main(String[] args) throws Exception {
var zipFile = new File(args[0]);
@joshlong
joshlong / ReversingServiceApplication.java
Last active September 7, 2023 09:46
this is a simple network service that reverses strings. I'm using it to test out Project Loom
package com.example.demo;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
@joshlong
joshlong / VcConfiguration.java
Created July 22, 2023 14:02
AOT for Spring ViewComponents with Thomas
@Configuration
class VcConfiguration {
@Bean
static ViewComponentResourceAotProcessor viewComponentResourceAotProcessor() {
return new ViewComponentResourceAotProcessor();
}
}
package hollywood.framework;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.FileCopyUtils;
#!/usr/bin/env bash
PID=${1}
RSS=`ps -o rss ${PID} | tail -n1`
RSS=`bc <<< "scale=1; ${RSS}/1024"`
echo "${PID}: ${RSS}M"