Changed package naming across all examples.

This commit is contained in:
Ilkka Seppala
2015-05-31 11:55:18 +03:00
parent 703ebd3e20
commit 8524c75ba6
437 changed files with 1095 additions and 1402 deletions

View File

@ -0,0 +1,42 @@
package com.iluwatar.tolerantreader;
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.
*
* In this example we use Java serialization to write representations of RainbowFish
* objects to file. RainbowFish is the initial version which we can easily read and
* write using RainbowFishSerializer methods. RainbowFish then evolves to 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 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()));
}
}

View File

@ -0,0 +1,42 @@
package com.iluwatar.tolerantreader;
import java.io.Serializable;
/**
*
* RainbowFish is the initial schema
*
*/
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;
}
public int getAge() {
return age;
}
public int getLengthMeters() {
return lengthMeters;
}
public int getWeightTons() {
return weightTons;
}
}

View File

@ -0,0 +1,80 @@
package com.iluwatar.tolerantreader;
import java.io.FileInputStream;
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 RainbowFish objects to file.
* Tolerant Reader pattern is implemented here by serializing maps instead of 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 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")));
}
}

View File

@ -0,0 +1,38 @@
package com.iluwatar.tolerantreader;
/**
*
* RainbowFishV2 is the evolved schema
*
*/
public class RainbowFishV2 extends RainbowFish {
private static final long serialVersionUID = 1L;
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;
}
}