1 | public class PaintMap implements Cloneable, Serializable {↵ | | 1 | public class StrokeMap implements Cloneable, Serializable {↵
|
|
2 | /** For serialization. */↵ | | 2 | /** For serialization. */↵
|
3 | static final long serialVersionUID = -4639833772123069274L;↵ | | 3 | static final long serialVersionUID = -8148916785963525169L;↵
|
|
4 | /** Storage for the keys and values. */↵ | | 4 | /** Storage for the keys and values. */↵
|
5 | private transient Map store;↵ | | 5 | private transient Map store;↵
|
6 | ↵ | | 6 | ↵
|
7 | /**↵ | | 7 | /**↵
|
8 | * Creates a new (empty) map.↵ | | 8 | * Creates a new (empty) map.↵
|
9 | */↵ | | 9 | */↵
|
10 | public PaintMap() {↵ | | 10 | public StrokeMap() {↵
|
11 | this.store = new HashMap(); ↵ | | 11 | this.store = new TreeMap(); ↵
|
12 | }↵ | | 12 | }↵
|
13 | ↵ | | 13 | ↵
|
14 | /**↵ | | 14 | /**↵
|
15 | * Returns the paint associated with the specified key, or ↵ | | 15 | * Returns the stroke associated with the specified key, or ↵
|
16 | * <code>null</code>.↵ | | 16 | * <code>null</code>.↵
|
17 | * ↵ | | 17 | * ↵
|
18 | * @param key the key (<code>null</code> not permitted).↵ | | 18 | * @param key the key (<code>null</code> not permitted).↵
|
19 | * ↵ | | 19 | * ↵
|
20 | * @return The paint, or <code>null</code>.↵ | | 20 | * @return The stroke, or <code>null</code>.↵
|
21 | * ↵ | | 21 | * ↵
|
22 | * @throws IllegalArgumentException if <code>key</code> is ↵ | | 22 | * @throws IllegalArgumentException if <code>key</code> is ↵
|
23 | * <code>null</code>.↵ | | 23 | * <code>null</code>.↵
|
24 | */↵ | | 24 | */↵
|
25 | public Paint getPaint(Comparable key) {↵ | | 25 | public Stroke getStroke(Comparable key) {↵
|
26 | if (key == null) {↵ | | 26 | if (key == null) {↵
|
27 | throw new IllegalArgumentException("Null 'key' argument.");↵ | | 27 | throw new IllegalArgumentException("Null 'key' argument.");↵
|
28 | }↵ | | 28 | }↵
|
29 | return (Paint) this.store.get(key);↵ | | 29 | return (Stroke) this.store.get(key);↵
|
30 | }↵ | | 30 | }↵
|
31 | ↵ | | 31 | ↵
|
32 | /**↵ | | 32 | /**↵
|
33 | * Returns <code>true</code> if the map contains the specified key, and↵ | | 33 | * Returns <code>true</code> if the map contains the specified key, and↵
|
34 | * <code>false</code> otherwise.↵ | | 34 | * <code>false</code> otherwise.↵
|
35 | * ↵ | | 35 | * ↵
|
36 | * @param key the key.↵ | | 36 | * @param key the key.↵
|
37 | * ↵ | | 37 | * ↵
|
38 | * @return <code>true</code> if the map contains the specified key, and↵ | | 38 | * @return <code>true</code> if the map contains the specified key, and↵
|
39 | * <code>false</code> otherwise.↵ | | 39 | * <code>false</code> otherwise.↵
|
40 | */↵ | | 40 | */↵
|
41 | public boolean containsKey(Comparable key) {↵ | | 41 | public boolean containsKey(Comparable key) {↵
|
42 | return this.store.containsKey(key);↵ | | 42 | return this.store.containsKey(key);↵
|
43 | }↵ | | 43 | }↵
|
44 | ↵ | | 44 | ↵
|
45 | /**↵ | | 45 | /**↵
|
46 | * Adds a mapping between the specified <code>key</code> and ↵ | | 46 | * Adds a mapping between the specified <code>key</code> and ↵
|
47 | * <code>paint</code> values.↵ | | 47 | * <code>stroke</code> values.↵
|
48 | * ↵ | | 48 | * ↵
|
49 | * @param key the key (<code>null</code> not permitted).↵ | | 49 | * @param key the key (<code>null</code> not permitted).↵
|
50 | * @param paint the paint.↵ | | 50 | * @param ↵
|
51 | * ↵ | | |
|
52 | * @throws IllegalArgumentException if <code>key</code> is ↵ | | |
|
53 | * <code>null</code>.↵ | | 51 | stroke the stroke.↵
|
54 | */↵ | | 52 | */↵
|
55 | public void put(Comparable key, Paint paint) {↵ | | 53 | public void put(Comparable key, Stroke stroke) {↵
|
56 | if (key == null) {↵ | | 54 | if (key == null) { ↵
|
57 | throw new IllegalArgumentException("Null 'key' argument.");↵ | | 55 | throw new IllegalArgumentException("Null 'key' argument.");↵
|
58 | }↵ | | 56 | }↵
|
59 | this.store.put(key, paint);↵ | | 57 | this.store.put(key, stroke);↵
|
60 | }↵ | | 58 | }↵
|
61 | ↵ | | 59 | ↵
|
62 | /**↵ | | 60 | /**↵
|
63 | * Resets the map to empty.↵ | | 61 | * Resets the map to empty.↵
|
64 | */↵ | | 62 | */↵
|
65 | public void clear() {↵ | | 63 | public void clear() {↵
|
66 | this.store.clear();↵ | | 64 | this.store.clear();↵
|
67 | }↵ | | 65 | }↵
|
68 | ↵ | | 66 | ↵
|
69 | /**↵ | | 67 | /**↵
|
70 | * Tests this map for equality with an arbitrary object.↵ | | 68 | * Tests this map for equality with an arbitrary object.↵
|
71 | * ↵ | | 69 | * ↵
|
72 | * @param obj the object (<code>null</code> permitted).↵ | | 70 | * @param obj the object (<code>null</code> permitted).↵
|
73 | * ↵ | | 71 | * ↵
|
74 | * @return A boolean.↵ | | 72 | * @return A boolean.↵
|
75 | */↵ | | 73 | */↵
|
76 | public boolean equals(Object obj) {↵ | | 74 | public boolean equals(Object obj) {↵
|
77 | if (obj == this) {↵ | | 75 | if (obj == this) {↵
|
78 | return true;↵ | | 76 | return true;↵
|
79 | }↵ | | 77 | }↵
|
80 | if (!(obj instanceof PaintMap)) {↵ | | 78 | if (!(obj instanceof StrokeMap)) {↵
|
81 | return false;↵ | | 79 | return false;↵
|
82 | }↵ | | 80 | }↵
|
83 | PaintMap that = (PaintMap) obj;↵ | | 81 | StrokeMap that = (StrokeMap) obj;↵
|
84 | if (this.store.size() != that.store.size()) {↵ | | 82 | if (this.store.size() != that.store.size()) {↵
|
85 | return false;↵ | | 83 | return false;↵
|
86 | }↵ | | 84 | }↵
|
87 | Set keys = this.store.keySet();↵ | | 85 | Set keys = this.store.keySet();↵
|
88 | Iterator iterator = keys.iterator();↵ | | 86 | Iterator iterator = keys.iterator();↵
|
89 | while (iterator.hasNext()) {↵ | | 87 | while (iterator.hasNext()) {↵
|
90 | Comparable key = (Comparable) iterator.next();↵ | | 88 | Comparable key = (Comparable) iterator.next();↵
|
91 | Paint p1 = getPaint(key);↵ | | 89 | Stroke s1 = getStroke(key);↵
|
92 | Paint p2 = that.getPaint(key);↵ | | 90 | Stroke s2 = that.getStroke(key);↵
|
93 | if (!PaintUtilities.equal(p1, p2)) {↵ | | 91 | if (!ObjectUtilities.equal(s1, s2)) {↵
|
94 | return false;↵ | | 92 | return false;↵
|
95 | }↵ | | 93 | }↵
|
96 | }↵ | | 94 | }↵
|
97 | return true;↵ | | 95 | return true;↵
|
98 | }↵ | | 96 | }↵
|
99 | ↵ | | 97 | ↵
|
100 | /**↵ | | 98 | /**↵
|
101 | * Returns a clone of this <code>PaintMap</code>.↵ | | 99 | * Returns a clone of this <code>StrokeMap</code>.↵
|
102 | * ↵ | | 100 | * ↵
|
103 | * @return A clone of this instance.↵ | | 101 | * @return A clone of this instance.↵
|
104 | * ↵ | | 102 | * ↵
|
105 | * @throws CloneNotSupportedException if any key is not cloneable.↵ | | 103 | * @throws CloneNotSupportedException if any key is not cloneable.↵
|
106 | */↵ | | 104 | */↵
|
107 | public Object clone() throws CloneNotSupportedException {↵ | | 105 | public Object clone() throws CloneNotSupportedException {↵
|
108 | // TODO: I think we need to make sure the keys are actually cloned,↵ | | 106 | // TODO: I think we need to make sure the keys are actually cloned,↵
|
109 | // whereas the paint instances are always immutable so they're OK↵ | | 107 | // whereas the stroke instances are always immutable so they're OK↵
|
110 | return super.clone();↵ | | 108 | return super.clone();↵
|
111 | }↵ | | 109 | }↵
|
112 | ↵ | | 110 | ↵
|
113 | /**↵ | | 111 | /**↵
|
114 | * Provides serialization support.↵ | | 112 | * Provides serialization support.↵
|
115 | *↵ | | 113 | *↵
|
116 | * @param stream the output stream.↵ | | 114 | * @param stream the output stream.↵
|
117 | *↵ | | 115 | *↵
|
118 | * @throws IOException if there is an I/O error.↵ | | 116 | * @throws IOException if there is an I/O error.↵
|
119 | */↵ | | 117 | */↵
|
120 | private void writeObject(ObjectOutputStream stream) throws IOException {↵ | | 118 | private void writeObject(ObjectOutputStream stream) throws IOException {↵
|
121 | stream.defaultWriteObject();↵ | | 119 | stream.defaultWriteObject();↵
|
122 | stream.writeInt(this.store.size());↵ | | 120 | stream.writeInt(this.store.size());↵
|
123 | Set keys = this.store.keySet();↵ | | 121 | Set keys = this.store.keySet();↵
|
124 | Iterator iterator = keys.iterator();↵ | | 122 | Iterator iterator = keys.iterator();↵
|
125 | while (iterator.hasNext()) {↵ | | 123 | while (iterator.hasNext()) {↵
|
126 | Comparable key = (Comparable) iterator.next();↵ | | 124 | Comparable key = (Comparable) iterator.next();↵
|
127 | stream.writeObject(key);↵ | | 125 | stream.writeObject(key);↵
|
128 | Paint paint = getPaint(key);↵ | | 126 | Stroke stroke = getStroke(key);↵
|
129 | SerialUtilities.writePaint(paint, stream);↵ | | 127 | SerialUtilities.writeStroke(stroke, stream);↵
|
130 | }↵ | | 128 | }↵
|
131 | }↵ | | 129 | }↵
|
|
132 | /**↵ | | 130 | /**↵
|
133 | * Provides serialization support.↵ | | 131 | * Provides serialization support.↵
|
134 | *↵ | | 132 | *↵
|
135 | * @param stream the input stream.↵ | | 133 | * @param stream the input stream.↵
|
136 | *↵ | | 134 | *↵
|
137 | * @throws IOException if there is an I/O error.↵ | | 135 | * @throws IOException if there is an I/O error.↵
|
138 | * @throws ClassNotFoundException if there is a classpath problem.↵ | | 136 | * @throws ClassNotFoundException if there is a classpath problem.↵
|
139 | */↵ | | 137 | */↵
|
140 | private void readObject(ObjectInputStream stream) ↵ | | 138 | private void readObject(ObjectInputStream stream) ↵
|
141 | throws IOException, ClassNotFoundException {↵ | | 139 | throws IOException, ClassNotFoundException {↵
|
142 | stream.defaultReadObject();↵ | | 140 | stream.defaultReadObject();↵
|
143 | this.store = new HashMap();↵ | | 141 | this.store = new TreeMap();↵
|
144 | int keyCount = stream.readInt();↵ | | 142 | int keyCount = stream.readInt();↵
|
145 | for (int i = 0; i < keyCount; i++) {↵ | | 143 | for (int i = 0; i < keyCount; i++) {↵
|
146 | Comparable key = (Comparable) stream.readObject();↵ | | 144 | Comparable key = (Comparable) stream.readObject();↵
|
147 | Paint paint = SerialUtilities.readPaint(stream);↵ | | 145 | Stroke stroke = SerialUtilities.readStroke(stream);↵
|
148 | this.store.put(key, paint) | | 146 | this.store.put(key, stroke)
|