Skip to content

Instantly share code, notes, and snippets.

@kaspernielsen
Created May 7, 2022 07:01
Show Gist options
  • Save kaspernielsen/0d22ab52d9e07d4122690523caeb2f77 to your computer and use it in GitHub Desktop.
Save kaspernielsen/0d22ab52d9e07d4122690523caeb2f77 to your computer and use it in GitHub Desktop.
public class StaticJava {
// Can only be created at build-time for static java
sealed interface RuntimeLocal<T> permits InternalRuntimeLocal {
// VM could knwo about this and optimize invocations
T get();
static <T> RuntimeLocal<T> of(Supplier<T> supplier) {}
static <T> RuntimeLocal<T> ofLazy(Supplier<T> supplier) {}
}
sealed interface ReinitLocal<T> permits InternalRuntimeLocal {
T get();
static <T> ReinitLocal<T> of(Supplier<T> supplier) {}
static <T> ReinitLocal<T> of(Supplier<T> supplier, Consumer<T> cleaner) {}
}
static class MixAndMatch {
private static final MethodHandle MH = null; // Initialized as normal
private static final RuntimeLocal<LocalDateTime> RUNTIME_TIME = RuntimeLocal.of(LocalDateTime::now);
private static final ReinitLocal<LocalDateTime> PROCESS_TIME = ReinitLocal.of(LocalDateTime::now);
public static void printApplicationStart() {
System.out.println("Application started running at " + RUNTIME_TIME.get());
}
public static void printApplicationProcessStart() {
System.out.println("Application started running in this process at " + PROCESS_TIME.get());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment