Merge pull request #281 from ankurkaushal/master
Reformat according to google style guide
This commit is contained in:
@ -4,39 +4,43 @@ 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.
|
||||
* 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.
|
||||
* 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 {
|
||||
|
||||
public static void main( String[] args ) throws IOException, ClassNotFoundException {
|
||||
// Write V1
|
||||
RainbowFish fishV1 = new RainbowFish("Zed", 10, 11, 12);
|
||||
System.out.println(String.format("fishV1 name=%s age=%d length=%d weight=%d", fishV1.getName(),
|
||||
fishV1.getAge(), fishV1.getLengthMeters(), fishV1.getWeightTons()));
|
||||
RainbowFishSerializer.writeV1(fishV1, "fish1.out");
|
||||
// Read V1
|
||||
RainbowFish deserializedFishV1 = RainbowFishSerializer.readV1("fish1.out");
|
||||
System.out.println(String.format("deserializedFishV1 name=%s age=%d length=%d weight=%d", deserializedFishV1.getName(),
|
||||
deserializedFishV1.getAge(), deserializedFishV1.getLengthMeters(), deserializedFishV1.getWeightTons()));
|
||||
// Write V2
|
||||
RainbowFishV2 fishV2 = new RainbowFishV2("Scar", 5, 12, 15, true, true, true);
|
||||
System.out.println(String.format("fishV2 name=%s age=%d length=%d weight=%d sleeping=%b hungry=%b angry=%b", fishV2.getName(),
|
||||
fishV2.getAge(), fishV2.getLengthMeters(), fishV2.getWeightTons(), fishV2.getHungry(), fishV2.getAngry(), fishV2.getSleeping()));
|
||||
RainbowFishSerializer.writeV2(fishV2, "fish2.out");
|
||||
// Read V2 with V1 method
|
||||
RainbowFish deserializedFishV2 = RainbowFishSerializer.readV1("fish2.out");
|
||||
System.out.println(String.format("deserializedFishV2 name=%s age=%d length=%d weight=%d", deserializedFishV2.getName(),
|
||||
deserializedFishV2.getAge(), deserializedFishV2.getLengthMeters(), deserializedFishV2.getWeightTons()));
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException, ClassNotFoundException {
|
||||
// Write V1
|
||||
RainbowFish fishV1 = new RainbowFish("Zed", 10, 11, 12);
|
||||
System.out.println(String.format("fishV1 name=%s age=%d length=%d weight=%d", fishV1.getName(),
|
||||
fishV1.getAge(), fishV1.getLengthMeters(), fishV1.getWeightTons()));
|
||||
RainbowFishSerializer.writeV1(fishV1, "fish1.out");
|
||||
// Read V1
|
||||
RainbowFish deserializedFishV1 = RainbowFishSerializer.readV1("fish1.out");
|
||||
System.out.println(String.format("deserializedFishV1 name=%s age=%d length=%d weight=%d",
|
||||
deserializedFishV1.getName(), deserializedFishV1.getAge(),
|
||||
deserializedFishV1.getLengthMeters(), deserializedFishV1.getWeightTons()));
|
||||
// Write V2
|
||||
RainbowFishV2 fishV2 = new RainbowFishV2("Scar", 5, 12, 15, true, true, true);
|
||||
System.out.println(String.format(
|
||||
"fishV2 name=%s age=%d length=%d weight=%d sleeping=%b hungry=%b angry=%b",
|
||||
fishV2.getName(), fishV2.getAge(), fishV2.getLengthMeters(), fishV2.getWeightTons(),
|
||||
fishV2.getHungry(), fishV2.getAngry(), fishV2.getSleeping()));
|
||||
RainbowFishSerializer.writeV2(fishV2, "fish2.out");
|
||||
// Read V2 with V1 method
|
||||
RainbowFish deserializedFishV2 = RainbowFishSerializer.readV1("fish2.out");
|
||||
System.out.println(String.format("deserializedFishV2 name=%s age=%d length=%d weight=%d",
|
||||
deserializedFishV2.getName(), deserializedFishV2.getAge(),
|
||||
deserializedFishV2.getLengthMeters(), deserializedFishV2.getWeightTons()));
|
||||
}
|
||||
}
|
||||
|
@ -9,34 +9,34 @@ import java.io.Serializable;
|
||||
*/
|
||||
public class RainbowFish implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String name;
|
||||
private int age;
|
||||
private int lengthMeters;
|
||||
private int weightTons;
|
||||
|
||||
public RainbowFish(String name, int age, int lengthMeters, int weightTons) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.lengthMeters = lengthMeters;
|
||||
this.weightTons = weightTons;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
private String name;
|
||||
private int age;
|
||||
private int lengthMeters;
|
||||
private int weightTons;
|
||||
|
||||
public int getLengthMeters() {
|
||||
return lengthMeters;
|
||||
}
|
||||
public RainbowFish(String name, int age, int lengthMeters, int weightTons) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.lengthMeters = lengthMeters;
|
||||
this.weightTons = weightTons;
|
||||
}
|
||||
|
||||
public int getWeightTons() {
|
||||
return weightTons;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public int getLengthMeters() {
|
||||
return lengthMeters;
|
||||
}
|
||||
|
||||
public int getWeightTons() {
|
||||
return weightTons;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -10,71 +10,73 @@ 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.
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
public class RainbowFishSerializer {
|
||||
|
||||
/**
|
||||
* Write V1 RainbowFish to file
|
||||
* @param rainbowFish
|
||||
* @param filename
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void writeV1(RainbowFish rainbowFish, String filename) throws IOException {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("name", rainbowFish.getName());
|
||||
map.put("age", String.format("%d", rainbowFish.getAge()));
|
||||
map.put("lengthMeters", String.format("%d", rainbowFish.getLengthMeters()));
|
||||
map.put("weightTons", String.format("%d", rainbowFish.getWeightTons()));
|
||||
FileOutputStream fileOut = new FileOutputStream(filename);
|
||||
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
|
||||
objOut.writeObject(map);
|
||||
objOut.close();
|
||||
fileOut.close();
|
||||
}
|
||||
/**
|
||||
* Write V1 RainbowFish to file
|
||||
*
|
||||
* @param rainbowFish
|
||||
* @param filename
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void writeV1(RainbowFish rainbowFish, String filename) throws IOException {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("name", rainbowFish.getName());
|
||||
map.put("age", String.format("%d", rainbowFish.getAge()));
|
||||
map.put("lengthMeters", String.format("%d", rainbowFish.getLengthMeters()));
|
||||
map.put("weightTons", String.format("%d", rainbowFish.getWeightTons()));
|
||||
FileOutputStream fileOut = new FileOutputStream(filename);
|
||||
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
|
||||
objOut.writeObject(map);
|
||||
objOut.close();
|
||||
fileOut.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Write V2 RainbowFish to file
|
||||
* @param rainbowFish
|
||||
* @param filename
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void writeV2(RainbowFishV2 rainbowFish, String filename) throws IOException {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("name", rainbowFish.getName());
|
||||
map.put("age", String.format("%d", rainbowFish.getAge()));
|
||||
map.put("lengthMeters", String.format("%d", rainbowFish.getLengthMeters()));
|
||||
map.put("weightTons", String.format("%d", rainbowFish.getWeightTons()));
|
||||
map.put("angry", Boolean.toString(rainbowFish.getAngry()));
|
||||
map.put("hungry", Boolean.toString(rainbowFish.getHungry()));
|
||||
map.put("sleeping", Boolean.toString(rainbowFish.getSleeping()));
|
||||
FileOutputStream fileOut = new FileOutputStream(filename);
|
||||
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
|
||||
objOut.writeObject(map);
|
||||
objOut.close();
|
||||
fileOut.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Read V1 RainbowFish from file
|
||||
* @param filename
|
||||
* @return
|
||||
* @throws IOException
|
||||
* @throws ClassNotFoundException
|
||||
*/
|
||||
public static RainbowFish readV1(String filename) throws IOException, ClassNotFoundException {
|
||||
Map<String, String> map = null;
|
||||
FileInputStream fileIn = new FileInputStream(filename);
|
||||
ObjectInputStream objIn = new ObjectInputStream(fileIn);
|
||||
map = (Map<String, String>) objIn.readObject();
|
||||
objIn.close();
|
||||
fileIn.close();
|
||||
return new RainbowFish(map.get("name"),
|
||||
Integer.parseInt(map.get("age")),
|
||||
Integer.parseInt(map.get("lengthMeters")),
|
||||
Integer.parseInt(map.get("weightTons")));
|
||||
}
|
||||
/**
|
||||
* Write V2 RainbowFish to file
|
||||
*
|
||||
* @param rainbowFish
|
||||
* @param filename
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void writeV2(RainbowFishV2 rainbowFish, String filename) throws IOException {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("name", rainbowFish.getName());
|
||||
map.put("age", String.format("%d", rainbowFish.getAge()));
|
||||
map.put("lengthMeters", String.format("%d", rainbowFish.getLengthMeters()));
|
||||
map.put("weightTons", String.format("%d", rainbowFish.getWeightTons()));
|
||||
map.put("angry", Boolean.toString(rainbowFish.getAngry()));
|
||||
map.put("hungry", Boolean.toString(rainbowFish.getHungry()));
|
||||
map.put("sleeping", Boolean.toString(rainbowFish.getSleeping()));
|
||||
FileOutputStream fileOut = new FileOutputStream(filename);
|
||||
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
|
||||
objOut.writeObject(map);
|
||||
objOut.close();
|
||||
fileOut.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Read V1 RainbowFish from file
|
||||
*
|
||||
* @param filename
|
||||
* @return
|
||||
* @throws IOException
|
||||
* @throws ClassNotFoundException
|
||||
*/
|
||||
public static RainbowFish readV1(String filename) throws IOException, ClassNotFoundException {
|
||||
Map<String, String> map = null;
|
||||
FileInputStream fileIn = new FileInputStream(filename);
|
||||
ObjectInputStream objIn = new ObjectInputStream(fileIn);
|
||||
map = (Map<String, String>) objIn.readObject();
|
||||
objIn.close();
|
||||
fileIn.close();
|
||||
return new RainbowFish(map.get("name"), Integer.parseInt(map.get("age")), Integer.parseInt(map
|
||||
.get("lengthMeters")), Integer.parseInt(map.get("weightTons")));
|
||||
}
|
||||
}
|
||||
|
@ -7,32 +7,33 @@ package com.iluwatar.tolerantreader;
|
||||
*/
|
||||
public class RainbowFishV2 extends RainbowFish {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private boolean sleeping;
|
||||
private boolean hungry;
|
||||
private boolean angry;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public RainbowFishV2(String name, int age, int lengthMeters, int weightTons) {
|
||||
super(name, age, lengthMeters, weightTons);
|
||||
}
|
||||
|
||||
public RainbowFishV2(String name, int age, int lengthMeters, int weightTons, boolean sleeping, boolean hungry, boolean angry) {
|
||||
this(name, age, lengthMeters, weightTons);
|
||||
this.sleeping = sleeping;
|
||||
this.hungry = hungry;
|
||||
this.angry = angry;
|
||||
}
|
||||
|
||||
public boolean getSleeping() {
|
||||
return sleeping;
|
||||
}
|
||||
|
||||
public boolean getHungry() {
|
||||
return hungry;
|
||||
}
|
||||
|
||||
public boolean getAngry() {
|
||||
return angry;
|
||||
}
|
||||
private boolean sleeping;
|
||||
private boolean hungry;
|
||||
private boolean angry;
|
||||
|
||||
public RainbowFishV2(String name, int age, int lengthMeters, int weightTons) {
|
||||
super(name, age, lengthMeters, weightTons);
|
||||
}
|
||||
|
||||
public RainbowFishV2(String name, int age, int lengthMeters, int weightTons, boolean sleeping,
|
||||
boolean hungry, boolean angry) {
|
||||
this(name, age, lengthMeters, weightTons);
|
||||
this.sleeping = sleeping;
|
||||
this.hungry = hungry;
|
||||
this.angry = angry;
|
||||
}
|
||||
|
||||
public boolean getSleeping() {
|
||||
return sleeping;
|
||||
}
|
||||
|
||||
public boolean getHungry() {
|
||||
return hungry;
|
||||
}
|
||||
|
||||
public boolean getAngry() {
|
||||
return angry;
|
||||
}
|
||||
}
|
||||
|
@ -15,19 +15,19 @@ import com.iluwatar.tolerantreader.App;
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() throws ClassNotFoundException, IOException {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
|
||||
@Before
|
||||
@After
|
||||
public void cleanup() {
|
||||
File file1 = new File("fish1.out");
|
||||
file1.delete();
|
||||
File file2 = new File("fish2.out");
|
||||
file2.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() throws ClassNotFoundException, IOException {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
|
||||
@Before
|
||||
@After
|
||||
public void cleanup() {
|
||||
File file1 = new File("fish1.out");
|
||||
file1.delete();
|
||||
File file2 = new File("fish2.out");
|
||||
file2.delete();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user