OnJava8-Examples/serialization/AStoreCADState.java

121 lines
3.5 KiB
Java
Raw Normal View History

2015-11-03 12:00:44 -08:00
// serialization/AStoreCADState.java
2016-12-30 17:23:13 -08:00
// (c)2017 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-01-22 16:48:11 -08:00
private static Random rand = new Random(47);
2015-06-15 17:47:35 -07:00
private static int counter = 0;
2017-01-22 16:48:11 -08:00
public abstract void setColor(Color newColor);
public abstract Color getColor();
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 String toString() {
2017-01-22 16:48:11 -08:00
return getClass() + "color[" + getColor() +
"] xPos[" + xPos + "] yPos[" + yPos +
"] dim[" + dimension + "]\n";
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++ % 3) {
default:
case 0: return new Circle(xVal, yVal, dim);
case 1: return new Square(xVal, yVal, dim);
case 2: return new Line(xVal, yVal, dim);
}
}
}
class Circle extends Shape {
2017-01-22 16:48:11 -08:00
private static Color color = Color.RED;
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);
}
2017-01-22 16:48:11 -08:00
public void setColor(Color newColor) {
color = newColor;
}
public Color getColor() { return color; }
2015-06-15 17:47:35 -07:00
}
class Square extends Shape {
2017-01-22 16:48:11 -08:00
private static Color color = Color.RED;
2017-05-01 14:33:10 -06:00
Square(int xVal, int yVal, int dim) {
2015-06-15 17:47:35 -07:00
super(xVal, yVal, dim);
}
2017-01-22 16:48:11 -08:00
public void setColor(Color newColor) {
color = newColor;
}
public Color getColor() { return color; }
2015-06-15 17:47:35 -07:00
}
class Line extends Shape {
2017-01-22 16:48:11 -08:00
private static Color color = Color.RED;
2015-06-15 17:47:35 -07:00
public static void
serializeStaticState(ObjectOutputStream os)
2017-01-22 16:48:11 -08:00
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();
}
2017-05-01 14:33:10 -06:00
Line(int xVal, int yVal, int dim) {
2015-06-15 17:47:35 -07:00
super(xVal, yVal, dim);
}
2017-01-22 16:48:11 -08:00
public void setColor(Color newColor) {
color = newColor;
}
public Color getColor() { return color; }
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 =
2017-01-22 16:48:11 -08:00
Arrays.asList(
Circle.class, Square.class, Line.class);
List<Shape> shapes = IntStream.range(0, 10)
.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));
2015-06-15 17:47:35 -07:00
// Save the state vector:
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:
2017-01-22 16:48:11 -08:00
[class Circlecolor[GREEN] xPos[58] yPos[55] dim[93]
, class Squarecolor[GREEN] xPos[61] yPos[61] dim[29]
, class Linecolor[GREEN] xPos[68] yPos[0] dim[22]
, class Circlecolor[GREEN] xPos[7] yPos[88] dim[28]
, class Squarecolor[GREEN] xPos[51] yPos[89] dim[9]
, class Linecolor[GREEN] xPos[78] yPos[98] dim[61]
, class Circlecolor[GREEN] xPos[20] yPos[58] dim[16]
, class Squarecolor[GREEN] xPos[40] yPos[11] dim[22]
, class Linecolor[GREEN] xPos[4] yPos[83] dim[6]
, class Circlecolor[GREEN] xPos[75] yPos[10] dim[42]
2015-06-15 17:47:35 -07:00
]
2015-09-07 11:44:36 -06:00
*/