Skip to content

Instantly share code, notes, and snippets.

View rzwitserloot's full-sized avatar

Reinier Zwitserloot rzwitserloot

View GitHub Profile
public static void main(String[] args) throws Exception {
Server server = new Server(8837);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
Crypto crypto = new Crypto();
Configuration templates = createTemplateConfiguration();
SessionStore sessions = createSessionStore(crypto);
UserStore users = createUserStore(crypto);
Totp totp = new Totp(users, sessions, crypto);
public void method(String arg1, String arg2) {
defaults: {
arg2 = "Hello";
}
// code here
}
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.SecureRandom;
import java.util.Random;
import lombok.NonNull;
import lombok.SneakyThrows;

Purpose

Allow default values (other than null / 0 / false) for builder. That is, if some .fieldName(value) method is never called on the builder, use the provided default instead of null etc.

Snippets to highlight issues and options.

Snippet: final fields with initializers.

@Value @Builder public class Test {

long created = System.currentTimeMillis();

@FunctionalInterface
public interface Action<T extends Base> {
void perform(T input);
}
public class Commands {
private Map<Class<? extends Base>, Action<?>> actions = new HashMap<>();
private <T extends Base> void addAction(Class<T> type, Action<T> action) {
actions.put(type, action);
// save this file as src/main/java/perftest/MyBenchmark.java relative to pom.xml
package perftest;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit
class DivLoader {
private final Object locker = new Object[0];
private final AtomicBoolean running = new AtomicBoolean();
private static final long WAIT_TIME = TimeUnit.SECONDS.toMillis(60);
private Thread thread;
private final AtomicReference<String> content = new AtomicReference<>();
private Runnable refresher() {
private static final Map<String, Long> allowed = new ConcurrentHashMap<String, Long>();
private static final AtomicLong lastCleanup = new AtomicLong();
private static final long MAX_DURATION = TimeUnit.MINUTES.toMillis(45);
private static final long CLEANUP_INTERVAL = TimeUnit.MINUTES.toMillis(30);
public static void process(String k) {
long n = System.currentTimeMillis();
allowed.put(k, n);
long m = lastCleanup.get();
if (n - m > CLEANUP_INTERVAL) {
package com.zwitserloot;
import org.openjdk.jmh.annotations.*;
import java.nio.charset.*;
import java.util.Arrays;
public class MyBenchmark {
private static final int HAYSTACK_SIZE = 100;
public static void main(String[] args) {
class Test {
void foo() {
class Bar extends Baz {
Bar retSelf() {
return this;
}
}
}
}