2019-10-22 07:15:35 +02:00
|
|
|
/*
|
2016-01-27 22:20:42 +00:00
|
|
|
* The MIT License
|
2019-10-12 20:05:54 +03:00
|
|
|
* Copyright © 2014-2019 Ilkka Seppälä
|
2016-01-27 22:20:42 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2019-10-22 07:15:35 +02:00
|
|
|
|
2015-05-31 11:55:18 +03:00
|
|
|
package com.iluwatar.tolerantreader;
|
2015-04-28 23:59:30 +03:00
|
|
|
|
2015-04-29 18:50:08 +03:00
|
|
|
import java.io.FileInputStream;
|
2015-04-28 23:59:30 +03:00
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
2015-04-29 18:50:08 +03:00
|
|
|
import java.io.ObjectInputStream;
|
2015-04-28 23:59:30 +03:00
|
|
|
import java.io.ObjectOutputStream;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2015-04-29 22:15:36 +03:00
|
|
|
/**
|
2015-11-01 21:29:13 -05:00
|
|
|
* RainbowFishSerializer provides methods for reading and writing {@link RainbowFish} objects to
|
2019-11-10 23:15:17 +05:30
|
|
|
* 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.
|
2015-04-29 22:15:36 +03:00
|
|
|
*/
|
2016-02-09 17:19:31 +02:00
|
|
|
public final class RainbowFishSerializer {
|
2015-04-28 23:59:30 +03:00
|
|
|
|
2015-12-23 10:03:11 +05:00
|
|
|
private RainbowFishSerializer() {
|
|
|
|
}
|
|
|
|
|
2015-11-01 21:29:13 -05:00
|
|
|
/**
|
2019-11-10 23:15:17 +05:30
|
|
|
* Write V1 RainbowFish to file.
|
2015-11-01 21:29:13 -05:00
|
|
|
*/
|
|
|
|
public static void writeV1(RainbowFish rainbowFish, String filename) throws IOException {
|
#984 for unitofwork, tolerantreader, twin (#1044)
* #984 for unitofwork, tolerantreader, twin
* #984 for unitofwork, tolerantreader, twin
* #987 for visitor, value-object, unitofwork, typeobjectpattern, tolerantreader, twin, tranpoline
2019-10-26 21:28:56 +05:30
|
|
|
var map = Map.of(
|
2019-11-10 23:15:17 +05:30
|
|
|
"name", rainbowFish.getName(),
|
|
|
|
"age", String.format("%d", rainbowFish.getAge()),
|
|
|
|
"lengthMeters", String.format("%d", rainbowFish.getLengthMeters()),
|
|
|
|
"weightTons", String.format("%d", rainbowFish.getWeightTons())
|
#984 for unitofwork, tolerantreader, twin (#1044)
* #984 for unitofwork, tolerantreader, twin
* #984 for unitofwork, tolerantreader, twin
* #987 for visitor, value-object, unitofwork, typeobjectpattern, tolerantreader, twin, tranpoline
2019-10-26 21:28:56 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
try (var fileOut = new FileOutputStream(filename);
|
2019-11-10 23:15:17 +05:30
|
|
|
var objOut = new ObjectOutputStream(fileOut)) {
|
2017-04-01 20:55:47 -06:00
|
|
|
objOut.writeObject(map);
|
|
|
|
}
|
2015-11-01 21:29:13 -05:00
|
|
|
}
|
2015-04-29 21:58:17 +03:00
|
|
|
|
2015-11-01 21:29:13 -05:00
|
|
|
/**
|
2019-11-10 23:15:17 +05:30
|
|
|
* Write V2 RainbowFish to file.
|
2015-11-01 21:29:13 -05:00
|
|
|
*/
|
|
|
|
public static void writeV2(RainbowFishV2 rainbowFish, String filename) throws IOException {
|
#984 for unitofwork, tolerantreader, twin (#1044)
* #984 for unitofwork, tolerantreader, twin
* #984 for unitofwork, tolerantreader, twin
* #987 for visitor, value-object, unitofwork, typeobjectpattern, tolerantreader, twin, tranpoline
2019-10-26 21:28:56 +05:30
|
|
|
var map = Map.of(
|
2019-11-10 23:15:17 +05:30
|
|
|
"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())
|
#984 for unitofwork, tolerantreader, twin (#1044)
* #984 for unitofwork, tolerantreader, twin
* #984 for unitofwork, tolerantreader, twin
* #987 for visitor, value-object, unitofwork, typeobjectpattern, tolerantreader, twin, tranpoline
2019-10-26 21:28:56 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
try (var fileOut = new FileOutputStream(filename);
|
2019-11-10 23:15:17 +05:30
|
|
|
var objOut = new ObjectOutputStream(fileOut)) {
|
2017-04-01 20:55:47 -06:00
|
|
|
objOut.writeObject(map);
|
|
|
|
}
|
2015-11-01 21:29:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-10 23:15:17 +05:30
|
|
|
* Read V1 RainbowFish from file.
|
2015-11-01 21:29:13 -05:00
|
|
|
*/
|
|
|
|
public static RainbowFish readV1(String filename) throws IOException, ClassNotFoundException {
|
2019-11-14 11:12:05 +05:30
|
|
|
Map<String, String> map;
|
2017-04-01 20:55:47 -06:00
|
|
|
|
#984 for unitofwork, tolerantreader, twin (#1044)
* #984 for unitofwork, tolerantreader, twin
* #984 for unitofwork, tolerantreader, twin
* #987 for visitor, value-object, unitofwork, typeobjectpattern, tolerantreader, twin, tranpoline
2019-10-26 21:28:56 +05:30
|
|
|
try (var fileIn = new FileInputStream(filename);
|
2019-11-10 23:15:17 +05:30
|
|
|
var objIn = new ObjectInputStream(fileIn)) {
|
2017-04-01 20:55:47 -06:00
|
|
|
map = (Map<String, String>) objIn.readObject();
|
|
|
|
}
|
|
|
|
|
2019-11-14 11:12:05 +05:30
|
|
|
return new RainbowFish(
|
|
|
|
map.get("name"),
|
|
|
|
Integer.parseInt(map.get("age")),
|
|
|
|
Integer.parseInt(map.get("lengthMeters")),
|
|
|
|
Integer.parseInt(map.get("weightTons"))
|
|
|
|
);
|
2015-11-01 21:29:13 -05:00
|
|
|
}
|
2015-04-28 23:59:30 +03:00
|
|
|
}
|