mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-20 10:24:53 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -43,31 +43,44 @@ import lombok.extern.slf4j.Slf4j;
|
||||
@Slf4j
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point.
|
||||
*/
|
||||
/** Program entry point. */
|
||||
public static void main(String[] args) throws IOException, ClassNotFoundException {
|
||||
// Write V1
|
||||
var fishV1 = new RainbowFish("Zed", 10, 11, 12);
|
||||
LOGGER.info("fishV1 name={} age={} length={} weight={}", fishV1.getName(),
|
||||
fishV1.getAge(), fishV1.getLengthMeters(), fishV1.getWeightTons());
|
||||
LOGGER.info(
|
||||
"fishV1 name={} age={} length={} weight={}",
|
||||
fishV1.getName(),
|
||||
fishV1.getAge(),
|
||||
fishV1.getLengthMeters(),
|
||||
fishV1.getWeightTons());
|
||||
RainbowFishSerializer.writeV1(fishV1, "fish1.out");
|
||||
// Read V1
|
||||
var deserializedRainbowFishV1 = RainbowFishSerializer.readV1("fish1.out");
|
||||
LOGGER.info("deserializedFishV1 name={} age={} length={} weight={}",
|
||||
deserializedRainbowFishV1.getName(), deserializedRainbowFishV1.getAge(),
|
||||
deserializedRainbowFishV1.getLengthMeters(), deserializedRainbowFishV1.getWeightTons());
|
||||
LOGGER.info(
|
||||
"deserializedFishV1 name={} age={} length={} weight={}",
|
||||
deserializedRainbowFishV1.getName(),
|
||||
deserializedRainbowFishV1.getAge(),
|
||||
deserializedRainbowFishV1.getLengthMeters(),
|
||||
deserializedRainbowFishV1.getWeightTons());
|
||||
// Write V2
|
||||
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.isHungry(), fishV2.isAngry(), fishV2.isSleeping());
|
||||
fishV2.getName(),
|
||||
fishV2.getAge(),
|
||||
fishV2.getLengthMeters(),
|
||||
fishV2.getWeightTons(),
|
||||
fishV2.isHungry(),
|
||||
fishV2.isAngry(),
|
||||
fishV2.isSleeping());
|
||||
RainbowFishSerializer.writeV2(fishV2, "fish2.out");
|
||||
// Read V2 with V1 method
|
||||
var deserializedFishV2 = RainbowFishSerializer.readV1("fish2.out");
|
||||
LOGGER.info("deserializedFishV2 name={} age={} length={} weight={}",
|
||||
deserializedFishV2.getName(), deserializedFishV2.getAge(),
|
||||
deserializedFishV2.getLengthMeters(), deserializedFishV2.getWeightTons());
|
||||
LOGGER.info(
|
||||
"deserializedFishV2 name={} age={} length={} weight={}",
|
||||
deserializedFishV2.getName(),
|
||||
deserializedFishV2.getAge(),
|
||||
deserializedFishV2.getLengthMeters(),
|
||||
deserializedFishV2.getWeightTons());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,19 +29,15 @@ import java.io.Serializable;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* RainbowFish is the initial schema.
|
||||
*/
|
||||
/** RainbowFish is the initial schema. */
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public class RainbowFish implements Serializable {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
@Serial private static final long serialVersionUID = 1L;
|
||||
|
||||
private final String name;
|
||||
private final int age;
|
||||
private final int lengthMeters;
|
||||
private final int weightTons;
|
||||
|
||||
}
|
||||
|
||||
+33
-29
@@ -44,51 +44,56 @@ public final class RainbowFishSerializer {
|
||||
public static final String LENGTH_METERS = "lengthMeters";
|
||||
public static final String WEIGHT_TONS = "weightTons";
|
||||
|
||||
/**
|
||||
* Write V1 RainbowFish to file.
|
||||
*/
|
||||
/** Write V1 RainbowFish to file. */
|
||||
public static void writeV1(RainbowFish rainbowFish, String filename) throws IOException {
|
||||
var map = Map.of(
|
||||
"name", rainbowFish.getName(),
|
||||
"age", String.format("%d", rainbowFish.getAge()),
|
||||
LENGTH_METERS, String.format("%d", rainbowFish.getLengthMeters()),
|
||||
WEIGHT_TONS, String.format("%d", rainbowFish.getWeightTons())
|
||||
);
|
||||
var map =
|
||||
Map.of(
|
||||
"name",
|
||||
rainbowFish.getName(),
|
||||
"age",
|
||||
String.format("%d", rainbowFish.getAge()),
|
||||
LENGTH_METERS,
|
||||
String.format("%d", rainbowFish.getLengthMeters()),
|
||||
WEIGHT_TONS,
|
||||
String.format("%d", rainbowFish.getWeightTons()));
|
||||
|
||||
try (var fileOut = new FileOutputStream(filename);
|
||||
var objOut = new ObjectOutputStream(fileOut)) {
|
||||
var objOut = new ObjectOutputStream(fileOut)) {
|
||||
objOut.writeObject(map);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write V2 RainbowFish to file.
|
||||
*/
|
||||
/** Write V2 RainbowFish to file. */
|
||||
public static void writeV2(RainbowFishV2 rainbowFish, String filename) throws IOException {
|
||||
var map = Map.of(
|
||||
"name", rainbowFish.getName(),
|
||||
"age", String.format("%d", rainbowFish.getAge()),
|
||||
LENGTH_METERS, String.format("%d", rainbowFish.getLengthMeters()),
|
||||
WEIGHT_TONS, String.format("%d", rainbowFish.getWeightTons()),
|
||||
"angry", Boolean.toString(rainbowFish.isAngry()),
|
||||
"hungry", Boolean.toString(rainbowFish.isHungry()),
|
||||
"sleeping", Boolean.toString(rainbowFish.isSleeping())
|
||||
);
|
||||
var map =
|
||||
Map.of(
|
||||
"name",
|
||||
rainbowFish.getName(),
|
||||
"age",
|
||||
String.format("%d", rainbowFish.getAge()),
|
||||
LENGTH_METERS,
|
||||
String.format("%d", rainbowFish.getLengthMeters()),
|
||||
WEIGHT_TONS,
|
||||
String.format("%d", rainbowFish.getWeightTons()),
|
||||
"angry",
|
||||
Boolean.toString(rainbowFish.isAngry()),
|
||||
"hungry",
|
||||
Boolean.toString(rainbowFish.isHungry()),
|
||||
"sleeping",
|
||||
Boolean.toString(rainbowFish.isSleeping()));
|
||||
|
||||
try (var fileOut = new FileOutputStream(filename);
|
||||
var objOut = new ObjectOutputStream(fileOut)) {
|
||||
var objOut = new ObjectOutputStream(fileOut)) {
|
||||
objOut.writeObject(map);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read V1 RainbowFish from file.
|
||||
*/
|
||||
/** Read V1 RainbowFish from file. */
|
||||
public static RainbowFish readV1(String filename) throws IOException, ClassNotFoundException {
|
||||
Map<String, String> map;
|
||||
|
||||
try (var fileIn = new FileInputStream(filename);
|
||||
var objIn = new ObjectInputStream(fileIn)) {
|
||||
var objIn = new ObjectInputStream(fileIn)) {
|
||||
map = (Map<String, String>) objIn.readObject();
|
||||
}
|
||||
|
||||
@@ -96,7 +101,6 @@ public final class RainbowFishSerializer {
|
||||
map.get("name"),
|
||||
Integer.parseInt(map.get("age")),
|
||||
Integer.parseInt(map.get(LENGTH_METERS)),
|
||||
Integer.parseInt(map.get(WEIGHT_TONS))
|
||||
);
|
||||
Integer.parseInt(map.get(WEIGHT_TONS)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,14 +27,11 @@ package com.iluwatar.tolerantreader;
|
||||
import java.io.Serial;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* RainbowFishV2 is the evolved schema.
|
||||
*/
|
||||
/** RainbowFishV2 is the evolved schema. */
|
||||
@Getter
|
||||
public class RainbowFishV2 extends RainbowFish {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
@Serial private static final long serialVersionUID = 1L;
|
||||
|
||||
private boolean sleeping;
|
||||
private boolean hungry;
|
||||
@@ -44,15 +41,18 @@ public class RainbowFishV2 extends RainbowFish {
|
||||
super(name, age, lengthMeters, weightTons);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public RainbowFishV2(String name, int age, int lengthMeters, int weightTons, boolean sleeping,
|
||||
boolean hungry, boolean angry) {
|
||||
/** Constructor. */
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user