This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SomePojo<T> { | |
private String someProperty; | |
private String anotherProperty; | |
private SomePojo(Builder<T> builder) { | |
this.someProperty = builder.someProperty; | |
this.anotherProperty = builder.anotherProperty; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Predicates predicates = new Predicates("SSH"); | |
System.getenv().keySet().stream().filter(predicates::containsPattern).collect(Collectors.toSet()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Predicates { | |
private String pattern; | |
public boolean containsPattern(String string) { | |
return string.contains(pattern); | |
} | |
public Predicates(String pattern) { | |
this.pattern = pattern; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
*/ |
NewerOlder