81 lines
2.8 KiB
Java
Raw Normal View History

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.HashMap;
import java.util.Map;
2015-04-29 22:15:36 +03:00
/**
*
2015-08-21 23:25:22 +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.
2015-04-29 22:15:36 +03:00
* This way the reader does not break even though new properties are added to the schema.
*
*/
2015-04-28 23:59:30 +03:00
public class RainbowFishSerializer {
2015-04-29 22:15:36 +03:00
/**
* Write V1 RainbowFish to file
* @param rainbowFish
* @param filename
* @throws IOException
*/
2015-04-29 21:58:17 +03:00
public static void writeV1(RainbowFish rainbowFish, String filename) throws IOException {
2015-04-28 23:59:30 +03:00
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()));
2015-04-29 18:50:08 +03:00
FileOutputStream fileOut = new FileOutputStream(filename);
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
objOut.writeObject(map);
objOut.close();
fileOut.close();
2015-04-28 23:59:30 +03:00
}
2015-04-29 21:58:17 +03:00
2015-04-29 22:15:36 +03:00
/**
* Write V2 RainbowFish to file
* @param rainbowFish
* @param filename
* @throws IOException
*/
2015-04-29 21:58:17 +03:00
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()));
FileOutputStream fileOut = new FileOutputStream(filename);
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
objOut.writeObject(map);
objOut.close();
fileOut.close();
}
2015-04-28 23:59:30 +03:00
2015-04-29 22:15:36 +03:00
/**
* Read V1 RainbowFish from file
* @param filename
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
2015-04-29 21:58:17 +03:00
public static RainbowFish readV1(String filename) throws IOException, ClassNotFoundException {
2015-04-29 18:50:08 +03:00
Map<String, String> map = null;
FileInputStream fileIn = new FileInputStream(filename);
ObjectInputStream objIn = new ObjectInputStream(fileIn);
map = (Map<String, String>) objIn.readObject();
objIn.close();
fileIn.close();
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
}