Skip to content

Commit 058cb87

Browse files
committed
Code comments.
1 parent b3bf437 commit 058cb87

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

tolerant-reader/src/main/java/com/iluwatar/App.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@
44

55
/**
66
*
7+
* Tolerant Reader is an integration pattern that helps creating robust communication
8+
* systems. The idea is to be as tolerant as possible when reading data from another
9+
* service. This way, when the communication schema changes, the readers must not break.
710
*
11+
* In this example we use Java serialization to write representations of RainbowFish
12+
* objects to file. RainbowFish is the initial version which we can easily read and
13+
* write using RainbowFishSerializer methods. RainbowFish then evolves to RainbowFishV2
14+
* and we again write it to file with a method designed to do just that. However, the reader
15+
* client does not know about the new format and still reads with the method designed for
16+
* V1 schema. Fortunately the reading method has been designed with the Tolerant Reader
17+
* pattern and does not break even though RainbowFishV2 has new fields that are serialized.
818
*
919
*/
1020
public class App {

tolerant-reader/src/main/java/com/iluwatar/RainbowFish.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
import java.io.Serializable;
44

5+
/**
6+
*
7+
* RainbowFish is the initial schema
8+
*
9+
*/
510
public class RainbowFish implements Serializable {
611

712
private static final long serialVersionUID = 1L;

tolerant-reader/src/main/java/com/iluwatar/RainbowFishSerializer.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,21 @@
99
import java.util.HashMap;
1010
import java.util.Map;
1111

12+
/**
13+
*
14+
* RainbowFishSerializer provides methods for reading and writing RainbowFish objects to file.
15+
* Tolerant Reader pattern is implemented here by serializing maps instead of RainbowFish objects.
16+
* This way the reader does not break even though new properties are added to the schema.
17+
*
18+
*/
1219
public class RainbowFishSerializer {
1320

21+
/**
22+
* Write V1 RainbowFish to file
23+
* @param rainbowFish
24+
* @param filename
25+
* @throws IOException
26+
*/
1427
public static void writeV1(RainbowFish rainbowFish, String filename) throws IOException {
1528
Map<String, String> map = new HashMap<>();
1629
map.put("name", rainbowFish.getName());
@@ -24,6 +37,12 @@ public static void writeV1(RainbowFish rainbowFish, String filename) throws IOEx
2437
fileOut.close();
2538
}
2639

40+
/**
41+
* Write V2 RainbowFish to file
42+
* @param rainbowFish
43+
* @param filename
44+
* @throws IOException
45+
*/
2746
public static void writeV2(RainbowFishV2 rainbowFish, String filename) throws IOException {
2847
Map<String, String> map = new HashMap<>();
2948
map.put("name", rainbowFish.getName());
@@ -40,6 +59,13 @@ public static void writeV2(RainbowFishV2 rainbowFish, String filename) throws IO
4059
fileOut.close();
4160
}
4261

62+
/**
63+
* Read V1 RainbowFish from file
64+
* @param filename
65+
* @return
66+
* @throws IOException
67+
* @throws ClassNotFoundException
68+
*/
4369
public static RainbowFish readV1(String filename) throws IOException, ClassNotFoundException {
4470
Map<String, String> map = null;
4571
FileInputStream fileIn = new FileInputStream(filename);

tolerant-reader/src/main/java/com/iluwatar/RainbowFishV2.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.iluwatar;
22

3+
/**
4+
*
5+
* RainbowFishV2 is the evolved schema
6+
*
7+
*/
38
public class RainbowFishV2 extends RainbowFish {
49

510
private static final long serialVersionUID = 1L;

0 commit comments

Comments
 (0)