-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShapeCollection.java
More file actions
143 lines (117 loc) · 3.63 KB
/
Copy pathShapeCollection.java
File metadata and controls
143 lines (117 loc) · 3.63 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
133
134
135
136
137
138
139
140
141
142
143
////////////////////////////////////////////////////////////
// class: ShapeCollection
//
// description: a class for storing an ordered collection
// of Shapes. Supports the operations of appending and
// removing shapes from the collection. Sequential access to
// the shapes stored in the collection is given through
// the getFirst() and getNext() methods.
////////////////////////////////////////////////////////////
public class ShapeCollection {
private ShapeNode first;
private ShapeNode next;
private ShapeNode last;
public ShapeCollection () {
first = null;
next = null;
last = null;
}
////////////////////////////////////////////////////////////
// method: Shape getFirst()
//
// description: returns the first Shape stored in the
// collection, and resets the "next" Shape to point to the
// following (i.e., second) Shape in the collection (if any)
////////////////////////////////////////////////////////////
public Shape getFirst () {
next = first;
return getNext();
}
////////////////////////////////////////////////////////////
// method: Shape getNext()
//
// description: returns the Shape currently pointed to by
// Next and updates Next to point to the following Shape
// (if any). In the case where next is null, getNext()
// returns null.
////////////////////////////////////////////////////////////
public Shape getNext () {
if (next == null)
return null;
Shape s = next.getShape();
next = next.getNext();
return s;
}
////////////////////////////////////////////////////////////
// method: void append (Shape s)
//
// description: Appends a Shape s to the end of the
// collection. After this operation, s will be the last
// shape stored in the collection.
////////////////////////////////////////////////////////////
public void append (Shape s) {
ShapeNode node = new ShapeNode(s);
if (last == null) {
first = node;
next = node;
last = node;
} else {
node.setPrev(last);
last.setNext(node);
last = node;
}
}
////////////////////////////////////////////////////////////
// method: void remove (Shape s)
//
// description: Searches the collection for a node
// containing s, and if found, s is removed from the
// collection. This method has no effect if s is not found
// the the collection.
////////////////////////////////////////////////////////////
public void remove (Shape s) {
if (first == null)
return;
ShapeNode cur = first;
while (cur != null && cur.getShape() != s)
cur = cur.getNext();
if (cur == null)
return;
if (cur.getNext() != null) {
cur.getNext().setPrev(cur.getPrev());
} else {
// cur is last
last = cur.getPrev();
}
if (cur.getPrev() != null) {
cur.getPrev().setNext(cur.getNext());
} else {
// cur is the first
first = cur.getNext();
}
if (next == cur) {
next = cur.getNext();
}
}
}
////////////////////////////////////////////////////////////
// class: ShapeNode
//
// description: a class for storing a Shape. Used by
// ShapeCollection.
////////////////////////////////////////////////////////////
class ShapeNode {
private Shape shape;
private ShapeNode next;
private ShapeNode prev;
public ShapeNode (Shape shape) {
this.shape = shape;
next = null;
prev = null;
}
public Shape getShape() { return shape; }
public ShapeNode getNext() { return next; }
public ShapeNode getPrev() { return prev; }
public void setNext (ShapeNode next) { this.next = next; }
public void setPrev (ShapeNode prev) { this.prev = prev; }
}