Skip to content

Commit

Permalink
#984 for unitofwork, tolerantreader, twin (#1044)
Browse files Browse the repository at this point in the history
* #984 for unitofwork, tolerantreader, twin

* #984 for unitofwork, tolerantreader, twin

* #987 for visitor, value-object, unitofwork, typeobjectpattern, tolerantreader, twin, tranpoline
  • Loading branch information
Anurag870 authored and iluwatar committed Oct 26, 2019
1 parent c7fee7b commit fadad43
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ private 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);
}
}
Expand All @@ -63,16 +65,18 @@ public static void writeV1(RainbowFish rainbowFish, String filename) throws IOEx
* 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);
}
}
Expand All @@ -83,8 +87,8 @@ public static void writeV2(RainbowFishV2 rainbowFish, String filename) throws IO
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();
}

Expand Down
4 changes: 2 additions & 2 deletions twin/src/main/java/com/iluwatar/twin/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static void main(String[] args) {
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);

Expand Down

0 comments on commit fadad43

Please sign in to comment.