1 package org.opentrafficsim.core.dsol;
2
3 import java.io.Serializable;
4 import java.util.Collection;
5 import java.util.Comparator;
6 import java.util.Iterator;
7 import java.util.NoSuchElementException;
8 import java.util.SortedSet;
9 import java.util.TreeSet;
10
11 import nl.tudelft.simulation.dsol.eventlists.EventListInterface;
12 import nl.tudelft.simulation.dsol.formalisms.eventscheduling.SimEventInterface;
13 import nl.tudelft.simulation.dsol.simtime.SimTime;
14
15
16
17
18
19
20
21
22
23
24
25
26 public class SynchronizedRedBlackTree<T extends SimTime<?, ?, T>> implements EventListInterface<T>, Serializable
27 {
28
29 private static final long serialVersionUID = 1L;
30
31
32 private TreeSet<SimEventInterface<T>> tree = new TreeSet<>();
33
34
35
36
37 public SynchronizedRedBlackTree()
38 {
39 super();
40 }
41
42
43 @Override
44 @SuppressWarnings("checkstyle:designforextension")
45 public SimEventInterface<T> removeFirst()
46 {
47 synchronized (this.tree)
48 {
49 SimEventInterface<T> first = this.first();
50 this.remove(first);
51 return first;
52 }
53 }
54
55
56 @Override
57 @SuppressWarnings("checkstyle:designforextension")
58 public SimEventInterface<T> removeLast()
59 {
60 synchronized (this.tree)
61 {
62 SimEventInterface<T> last = this.last();
63 this.remove(last);
64 return last;
65 }
66 }
67
68
69
70
71
72
73 @Override
74 @SuppressWarnings("checkstyle:designforextension")
75 public SimEventInterface<T> first()
76 {
77 synchronized (this.tree)
78 {
79 try
80 {
81 return this.tree.first();
82 }
83 catch (NoSuchElementException noSuchElementException)
84 {
85 return null;
86 }
87 }
88 }
89
90
91
92
93
94
95 @Override
96 @SuppressWarnings("checkstyle:designforextension")
97 public SimEventInterface<T> last()
98 {
99 synchronized (this.tree)
100 {
101 try
102 {
103 return this.tree.last();
104 }
105 catch (NoSuchElementException noSuchElementException)
106 {
107 return null;
108 }
109 }
110 }
111
112
113 @Override
114 public Comparator<? super SimEventInterface<T>> comparator()
115 {
116 synchronized (this.tree)
117 {
118 return this.tree.comparator();
119 }
120 }
121
122
123 @Override
124 public SortedSet<SimEventInterface<T>> subSet(final SimEventInterface<T> fromElement, final SimEventInterface<T> toElement)
125 {
126 synchronized (this.tree)
127 {
128 return this.tree.subSet(fromElement, toElement);
129 }
130 }
131
132
133 @Override
134 public SortedSet<SimEventInterface<T>> headSet(final SimEventInterface<T> toElement)
135 {
136 synchronized (this.tree)
137 {
138 return this.tree.headSet(toElement);
139 }
140 }
141
142
143 @Override
144 public SortedSet<SimEventInterface<T>> tailSet(final SimEventInterface<T> fromElement)
145 {
146 synchronized (this.tree)
147 {
148 return this.tree.tailSet(fromElement);
149 }
150 }
151
152
153 @Override
154 public int size()
155 {
156 synchronized (this.tree)
157 {
158 return this.tree.size();
159 }
160 }
161
162
163 @Override
164 public boolean isEmpty()
165 {
166 synchronized (this.tree)
167 {
168 return this.tree.isEmpty();
169 }
170 }
171
172
173 @Override
174 public boolean contains(final Object o)
175 {
176 synchronized (this.tree)
177 {
178 return this.tree.contains(o);
179 }
180 }
181
182
183 @Override
184 public Iterator<SimEventInterface<T>> iterator()
185 {
186 synchronized (this.tree)
187 {
188 return this.tree.iterator();
189 }
190 }
191
192
193 @Override
194 public Object[] toArray()
195 {
196 synchronized (this.tree)
197 {
198 return this.tree.toArray();
199 }
200 }
201
202
203 @Override
204 public <X> X[] toArray(final X[] a)
205 {
206 synchronized (this.tree)
207 {
208 return this.tree.toArray(a);
209 }
210 }
211
212
213 @Override
214 public boolean add(final SimEventInterface<T> e)
215 {
216 synchronized (this.tree)
217 {
218 return this.tree.add(e);
219 }
220 }
221
222
223 @Override
224 public boolean remove(final Object o)
225 {
226 synchronized (this.tree)
227 {
228 return this.tree.remove(o);
229 }
230 }
231
232
233 @Override
234 public boolean containsAll(final Collection<?> c)
235 {
236 synchronized (this.tree)
237 {
238 return this.tree.containsAll(c);
239 }
240 }
241
242
243 @Override
244 public boolean addAll(final Collection<? extends SimEventInterface<T>> c)
245 {
246 synchronized (this.tree)
247 {
248 return this.tree.addAll(c);
249 }
250 }
251
252
253 @Override
254 public boolean retainAll(final Collection<?> c)
255 {
256 synchronized (this.tree)
257 {
258 return this.tree.retainAll(c);
259 }
260 }
261
262
263 @Override
264 public boolean removeAll(final Collection<?> c)
265 {
266 synchronized (this.tree)
267 {
268 return this.tree.removeAll(c);
269 }
270 }
271
272
273 @Override
274 public void clear()
275 {
276 this.tree.clear();
277 }
278
279
280 @Override
281 public String toString()
282 {
283 return "SynchronizedRedBlackTree [" + this.tree.size() + " events]";
284 }
285
286 }