forked from BruceEckel/OnJava8-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAStoreCADState.java
More file actions
132 lines (113 loc) · 3.46 KB
/
AStoreCADState.java
File metadata and controls
132 lines (113 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// serialization/AStoreCADState.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Saving the state of a fictitious CAD system
import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
enum Color {RED, BLUE, GREEN}
abstract class Shape implements Serializable {
private static Random rand = new Random(47);
private static int counter = 0;
private int xPos, yPos, dimension;
Shape(int xVal, int yVal, int dim) {
xPos = xVal;
yPos = yVal;
dimension = dim;
}
public static Shape randomFactory() {
int xVal = rand.nextInt(100);
int yVal = rand.nextInt(100);
int dim = rand.nextInt(100);
switch (counter++ % 2) {
default:
case 0:
return new Circle(xVal, yVal, dim);
case 1:
return new Line(xVal, yVal, dim);
}
}
public abstract Color getColor();
public abstract void setColor(Color newColor);
@Override
public String toString() {
return "\n" + getClass() + " " + getColor() +
" xPos[" + xPos + "] yPos[" + yPos +
"] dim[" + dimension + "]";
}
}
class Circle extends Shape {
private static Color color = Color.RED;
Circle(int xVal, int yVal, int dim) {
super(xVal, yVal, dim);
}
@Override
public Color getColor() {
return color;
}
@Override
public void setColor(Color newColor) {
color = newColor;
}
}
class Line extends Shape {
private static Color color = Color.RED;
Line(int xVal, int yVal, int dim) {
super(xVal, yVal, dim);
}
public static void
serializeStaticState(ObjectOutputStream os)
throws IOException {
os.writeObject(color);
}
public static void
deserializeStaticState(ObjectInputStream os)
throws IOException, ClassNotFoundException {
color = (Color) os.readObject();
}
@Override
public Color getColor() {
return color;
}
@Override
public void setColor(Color newColor) {
color = newColor;
}
}
public class AStoreCADState {
public static void main(String[] args) {
List<Class<? extends Shape>> shapeTypes =
Arrays.asList(Circle.class, Line.class);
List<Shape> shapes = IntStream.range(0, 5)
.mapToObj(i -> Shape.randomFactory())
.collect(Collectors.toList());
// Set all the static colors to GREEN:
shapes.forEach(s -> s.setColor(Color.GREEN));
// Serialize everything to CADState.dat:
try (
ObjectOutputStream out =
new ObjectOutputStream(
new FileOutputStream("CADState.dat"))
) {
out.writeObject(shapeTypes);
Line.serializeStaticState(out);
out.writeObject(shapes);
} catch (IOException e) {
throw new RuntimeException(e);
}
// Display the shapes:
System.out.println(shapes);
}
}
/* 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]]
*/