Java 11 migration: patterns (t-v) (#1085)

* Moves visitor pattern to java 11

* Moves value-object pattern to java 11

* Moves unit-of-work pattern to java 11

* Moves typeobjectpattern pattern to java 11

* Moves twin pattern to java 11

* Moves trampoline pattern to java 11

* Moves tolerant-reader pattern to java 11

* Moves tls pattern to java 11

* Moves throttling pattern to java 11

* Moves thread-pool pattern to java 11

* Moves template-method pattern to java 11
This commit is contained in:
Anurag Agarwal
2019-11-14 11:12:05 +05:30
committed by Ilkka Seppälä
parent 160b737dcc
commit 50467c9e76
45 changed files with 379 additions and 422 deletions
+21 -23
View File
@@ -24,10 +24,7 @@
package com.iluwatar.tls;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -71,20 +68,20 @@ public class App {
* @param args command line args
*/
public static void main(String[] args) {
int counterDateValues = 0;
int counterExceptions = 0;
var counterDateValues = 0;
var counterExceptions = 0;
// Create a callable
DateFormatCallable callableDf = new DateFormatCallable("dd/MM/yyyy", "15/12/2015");
var callableDf = new DateFormatCallable("dd/MM/yyyy", "15/12/2015");
// start 4 threads, each using the same Callable instance
ExecutorService executor = Executors.newCachedThreadPool();
var executor = Executors.newCachedThreadPool();
Future<Result> futureResult1 = executor.submit(callableDf);
Future<Result> futureResult2 = executor.submit(callableDf);
Future<Result> futureResult3 = executor.submit(callableDf);
Future<Result> futureResult4 = executor.submit(callableDf);
var futureResult1 = executor.submit(callableDf);
var futureResult2 = executor.submit(callableDf);
var futureResult3 = executor.submit(callableDf);
var futureResult4 = executor.submit(callableDf);
try {
Result[] result = new Result[4];
var result = new Result[4];
result[0] = futureResult1.get();
result[1] = futureResult2.get();
result[2] = futureResult3.get();
@@ -92,9 +89,9 @@ public class App {
// Print results of thread executions (converted dates and raised exceptions)
// and count them
for (int i = 0; i < result.length; i++) {
counterDateValues = counterDateValues + printAndCountDates(result[i]);
counterExceptions = counterExceptions + printAndCountExceptions(result[i]);
for (var value : result) {
counterDateValues = counterDateValues + printAndCountDates(value);
counterExceptions = counterExceptions + printAndCountExceptions(value);
}
// a correct run should deliver 20 times 15.12.2015
@@ -115,15 +112,16 @@ public class App {
*/
private static int printAndCountDates(Result res) {
// a correct run should deliver 5 times 15.12.2015 per each thread
int counter = 0;
for (Date dt : res.getDateList()) {
var counter = 0;
for (var dt : res.getDateList()) {
counter++;
Calendar cal = Calendar.getInstance();
var cal = Calendar.getInstance();
cal.setTime(dt);
// Formatted output of the date value: DD.MM.YYYY
LOGGER.info(
cal.get(Calendar.DAY_OF_MONTH) + "." + cal.get(Calendar.MONTH) + "." + +cal
.get(Calendar.YEAR));
LOGGER.info(cal.get(Calendar.DAY_OF_MONTH) + "."
+ cal.get(Calendar.MONTH) + "."
+ cal.get(Calendar.YEAR)
);
}
return counter;
}
@@ -136,8 +134,8 @@ public class App {
*/
private static int printAndCountExceptions(Result res) {
// a correct run shouldn't deliver any exception
int counter = 0;
for (String ex : res.getExceptionList()) {
var counter = 0;
for (var ex : res.getExceptionList()) {
counter++;
LOGGER.info(ex);
}
@@ -26,6 +26,7 @@ package com.iluwatar.tls;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.concurrent.Callable;
import java.util.stream.IntStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -58,13 +59,10 @@ public class DateFormatCallable implements Callable<Result> {
* @param inDateValue string date value, e.g. "21/06/2016"
*/
public DateFormatCallable(String inDateFormat, String inDateValue) {
final String idf = inDateFormat; //TLTL
this.df = new ThreadLocal<DateFormat>() { //TLTL
@Override //TLTL
protected DateFormat initialValue() { //TLTL
return new SimpleDateFormat(idf); //TLTL
} //TLTL
}; //TLTL
final var idf = inDateFormat; //TLTL
this.df = ThreadLocal.withInitial(() -> { //TLTL
return new SimpleDateFormat(idf); //TLTL
}); //TLTL
// this.df = new SimpleDateFormat(inDateFormat); //NTLNTL
this.dateValue = inDateValue;
}
@@ -72,10 +70,10 @@ public class DateFormatCallable implements Callable<Result> {
@Override
public Result call() {
LOGGER.info(Thread.currentThread() + " started executing...");
Result result = new Result();
var result = new Result();
// Convert date value to date 5 times
for (int i = 1; i <= 5; i++) {
IntStream.rangeClosed(1, 5).forEach(i -> {
try {
// this is the statement where it is important to have the
// instance of SimpleDateFormat locally
@@ -86,8 +84,7 @@ public class DateFormatCallable implements Callable<Result> {
// write the Exception to a list and continue work
result.getExceptionList().add(e.getClass() + ": " + e.getMessage());
}
}
});
LOGGER.info(Thread.currentThread() + " finished processing part of the thread");
@@ -39,11 +39,11 @@ import java.util.List;
*/
public class Result {
// A list to collect the date values created in one thread
private List<Date> dateList = new ArrayList<Date>();
private List<Date> dateList = new ArrayList<>();
// A list to collect Exceptions thrown in one threads (should be none in
// this example)
private List<String> exceptionList = new ArrayList<String>();
private List<String> exceptionList = new ArrayList<>();
/**
* Get list of date values collected within a thread execution.