Resolves checkstyle errors for template-method thread-pool throttling tls tolerant-reader (#1073)

* Reduces checkstyle errors in template-method

* Reduces checkstyle errors in thread-pool

* Reduces checkstyle errors in throttling

* Reduces checkstyle errors in tls

* Reduces checkstyle errors in tolerant-reader
This commit is contained in:
Anurag Agarwal 2019-11-10 23:15:17 +05:30 committed by Ilkka Seppälä
parent 9c8ad4485b
commit b92eb5229d
23 changed files with 176 additions and 223 deletions

View File

@ -24,18 +24,16 @@
package com.iluwatar.templatemethod;
/**
*
* Template Method defines a skeleton for an algorithm. The algorithm subclasses provide
* implementation for the blank parts.
* <p>
* In this example {@link HalflingThief} contains {@link StealingMethod} that can be changed. First
* the thief hits with {@link HitAndRunMethod} and then with {@link SubtleMethod}.
*
* <p>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
*/

View File

@ -24,9 +24,7 @@
package com.iluwatar.templatemethod;
/**
*
* Halfling thief uses {@link StealingMethod} to steal.
*
*/
public class HalflingThief {

View File

@ -27,9 +27,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* HitAndRunMethod implementation of {@link StealingMethod}.
*
*/
public class HitAndRunMethod extends StealingMethod {

View File

@ -27,9 +27,7 @@ import org.slf4j.Logger;
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();

View File

@ -27,9 +27,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* SubtleMethod implementation of {@link StealingMethod}.
*
*/
public class SubtleMethod extends StealingMethod {

View File

@ -23,35 +23,31 @@
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.
* <p>
* 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.
*
* <p>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
*/

View File

@ -24,9 +24,7 @@
package com.iluwatar.threadpool;
/**
*
* CoffeeMakingTask is a concrete task
*
* CoffeeMakingTask is a concrete task.
*/
public class CoffeeMakingTask extends Task {

View File

@ -24,9 +24,7 @@
package com.iluwatar.threadpool;
/**
*
* PotatoPeelingTask is a concrete task
*
* PotatoPeelingTask is a concrete task.
*/
public class PotatoPeelingTask extends Task {

View File

@ -26,9 +26,7 @@ package com.iluwatar.threadpool;
import java.util.concurrent.atomic.AtomicInteger;
/**
*
* Abstract base class for tasks
*
* Abstract base class for tasks.
*/
public abstract class Task {

View File

@ -27,9 +27,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* Worker implements {@link Runnable} and thus can be executed by {@link java.util.concurrent.ExecutorService}
*
* Worker implements {@link Runnable} and thus can be executed by {@link
* java.util.concurrent.ExecutorService}.
*/
public class Worker implements Runnable {

View File

@ -23,33 +23,30 @@
package com.iluwatar.throttling;
import com.iluwatar.throttling.timer.ThrottleTimerImpl;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.iluwatar.throttling.timer.Throttler;
import com.iluwatar.throttling.timer.ThrottleTimerImpl;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* Throttling pattern is a design pattern to throttle or limit the use of resources or even a complete service by
* users or a particular tenant. This can allow systems to continue to function and meet service level agreements,
* even when an increase in demand places load on resources.
* Throttling pattern is a design pattern to throttle or limit the use of resources or even a
* complete service by users or a particular tenant. This can allow systems to continue to function
* and meet service level agreements, even when an increase in demand places load on resources.
* <p>
* 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.
* </p>
* ({@link Tenant}) is the Tenant POJO class with which many tenants can be created
* ({@link B2BService}) is the service which is consumed by the tenants and is throttled.
* ({@link Tenant}) is the Tenant POJO class with which many tenants can be created ({@link
* B2BService}) is the service which is consumed by the tenants and is throttled.
*/
public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
/**
* Application entry point
* Application entry point.
*
* @param args main arguments
*/
public static void main(String[] args) {
@ -71,7 +68,7 @@ public class App {
}
/**
* Make calls to the B2BService dummy API
* Make calls to the B2BService dummy API.
*/
private static void makeServiceCalls(Tenant tenant, CallsCount callsCount) {
var timer = new ThrottleTimerImpl(10, callsCount);

View File

@ -23,15 +23,14 @@
package com.iluwatar.throttling;
import com.iluwatar.throttling.timer.Throttler;
import java.util.concurrent.ThreadLocalRandom;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.iluwatar.throttling.timer.Throttler;
import java.util.concurrent.ThreadLocalRandom;
/**
* A service which accepts a tenant and throttles the resource based on the time given to the tenant.
* A service which accepts a tenant and throttles the resource based on the time given to the
* tenant.
*/
class B2BService {
@ -44,6 +43,7 @@ class B2BService {
}
/**
* Calls dummy customer api.
*
* @return customer id which is randomly generated
*/

View File

@ -23,18 +23,17 @@
package com.iluwatar.throttling;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A class to keep track of the counter of different Tenants
* @author drastogi
* A class to keep track of the counter of different Tenants.
*
* @author drastogi
*/
public final class CallsCount {
@ -43,6 +42,7 @@ public final class CallsCount {
/**
* Add a new tenant to the map.
*
* @param tenantName name of the tenant.
*/
public void addTenant(String tenantName) {
@ -51,6 +51,7 @@ public final class CallsCount {
/**
* Increment the count of the specified tenant.
*
* @param tenantName name of the tenant.
*/
public void incrementCount(String tenantName) {
@ -58,6 +59,7 @@ public final class CallsCount {
}
/**
* Get count of tenant based on tenant name.
*
* @param tenantName name of the tenant.
* @return the count of the tenant.

View File

@ -34,6 +34,7 @@ public class Tenant {
private int allowedCallsPerSecond;
/**
* Constructor.
*
* @param name Name of the tenant
* @param allowedCallsPerSecond The number of calls allowed for a particular tenant.

View File

@ -21,16 +21,12 @@
* THE SOFTWARE.
*/
/**
*
*/
package com.iluwatar.throttling.timer;
import com.iluwatar.throttling.CallsCount;
import java.util.Timer;
import java.util.TimerTask;
import com.iluwatar.throttling.CallsCount;
/**
* Implementation of throttler interface. This class resets the counter every second.
* @author drastogi

View File

@ -21,9 +21,6 @@
* THE SOFTWARE.
*/
/**
*
*/
package com.iluwatar.throttling.timer;
/**

View File

@ -23,58 +23,52 @@
package com.iluwatar.tls;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
/**
* ThreadLocal pattern
* <p>
* 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.
* <p>
* 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.
* <p>
* 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.
* <p>
* 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.
* <p>
* 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.
* <p>
* 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.
*
* <p>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.
*
* <p>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.
*
* <p>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.
*
* <p>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.
*
* <p>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.
*
* <p>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
* Program entry point.
*
* @param args
* command line args
* @param args command line args
*/
public static void main(String[] args) {
int counterDateValues = 0;
@ -115,7 +109,7 @@ public class App {
}
/**
* Print result (date values) of a thread execution and count dates
* Print result (date values) of a thread execution and count dates.
*
* @param res contains results of a thread execution
*/
@ -128,13 +122,14 @@ public class App {
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
* Print result (exceptions) of a thread execution and count exceptions.
*
* @param res contains results of a thread execution
* @return number of dates

View File

@ -23,23 +23,21 @@
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}
* 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}
* <p>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
*/
@ -54,12 +52,10 @@ public class DateFormatCallable implements Callable<Result> {
/**
* The date format and the date value are passed to the constructor
* The date format and the date value are passed to the constructor.
*
* @param inDateFormat
* string date format string, e.g. "dd/MM/yyyy"
* @param inDateValue
* string date value, e.g. "21/06/2016"
* @param inDateFormat string date format string, e.g. "dd/MM/yyyy"
* @param inDateValue string date value, e.g. "21/06/2016"
*/
public DateFormatCallable(String inDateFormat, String inDateValue) {
final String idf = inDateFormat; //TLTL
@ -73,9 +69,6 @@ public class DateFormatCallable implements Callable<Result> {
this.dateValue = inDateValue;
}
/**
* @see java.util.concurrent.Callable#call()
*/
@Override
public Result call() {
LOGGER.info(Thread.currentThread() + " started executing...");

View File

@ -32,8 +32,8 @@ import java.util.Date;
import java.util.List;
/**
* Result object that will be returned by the Callable {@link DateFormatCallable}
* used in {@link App}
* Result object that will be returned by the Callable {@link DateFormatCallable} used in {@link
* App}.
*
* @author Thomas Bauer, 2017
*/
@ -46,6 +46,7 @@ public class Result {
private List<String> exceptionList = new ArrayList<String>();
/**
* Get list of date values collected within a thread execution.
*
* @return List of date values collected within an thread execution
*/
@ -54,6 +55,7 @@ public class Result {
}
/**
* Get list of exceptions thrown within a thread execution.
*
* @return List of exceptions thrown within an thread execution
*/

View File

@ -23,32 +23,29 @@
package com.iluwatar.tolerantreader;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
/**
*
* Tolerant Reader is an integration pattern that helps creating robust communication systems. The
* idea is to be as tolerant as possible when reading data from another service. This way, when the
* communication schema changes, the readers must not break.
* <p>
* 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.
*
* <p>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

View File

@ -26,9 +26,7 @@ package com.iluwatar.tolerantreader;
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;

View File

@ -28,16 +28,13 @@ import java.io.FileOutputStream;
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,7 +42,7 @@ public final class 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(
@ -62,7 +59,7 @@ public final class RainbowFishSerializer {
}
/**
* Write V2 RainbowFish to file
* Write V2 RainbowFish to file.
*/
public static void writeV2(RainbowFishV2 rainbowFish, String filename) throws IOException {
var map = Map.of(
@ -82,7 +79,7 @@ public final class RainbowFishSerializer {
}
/**
* Read V1 RainbowFish from file
* Read V1 RainbowFish from file.
*/
public static RainbowFish readV1(String filename) throws IOException, ClassNotFoundException {
Map<String, String> map = null;
@ -92,7 +89,8 @@ public final class RainbowFishSerializer {
map = (Map<String, String>) objIn.readObject();
}
return new RainbowFish(map.get("name"), Integer.parseInt(map.get("age")), Integer.parseInt(map.get("lengthMeters")),
return new RainbowFish(map.get("name"), Integer.parseInt(map.get("age")), Integer
.parseInt(map.get("lengthMeters")),
Integer.parseInt(map.get("weightTons")));
}
}

View File

@ -24,9 +24,7 @@
package com.iluwatar.tolerantreader;
/**
*
* RainbowFishV2 is the evolved schema
*
* RainbowFishV2 is the evolved schema.
*/
public class RainbowFishV2 extends RainbowFish {
@ -41,7 +39,7 @@ public class RainbowFishV2 extends RainbowFish {
}
/**
* Constructor
* Constructor.
*/
public RainbowFishV2(String name, int age, int lengthMeters, int weightTons, boolean sleeping,
boolean hungry, boolean angry) {