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:
		
				
					committed by
					
						 Ilkka Seppälä
						Ilkka Seppälä
					
				
			
			
				
	
			
			
			
						parent
						
							9c8ad4485b
						
					
				
				
					commit
					b92eb5229d
				
			| @@ -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 | ||||
| @@ -59,8 +56,8 @@ public class App { | ||||
|     // 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( | ||||
|   | ||||
| @@ -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; | ||||
|   | ||||
| @@ -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,54 +42,55 @@ 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( | ||||
|             "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<String, String> map = null; | ||||
|  | ||||
|     try (var fileIn = new FileInputStream(filename); | ||||
|         var objIn = new ObjectInputStream(fileIn)) { | ||||
|          var objIn = new ObjectInputStream(fileIn)) { | ||||
|       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"))); | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -24,9 +24,7 @@ | ||||
| package com.iluwatar.tolerantreader; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|  * RainbowFishV2 is the evolved schema | ||||
|  * | ||||
|  * RainbowFishV2 is the evolved schema. | ||||
|  */ | ||||
| public class RainbowFishV2 extends RainbowFish { | ||||
|  | ||||
| @@ -41,10 +39,10 @@ public class RainbowFishV2 extends RainbowFish { | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * Constructor | ||||
|    * Constructor. | ||||
|    */ | ||||
|   public RainbowFishV2(String name, int age, int lengthMeters, int weightTons, boolean sleeping, | ||||
|       boolean hungry, boolean angry) { | ||||
|                        boolean hungry, boolean angry) { | ||||
|     this(name, age, lengthMeters, weightTons); | ||||
|     this.sleeping = sleeping; | ||||
|     this.hungry = hungry; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user