Skip to content

Instantly share code, notes, and snippets.

View billybong's full-sized avatar

Billy Sjöberg billybong

View GitHub Profile
package net.mojang.benchmark;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
@billybong
billybong / ReproTest.java
Created November 6, 2019 12:00
json-smart serialization fails without setter - treats bean field without setter as public
import net.minidev.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class ReproTest {
public static void main(String[] args) {
MyData data = new MyData("a");
Map<String, Object> m = new HashMap<>();
m.put("data", data);
@billybong
billybong / JacksonBodyHandlers.java
Last active November 11, 2020 21:12
JacksonBodyHandler for parsing JSON responses directly from Java 11+ http clients
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.http.HttpResponse;
import java.util.function.Function;
public class JacksonBodyHandlers {
private final ObjectMapper objectMapper;
var providesStubRemoteApi = new Factories(){
Optional<RemoteApi> remoteApi(){
RemoteApi remoteApi = MyStubRemoteApi();
return Optional.of(remoteApi);
}
}
var app = bootstrapper.bootstrap(config, providesStubRemoteApi);
class Bootstrapper {
public Application bootStrap(Config cfg, Factories factories){
var someDependency = constructTheDependency();
var myService = factories.myService().orElseGet(() -> constructTheRealThing(someDependency));
}
}
@billybong
billybong / DependencyInjection.java
Last active August 23, 2018 19:44
DI without framework, Java
interface Factories {
default Optional<MyService> myService(){return Optional.empty();}
default Optional<RemoteApi> remoteApi(){return Optional.empty();}
static Factories factories(){return new Factories(){};}
}
import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.channels.Channel
import kotlinx.coroutines.experimental.channels.actor
class RequestCollapser<in ARGUMENT, BATCH_RESP, out RETURN_TYPE>(
private val threshold: Int,
private val fn: suspend (Collection<ARGUMENT>) -> BATCH_RESP,
private val remapper: (ARGUMENT, BATCH_RESP) -> RETURN_TYPE
) {
#!/usr/bin/env jjs
/*####################################################################################################################################
# As Nashorn does not have http capabilities through XMLHttpRequest (DOM API), we have to use regular Java classes instead.
# This sample shows how this can be acheived without depending on any third party libraries. Just a standard Java 8 JDK.
# Make sure to have JAVA_HOME/bin on your PATH for the shebang to work. Then just chmod +x away and run...
# Alternatively if you're on a non *nix OS, start with jjs -scritping httpsample.js
####################################################################################################################################*/
var url = "https://api.github.com/users/billybong/repos";
var response;