Repository navigation
Expand file tree
/
Copy pathSquareList.java
More file actions
164 lines (137 loc) · 4.13 KB
/
Copy pathSquareList.java
File metadata and controls
164 lines (137 loc) · 4.13 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Comparator;
/**
* A list of squares within a single window.
*
* <p>
* Adapterion of Nifty Assignment (http://nifty.stanford.edu/) by Mike Clancy in
* 2001. Original code by Mike Clancy. Updated Fall 2022 by K. Raven
* Russell.</p>
*/
public class SquareList {
/**
* initializes list.
*/
private ThreeTenLinkedList<Square> list;
/**
* Initialize an empty list of squares.
*/
public SquareList() {
list = new ThreeTenLinkedList<>();
}
/**
* get the value of the list's head.
*
* @return Head of list
*/
public Node<Square> getHead() {
return list.getHead();
}
/**
* get the value of the list's tail.
*
* @return tail of list
*/
public Node<Square> getTail() {
return list.getTail(); //dummy return, replace this!
}
/**
* Get the # of squares in the list.
*
* @return the list size
*/
public int numSquares() {
return list.getSize();
}
/**
* adds a square to the list.
*
* @param sq square to be added
*/
public void add(Square sq) {
if(sq == null){
throw new IllegalArgumentException("Invalid Square");
}
else{
list.insert(sq);
}
}
/**
* Deletes squares within an X and Y position.
*
* @param x position
* @param y position
* @return whether the squares were deleted or not
*/
public boolean handleClick(int x, int y) {
Node<Square> curr = list.getHead();
boolean deleted = false;
while(curr != null){
if(curr.data.contains(x, y)){
list.delete(curr.data);
deleted = true;
}
curr = curr.next;
}
return deleted;
}
/**
* Gets an iterator for the list of squares. Squares are returned in the
* order added.
*
* @return the iterator requested
*/
public Iterator<Square> elements() {
return new Iterator<Square>() {
/**
* The current node pointed to by the iterator (containing the next
* value to be returned).
*/
private Node<Square> current = getHead();
/**
* {@inheritDoc}
*/
@Override
public Square next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
Square ret = current.data;
current = current.next;
return ret;
}
/**
* {@inheritDoc}
*/
@Override
public boolean hasNext() {
return (current != null);
}
};
}
/**
* Sorts all the squares in the window by the time they were created.
*/
public void sortCreation() {
Comparator<Square> comp = new Comparator<Square>() {
public int compare(Square s1, Square s2) {
return (s1.id() - s2.id());
}
};
ThreeTenLinkedList.NodePair<Square> pair = new ThreeTenLinkedList.NodePair<>(getHead(), getTail());
ThreeTenLinkedList.NodePair<Square> ret = ThreeTenLinkedList.sort(pair, comp);
}
/**
* Method that sorts the squares by location.
*/
public void sortLoc() {
Comparator<Square> comp = new Comparator<Square>() {
public int compare(Square s1, Square s2) {
return (s1.getUpperLeftX() * s1.getUpperLeftY()) - (s2.getUpperLeftX() * s2.getUpperLeftY());
}
};
ThreeTenLinkedList.NodePair<Square> pair = new ThreeTenLinkedList.NodePair<>(getHead(), getTail());
ThreeTenLinkedList.NodePair<Square> ret = ThreeTenLinkedList.sort(pair, comp);
}
}