diff --git a/template-method/src/main/java/com/iluwatar/templatemethod/App.java b/template-method/src/main/java/com/iluwatar/templatemethod/App.java index 8776b5ebc89f..f98e9f760a05 100644 --- a/template-method/src/main/java/com/iluwatar/templatemethod/App.java +++ b/template-method/src/main/java/com/iluwatar/templatemethod/App.java @@ -24,19 +24,17 @@ package com.iluwatar.templatemethod; /** - * * Template Method defines a skeleton for an algorithm. The algorithm subclasses provide * implementation for the blank parts. - *
- * In this example {@link HalflingThief} contains {@link StealingMethod} that can be changed. First - * the thief hits with {@link HitAndRunMethod} and then with {@link SubtleMethod}. - * + * + *
In this example {@link HalflingThief} contains {@link StealingMethod} that can be changed. + * First the thief hits with {@link HitAndRunMethod} and then with {@link SubtleMethod}. */ public class App { /** - * Program entry point - * + * Program entry point. + * * @param args command line args */ public static void main(String[] args) { diff --git a/template-method/src/main/java/com/iluwatar/templatemethod/HalflingThief.java b/template-method/src/main/java/com/iluwatar/templatemethod/HalflingThief.java index 545c9fbd31e5..631a58600699 100644 --- a/template-method/src/main/java/com/iluwatar/templatemethod/HalflingThief.java +++ b/template-method/src/main/java/com/iluwatar/templatemethod/HalflingThief.java @@ -24,9 +24,7 @@ package com.iluwatar.templatemethod; /** - * * Halfling thief uses {@link StealingMethod} to steal. - * */ public class HalflingThief { diff --git a/template-method/src/main/java/com/iluwatar/templatemethod/HitAndRunMethod.java b/template-method/src/main/java/com/iluwatar/templatemethod/HitAndRunMethod.java index bdb6771cd795..4ede6f02372e 100644 --- a/template-method/src/main/java/com/iluwatar/templatemethod/HitAndRunMethod.java +++ b/template-method/src/main/java/com/iluwatar/templatemethod/HitAndRunMethod.java @@ -27,9 +27,7 @@ import org.slf4j.LoggerFactory; /** - * * HitAndRunMethod implementation of {@link StealingMethod}. - * */ public class HitAndRunMethod extends StealingMethod { diff --git a/template-method/src/main/java/com/iluwatar/templatemethod/StealingMethod.java b/template-method/src/main/java/com/iluwatar/templatemethod/StealingMethod.java index 6ac898bcaad4..bf206f3a582b 100644 --- a/template-method/src/main/java/com/iluwatar/templatemethod/StealingMethod.java +++ b/template-method/src/main/java/com/iluwatar/templatemethod/StealingMethod.java @@ -27,9 +27,7 @@ import org.slf4j.LoggerFactory; /** - * * StealingMethod defines skeleton for the algorithm. - * */ public abstract class StealingMethod { @@ -42,7 +40,7 @@ public abstract class StealingMethod { protected abstract void stealTheItem(String target); /** - * Steal + * Steal. */ public void steal() { var target = pickTarget(); diff --git a/template-method/src/main/java/com/iluwatar/templatemethod/SubtleMethod.java b/template-method/src/main/java/com/iluwatar/templatemethod/SubtleMethod.java index 500769813f24..eaf897667c79 100644 --- a/template-method/src/main/java/com/iluwatar/templatemethod/SubtleMethod.java +++ b/template-method/src/main/java/com/iluwatar/templatemethod/SubtleMethod.java @@ -27,9 +27,7 @@ import org.slf4j.LoggerFactory; /** - * * SubtleMethod implementation of {@link StealingMethod}. - * */ public class SubtleMethod extends StealingMethod { diff --git a/thread-pool/src/main/java/com/iluwatar/threadpool/App.java b/thread-pool/src/main/java/com/iluwatar/threadpool/App.java index b21f8cc4a1d3..0d5ee23f5806 100644 --- a/thread-pool/src/main/java/com/iluwatar/threadpool/App.java +++ b/thread-pool/src/main/java/com/iluwatar/threadpool/App.java @@ -23,36 +23,32 @@ package com.iluwatar.threadpool; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** - * * Thread Pool pattern is where a number of threads are created to perform a number of tasks, which * are usually organized in a queue. The results from the tasks being executed might also be placed * in a queue, or the tasks might return no result. Typically, there are many more tasks than * threads. As soon as a thread completes its task, it will request the next task from the queue * until all tasks have been completed. The thread can then terminate, or sleep until there are new * tasks available. - *
- * In this example we create a list of tasks presenting work to be done. Each task is then wrapped - * into a {@link Worker} object that implements {@link Runnable}. We create an - * {@link ExecutorService} with fixed number of threads (Thread Pool) and use them to execute the - * {@link Worker}s. * + *
In this example we create a list of tasks presenting work to be done. Each task is then
+ * wrapped into a {@link Worker} object that implements {@link Runnable}. We create an {@link
+ * ExecutorService} with fixed number of threads (Thread Pool) and use them to execute the {@link
+ * Worker}s.
*/
public class App {
-
+
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
/**
- * Program entry point
- *
+ * Program entry point.
+ *
* @param args command line args
*/
public static void main(String[] args) {
@@ -61,21 +57,21 @@ public static void main(String[] args) {
// Create a list of tasks to be executed
List
- * In this example we have ({@link App}) as the initiating point of the service.
- * This is a time based throttling, i.e. only a certain number of calls are allowed per second.
+ * In this example we have ({@link App}) as the initiating point of the service. This is a time
+ * based throttling, i.e. only a certain number of calls are allowed per second.
*
- * This App shows how to create an isolated space per each thread. In this
- * example the usage of SimpleDateFormat is made to be thread-safe. This is an
- * example of the ThreadLocal pattern.
- *
- * By applying the ThreadLocal pattern you can keep track of application
- * instances or locale settings throughout the handling of a request. The
- * ThreadLocal class works like a static variable, with the exception that it is
- * only bound to the current thread! This allows us to use static variables in a
- * thread-safe way.
- *
- * In Java, thread-local variables are implemented by the ThreadLocal class
- * object. ThreadLocal holds a variable of type T, which is accessible via get/set
- * methods.
- *
- * SimpleDateFormat is one of the basic Java classes and is not thread-safe. If
- * you do not isolate the instance of SimpleDateFormat per each thread then
- * problems arise.
- *
- * App converts the String date value 15/12/2015 to the Date format using the
- * Java class SimpleDateFormat. It does this 20 times using 4 threads, each doing
- * it 5 times. With the usage of as ThreadLocal in DateFormatCallable everything
- * runs well. But if you comment out the ThreadLocal variant (marked with "//TLTL")
- * and comment in the non ThreadLocal variant (marked with "//NTLNTL") you can
- * see what will happen without the ThreadLocal. Most likely you will get incorrect
- * date values and / or exceptions.
- *
- * This example clearly show what will happen when using non thread-safe classes
- * in a thread. In real life this may happen one in of 1.000 or 10.000 conversions
- * and those are really hard to find errors.
- *
- * @author Thomas Bauer, 2017
+ *
+ * This App shows how to create an isolated space per each thread. In this example the usage of
+ * SimpleDateFormat is made to be thread-safe. This is an example of the ThreadLocal pattern.
+ *
+ * By applying the ThreadLocal pattern you can keep track of application instances or locale
+ * settings throughout the handling of a request. The ThreadLocal class works like a static
+ * variable, with the exception that it is only bound to the current thread! This allows us to use
+ * static variables in a thread-safe way.
+ *
+ * In Java, thread-local variables are implemented by the ThreadLocal class object. ThreadLocal
+ * holds a variable of type T, which is accessible via get/set methods.
+ *
+ * SimpleDateFormat is one of the basic Java classes and is not thread-safe. If you do not
+ * isolate the instance of SimpleDateFormat per each thread then problems arise.
+ *
+ * App converts the String date value 15/12/2015 to the Date format using the Java class
+ * SimpleDateFormat. It does this 20 times using 4 threads, each doing it 5 times. With the usage of
+ * as ThreadLocal in DateFormatCallable everything runs well. But if you comment out the ThreadLocal
+ * variant (marked with "//TLTL") and comment in the non ThreadLocal variant (marked with
+ * "//NTLNTL") you can see what will happen without the ThreadLocal. Most likely you will get
+ * incorrect date values and / or exceptions.
+ *
+ * This example clearly show what will happen when using non thread-safe classes in a thread. In
+ * real life this may happen one in of 1.000 or 10.000 conversions and those are really hard to find
+ * errors.
+ *
+ * @author Thomas Bauer, 2017
*/
public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
+
/**
- * Program entry point
- *
- * @param args
- * command line args
+ * Program entry point.
+ *
+ * @param args command line args
*/
public static void main(String[] args) {
int counterDateValues = 0;
@@ -115,9 +109,9 @@ public static void main(String[] args) {
}
/**
- * Print result (date values) of a thread execution and count dates
- *
- * @param res contains results of a thread execution
+ * Print result (date values) of a thread execution and count dates.
+ *
+ * @param res contains results of a thread execution
*/
private static int printAndCountDates(Result res) {
// a correct run should deliver 5 times 15.12.2015 per each thread
@@ -128,15 +122,16 @@ private static int printAndCountDates(Result res) {
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));
+ cal.get(Calendar.DAY_OF_MONTH) + "." + cal.get(Calendar.MONTH) + "." + +cal
+ .get(Calendar.YEAR));
}
return counter;
}
/**
- * Print result (exceptions) of a thread execution and count exceptions
- *
- * @param res contains results of a thread execution
+ * Print result (exceptions) of a thread execution and count exceptions.
+ *
+ * @param res contains results of a thread execution
* @return number of dates
*/
private static int printAndCountExceptions(Result res) {
diff --git a/tls/src/main/java/com/iluwatar/tls/DateFormatCallable.java b/tls/src/main/java/com/iluwatar/tls/DateFormatCallable.java
index d83ad6441f08..8f0006b9e3a5 100644
--- a/tls/src/main/java/com/iluwatar/tls/DateFormatCallable.java
+++ b/tls/src/main/java/com/iluwatar/tls/DateFormatCallable.java
@@ -23,25 +23,23 @@
package com.iluwatar.tls;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.concurrent.Callable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
- * DateFormatCallable converts string dates to a date format using
- * SimpleDateFormat. The date format and the date value will be passed to the
- * Callable by the constructor. The constructor creates a instance of
- * SimpleDateFormat and stores it in a ThreadLocal class variable. For the
- * complete description of the example see {@link App}
- *
- * You can comment out the code marked with //TLTL and comment in the
- * code marked //NTLNTL. Then you can see what will happen if you do not
- * use the ThreadLocal. For details see the description of {@link App}
+ * DateFormatCallable converts string dates to a date format using SimpleDateFormat. The date format
+ * and the date value will be passed to the Callable by the constructor. The constructor creates a
+ * instance of SimpleDateFormat and stores it in a ThreadLocal class variable. For the complete
+ * description of the example see {@link App}.
*
- * @author Thomas Bauer, 2017
+ * You can comment out the code marked with //TLTL and comment in the code marked //NTLNTL. Then
+ * you can see what will happen if you do not use the ThreadLocal. For details see the description
+ * of {@link App}
+ *
+ * @author Thomas Bauer, 2017
*/
public class DateFormatCallable implements Callable
- * In this example we use Java serialization to write representations of {@link RainbowFish} objects
- * to file. {@link RainbowFish} is the initial version which we can easily read and write using
- * {@link RainbowFishSerializer} methods. {@link RainbowFish} then evolves to {@link RainbowFishV2}
- * and we again write it to file with a method designed to do just that. However, the reader client
- * does not know about the new format and still reads with the method designed for V1 schema.
- * Fortunately the reading method has been designed with the Tolerant Reader pattern and does not
- * break even though {@link RainbowFishV2} has new fields that are serialized.
*
+ * In this example we use Java serialization to write representations of {@link RainbowFish}
+ * objects to file. {@link RainbowFish} is the initial version which we can easily read and write
+ * using {@link RainbowFishSerializer} methods. {@link RainbowFish} then evolves to {@link
+ * RainbowFishV2} and we again write it to file with a method designed to do just that. However, the
+ * reader client does not know about the new format and still reads with the method designed for V1
+ * schema. Fortunately the reading method has been designed with the Tolerant Reader pattern and
+ * does not break even though {@link RainbowFishV2} has new fields that are serialized.
*/
public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
/**
- * Program entry point
+ * Program entry point.
*/
public static void main(String[] args) throws IOException, ClassNotFoundException {
// Write V1
@@ -59,8 +56,8 @@ public static void main(String[] args) throws IOException, ClassNotFoundExceptio
// Read V1
var deserializedRainbowFishV1 = RainbowFishSerializer.readV1("fish1.out");
LOGGER.info("deserializedFishV1 name={} age={} length={} weight={}",
- deserializedRainbowFishV1.getName(), deserializedRainbowFishV1.getAge(),
- deserializedRainbowFishV1.getLengthMeters(), deserializedRainbowFishV1.getWeightTons());
+ deserializedRainbowFishV1.getName(), deserializedRainbowFishV1.getAge(),
+ deserializedRainbowFishV1.getLengthMeters(), deserializedRainbowFishV1.getWeightTons());
// Write V2
var fishV2 = new RainbowFishV2("Scar", 5, 12, 15, true, true, true);
LOGGER.info(
diff --git a/tolerant-reader/src/main/java/com/iluwatar/tolerantreader/RainbowFish.java b/tolerant-reader/src/main/java/com/iluwatar/tolerantreader/RainbowFish.java
index 221f1c32fc36..775fc98f7011 100644
--- a/tolerant-reader/src/main/java/com/iluwatar/tolerantreader/RainbowFish.java
+++ b/tolerant-reader/src/main/java/com/iluwatar/tolerantreader/RainbowFish.java
@@ -26,9 +26,7 @@
import java.io.Serializable;
/**
- *
- * RainbowFish is the initial schema
- *
+ * RainbowFish is the initial schema.
*/
public class RainbowFish implements Serializable {
@@ -40,7 +38,7 @@ public class RainbowFish implements Serializable {
private int weightTons;
/**
- * Constructor
+ * Constructor.
*/
public RainbowFish(String name, int age, int lengthMeters, int weightTons) {
this.name = name;
diff --git a/tolerant-reader/src/main/java/com/iluwatar/tolerantreader/RainbowFishSerializer.java b/tolerant-reader/src/main/java/com/iluwatar/tolerantreader/RainbowFishSerializer.java
index 3e8db7622101..33c464190e1f 100644
--- a/tolerant-reader/src/main/java/com/iluwatar/tolerantreader/RainbowFishSerializer.java
+++ b/tolerant-reader/src/main/java/com/iluwatar/tolerantreader/RainbowFishSerializer.java
@@ -28,16 +28,13 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
-import java.util.HashMap;
import java.util.Map;
/**
- *
* RainbowFishSerializer provides methods for reading and writing {@link RainbowFish} objects to
- * file. Tolerant Reader pattern is implemented here by serializing maps instead of
- * {@link RainbowFish} objects. This way the reader does not break even though new properties are
- * added to the schema.
- *
+ * file. Tolerant Reader pattern is implemented here by serializing maps instead of {@link
+ * RainbowFish} objects. This way the reader does not break even though new properties are added to
+ * the schema.
*/
public final class RainbowFishSerializer {
@@ -45,54 +42,55 @@ private RainbowFishSerializer() {
}
/**
- * Write V1 RainbowFish to file
+ * Write V1 RainbowFish to file.
*/
public static void writeV1(RainbowFish rainbowFish, String filename) throws IOException {
var map = Map.of(
- "name", rainbowFish.getName(),
- "age", String.format("%d", rainbowFish.getAge()),
- "lengthMeters", String.format("%d", rainbowFish.getLengthMeters()),
- "weightTons", String.format("%d", rainbowFish.getWeightTons())
+ "name", rainbowFish.getName(),
+ "age", String.format("%d", rainbowFish.getAge()),
+ "lengthMeters", String.format("%d", rainbowFish.getLengthMeters()),
+ "weightTons", String.format("%d", rainbowFish.getWeightTons())
);
try (var fileOut = new FileOutputStream(filename);
- var objOut = new ObjectOutputStream(fileOut)) {
+ var objOut = new ObjectOutputStream(fileOut)) {
objOut.writeObject(map);
}
}
/**
- * Write V2 RainbowFish to file
+ * Write V2 RainbowFish to file.
*/
public static void writeV2(RainbowFishV2 rainbowFish, String filename) throws IOException {
var map = Map.of(
- "name", rainbowFish.getName(),
- "age", String.format("%d", rainbowFish.getAge()),
- "lengthMeters", String.format("%d", rainbowFish.getLengthMeters()),
- "weightTons", String.format("%d", rainbowFish.getWeightTons()),
- "angry", Boolean.toString(rainbowFish.getAngry()),
- "hungry", Boolean.toString(rainbowFish.getHungry()),
- "sleeping", Boolean.toString(rainbowFish.getSleeping())
+ "name", rainbowFish.getName(),
+ "age", String.format("%d", rainbowFish.getAge()),
+ "lengthMeters", String.format("%d", rainbowFish.getLengthMeters()),
+ "weightTons", String.format("%d", rainbowFish.getWeightTons()),
+ "angry", Boolean.toString(rainbowFish.getAngry()),
+ "hungry", Boolean.toString(rainbowFish.getHungry()),
+ "sleeping", Boolean.toString(rainbowFish.getSleeping())
);
try (var fileOut = new FileOutputStream(filename);
- var objOut = new ObjectOutputStream(fileOut)) {
+ var objOut = new ObjectOutputStream(fileOut)) {
objOut.writeObject(map);
}
}
/**
- * Read V1 RainbowFish from file
+ * Read V1 RainbowFish from file.
*/
public static RainbowFish readV1(String filename) throws IOException, ClassNotFoundException {
Map