OnJava8-Examples/serialization/AStoreCADState.java

105 lines
3.0 KiB
Java
Raw Permalink Normal View History

2015-11-03 12:00:44 -08:00
// serialization/AStoreCADState.java
// (c)2021 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-01-25 18:05:55 -08:00
// Saving the state of a fictitious CAD system
2015-06-15 17:47:35 -07:00
import java.io.*;
import java.util.*;
2017-01-22 16:48:11 -08:00
import java.util.stream.*;
enum Color { RED, BLUE, GREEN }
2015-06-15 17:47:35 -07:00
abstract class Shape implements Serializable {
private int xPos, yPos, dimension;
2017-05-01 14:33:10 -06:00
Shape(int xVal, int yVal, int dim) {
2015-06-15 17:47:35 -07:00
xPos = xVal;
yPos = yVal;
dimension = dim;
}
public abstract void setColor(Color newColor);
public abstract Color getColor();
@Override public String toString() {
return "\n" + getClass() + " " + getColor() +
" xPos[" + xPos + "] yPos[" + yPos +
"] dim[" + dimension + "]";
2015-06-15 17:47:35 -07:00
}
private static Random rand = new Random(47);
private static int counter = 0;
2015-06-15 17:47:35 -07:00
public static Shape randomFactory() {
int xVal = rand.nextInt(100);
int yVal = rand.nextInt(100);
int dim = rand.nextInt(100);
switch(counter++ % 2) {
2015-06-15 17:47:35 -07:00
default:
case 0: return new Circle(xVal, yVal, dim);
case 1: return new Line(xVal, yVal, dim);
2015-06-15 17:47:35 -07:00
}
}
}
class Circle extends Shape {
2017-05-01 14:33:10 -06:00
Circle(int xVal, int yVal, int dim) {
2015-06-15 17:47:35 -07:00
super(xVal, yVal, dim);
}
private static Color color = Color.RED;
@Override public void setColor(Color newColor) {
2017-01-22 16:48:11 -08:00
color = newColor;
}
@Override public Color getColor() { return color; }
2015-06-15 17:47:35 -07:00
}
class Line extends Shape {
Line(int xVal, int yVal, int dim) {
2015-06-15 17:47:35 -07:00
super(xVal, yVal, dim);
}
private static Color color = Color.RED;
@Override public void setColor(Color newColor) {
2017-01-22 16:48:11 -08:00
color = newColor;
}
@Override public Color getColor() { return color; }
2015-06-15 17:47:35 -07:00
public static void
serializeStaticState(ObjectOutputStream os)
throws IOException {
os.writeObject(color);
}
2015-06-15 17:47:35 -07:00
public static void
deserializeStaticState(ObjectInputStream os)
2017-01-22 16:48:11 -08:00
throws IOException, ClassNotFoundException {
color = (Color)os.readObject();
}
2015-06-15 17:47:35 -07:00
}
public class AStoreCADState {
2017-01-22 16:48:11 -08:00
public static void main(String[] args) {
2015-06-15 17:47:35 -07:00
List<Class<? extends Shape>> shapeTypes =
Arrays.asList(Circle.class, Line.class);
List<Shape> shapes = IntStream.range(0, 5)
2017-01-22 16:48:11 -08:00
.mapToObj(i -> Shape.randomFactory())
.collect(Collectors.toList());
2015-06-15 17:47:35 -07:00
// Set all the static colors to GREEN:
2017-01-22 16:48:11 -08:00
shapes.forEach(s -> s.setColor(Color.GREEN));
// Serialize everything to CADState.dat:
try(
ObjectOutputStream out =
new ObjectOutputStream(
new FileOutputStream("CADState.dat"))
) {
2016-01-25 18:05:55 -08:00
out.writeObject(shapeTypes);
Line.serializeStaticState(out);
out.writeObject(shapes);
2017-01-22 16:48:11 -08:00
} catch(IOException e) {
throw new RuntimeException(e);
2016-01-25 18:05:55 -08:00
}
2015-06-15 17:47:35 -07:00
// Display the shapes:
System.out.println(shapes);
}
2015-09-07 11:44:36 -06:00
}
/* Output:
[
class Circle GREEN xPos[58] yPos[55] dim[93],
class Line GREEN xPos[61] yPos[61] dim[29],
class Circle GREEN xPos[68] yPos[0] dim[22],
class Line GREEN xPos[7] yPos[88] dim[28],
class Circle GREEN xPos[51] yPos[89] dim[9]]
2015-09-07 11:44:36 -06:00
*/