100 lines
3.5 KiB
Java
Raw Normal View History

/*
* The MIT License
2019-10-12 20:05:54 +03:00
* Copyright © 2014-2019 Ilkka Seppälä
*
* 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.
*/
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
/**
* RainbowFishSerializer provides methods for reading and writing {@link RainbowFish} objects to
* 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
*/
public final class RainbowFishSerializer {
2015-04-28 23:59:30 +03:00
private RainbowFishSerializer() {
}
/**
* 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()),
"lengthMeters", String.format("%d", rainbowFish.getLengthMeters()),
"weightTons", String.format("%d", rainbowFish.getWeightTons())
);
try (var fileOut = new FileOutputStream(filename);
var objOut = new ObjectOutputStream(fileOut)) {
2017-04-01 20:55:47 -06:00
objOut.writeObject(map);
}
}
2015-04-29 21:58:17 +03:00
/**
* 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()),
"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)) {
2017-04-01 20:55:47 -06:00
objOut.writeObject(map);
}
}
/**
* Read V1 RainbowFish from file.
*/
public static RainbowFish readV1(String filename) throws IOException, ClassNotFoundException {
Map<String, String> map;
2017-04-01 20:55:47 -06:00
try (var fileIn = new FileInputStream(filename);
var objIn = new ObjectInputStream(fileIn)) {
2017-04-01 20:55:47 -06:00
map = (Map<String, String>) objIn.readObject();
}
return new RainbowFish(
map.get("name"),
Integer.parseInt(map.get("age")),
Integer.parseInt(map.get("lengthMeters")),
Integer.parseInt(map.get("weightTons"))
);
}
2015-04-28 23:59:30 +03:00
}