Skip to content

Instantly share code, notes, and snippets.

View nschlimm's full-sized avatar
🏠
Working from home

Niklas Schlimm nschlimm

🏠
Working from home
  • TIMOCOM GmbH
  • Cologne, Germany
View GitHub Profile
@nschlimm
nschlimm / TypeInference.java
Last active February 5, 2019 08:23
Type Inference on Method chaining
public class SomePojo<T> {
private String someProperty;
private String anotherProperty;
private SomePojo(Builder<T> builder) {
this.someProperty = builder.someProperty;
this.anotherProperty = builder.anotherProperty;
}
// shopping basket with state pattern
public static class ShoppingBasket {
private String orderNo;
private List<String> articleNumbers = new ArrayList<>();
private UpdateState state = UpdateState.UPDATEABLE;
public void add(String articleNumber) {
articleNumbers.add(state.set(articleNumber));
}
public String getOrderNo() {
return orderNo;
public enum UpdateState {
UPDATEABLE(()->Validate.validState(true)), READONLY(()->Validate.validState(false));
private Runnable action;
private UpdateState(Runnable action) {
this.action=action;
}
public <T> T set(T value) {
action.run();
return value;
}
public static class ShoppingBasket1 {
private String orderNo;
private List<String> articleNumbers = new ArrayList<>();
public void add(String articleNumber) {
articleNumbers.add(articleNumber);
}
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
Predicates predicates = new Predicates("SSH");
System.getenv().keySet().stream().filter(predicates::containsPattern).collect(Collectors.toSet());
@nschlimm
nschlimm / Predicates.java
Last active January 30, 2019 08:25
Predicates with standard parameter
public static class Predicates {
private String pattern;
public boolean containsPattern(String string) {
return string.contains(pattern);
}
public Predicates(String pattern) {
this.pattern = pattern;
}
}
@nschlimm
nschlimm / gist:2506857
Created April 27, 2012 07:22
Read and write fully
public class ReadWriteAll {
public static void main(String[] args) throws InterruptedException, IOException {
try (AsynchronousFileChannel outputfile = AsynchronousFileChannel.open(Paths.get("E:/temp/afile.out"),
StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE,
StandardOpenOption.DELETE_ON_CLOSE)) {
writeFully(outputfile, (ByteBuffer) ByteBuffer.allocateDirect(1048576).put(new byte[1048576]).flip(), 0L);
readAll(outputfile, (ByteBuffer) ByteBuffer.allocateDirect(1000).put(new byte[1000]).flip(), 1000L);
} catch (Exception e) {
e.printStackTrace();
@nschlimm
nschlimm / gist:2308688
Created April 5, 2012 07:09
LoggingClient
public class MyLoggingClient {
private static AtomicInteger fileindex = new AtomicInteger(0);
private static final String FILE_URI = "file:/E:/temp/afile.out";
public static void main(String[] args) throws IOException {
new Thread(new Runnable() { // arbitrary thread that writes stuff into an asynchronous I/O data sink
@Override
public void run() {
try {
@nschlimm
nschlimm / gist:2301874
Created April 4, 2012 14:36
close method
/**
* Method that closes this file channel gracefully without loosing any data.
*/
@Override
public void close() throws IOException {
AsynchronousFileChannel writeableChannel = innerChannel;
System.out.println("Starting graceful shutdown ...");
closeLock.lock();
try {
state = PREPARE;
@nschlimm
nschlimm / gist:2301734
Created April 4, 2012 14:27
DefensiveThreadPoolExecutor
private class DefensiveThreadPoolExecutor extends ThreadPoolExecutor {
public DefensiveThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
LinkedBlockingQueue<Runnable> workQueue, ThreadFactory factory, RejectedExecutionHandler handler) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, factory, handler);
}
/**
* "Last" task issues a signal that queue is empty after task processing was completed.
*/