* #984 for unitofwork, tolerantreader, twin * #984 for unitofwork, tolerantreader, twin * #987 for visitor, value-object, unitofwork, typeobjectpattern, tolerantreader, twin, tranpoline
This commit is contained in:
parent
c7fee7bb07
commit
fadad43f8b
@ -52,24 +52,24 @@ public class App {
|
||||
*/
|
||||
public static void main(String[] args) throws IOException, ClassNotFoundException {
|
||||
// Write V1
|
||||
RainbowFish fishV1 = new RainbowFish("Zed", 10, 11, 12);
|
||||
var fishV1 = new RainbowFish("Zed", 10, 11, 12);
|
||||
LOGGER.info("fishV1 name={} age={} length={} weight={}", fishV1.getName(),
|
||||
fishV1.getAge(), fishV1.getLengthMeters(), fishV1.getWeightTons());
|
||||
RainbowFishSerializer.writeV1(fishV1, "fish1.out");
|
||||
// Read V1
|
||||
RainbowFish deserializedFishV1 = RainbowFishSerializer.readV1("fish1.out");
|
||||
var deserializedRainbowFishV1 = RainbowFishSerializer.readV1("fish1.out");
|
||||
LOGGER.info("deserializedFishV1 name={} age={} length={} weight={}",
|
||||
deserializedFishV1.getName(), deserializedFishV1.getAge(),
|
||||
deserializedFishV1.getLengthMeters(), deserializedFishV1.getWeightTons());
|
||||
deserializedRainbowFishV1.getName(), deserializedRainbowFishV1.getAge(),
|
||||
deserializedRainbowFishV1.getLengthMeters(), deserializedRainbowFishV1.getWeightTons());
|
||||
// Write V2
|
||||
RainbowFishV2 fishV2 = new RainbowFishV2("Scar", 5, 12, 15, true, true, true);
|
||||
var fishV2 = new RainbowFishV2("Scar", 5, 12, 15, true, true, true);
|
||||
LOGGER.info(
|
||||
"fishV2 name={} age={} length={} weight={} sleeping={} hungry={} angry={}",
|
||||
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");
|
||||
var deserializedFishV2 = RainbowFishSerializer.readV1("fish2.out");
|
||||
LOGGER.info("deserializedFishV2 name={} age={} length={} weight={}",
|
||||
deserializedFishV2.getName(), deserializedFishV2.getAge(),
|
||||
deserializedFishV2.getLengthMeters(), deserializedFishV2.getWeightTons());
|
||||
|
@ -48,13 +48,15 @@ public final class RainbowFishSerializer {
|
||||
* Write V1 RainbowFish to file
|
||||
*/
|
||||
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()));
|
||||
try (FileOutputStream fileOut = new FileOutputStream(filename);
|
||||
ObjectOutputStream objOut = new ObjectOutputStream(fileOut)) {
|
||||
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())
|
||||
);
|
||||
|
||||
try (var fileOut = new FileOutputStream(filename);
|
||||
var objOut = new ObjectOutputStream(fileOut)) {
|
||||
objOut.writeObject(map);
|
||||
}
|
||||
}
|
||||
@ -63,16 +65,18 @@ public final class RainbowFishSerializer {
|
||||
* Write V2 RainbowFish to file
|
||||
*/
|
||||
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()));
|
||||
try (FileOutputStream fileOut = new FileOutputStream(filename);
|
||||
ObjectOutputStream objOut = new ObjectOutputStream(fileOut)) {
|
||||
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())
|
||||
);
|
||||
|
||||
try (var fileOut = new FileOutputStream(filename);
|
||||
var objOut = new ObjectOutputStream(fileOut)) {
|
||||
objOut.writeObject(map);
|
||||
}
|
||||
}
|
||||
@ -83,8 +87,8 @@ public final class RainbowFishSerializer {
|
||||
public static RainbowFish readV1(String filename) throws IOException, ClassNotFoundException {
|
||||
Map<String, String> map = null;
|
||||
|
||||
try (FileInputStream fileIn = new FileInputStream(filename);
|
||||
ObjectInputStream objIn = new ObjectInputStream(fileIn)) {
|
||||
try (var fileIn = new FileInputStream(filename);
|
||||
var objIn = new ObjectInputStream(fileIn)) {
|
||||
map = (Map<String, String>) objIn.readObject();
|
||||
}
|
||||
|
||||
|
@ -41,8 +41,8 @@ public class App {
|
||||
*/
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
BallItem ballItem = new BallItem();
|
||||
BallThread ballThread = new BallThread();
|
||||
var ballItem = new BallItem();
|
||||
var ballThread = new BallThread();
|
||||
|
||||
ballItem.setTwin(ballThread);
|
||||
ballThread.setTwin(ballItem);
|
||||
|
@ -39,7 +39,7 @@ public class App {
|
||||
var shyam = new Student(2, "Shyam", "Z bridge, Pune");
|
||||
var gopi = new Student(3, "Gopi", "Street 10, Mumbai");
|
||||
|
||||
HashMap<String, List<Student>> context = new HashMap<>();
|
||||
var context = new HashMap<String, List<Student>>();
|
||||
var studentDatabase = new StudentDatabase();
|
||||
var studentRepository = new StudentRepository(context, studentDatabase);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user