1 package org.opentrafficsim.core.perception.collections;
2
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.List;
6 import java.util.ListIterator;
7
8 import org.opentrafficsim.core.perception.HistoryManager;
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 public abstract class AbstractHistoricalList<E, L extends List<E>> extends AbstractHistoricalCollection<E, L>
28 implements HistoricalList<E>
29 {
30
31
32
33
34
35
36 protected AbstractHistoricalList(final HistoryManager historyManager, final L list)
37 {
38 super(historyManager, list);
39 }
40
41
42
43
44 @Override
45 public synchronized void add(final int index, final E value)
46 {
47 addEvent(new AddEvent<>(now().si, value, index));
48 getCollection().add(index, value);
49 }
50
51
52 @Override
53 public synchronized boolean add(final E value)
54 {
55 addEvent(new AddEvent<>(now().si, value, getCollection().size()));
56 return getCollection().add(value);
57 }
58
59
60 @Override
61 public synchronized E remove(final int index)
62 {
63 addEvent(new RemoveEvent<>(now().si, getCollection().get(index), index));
64 return getCollection().remove(index);
65 }
66
67
68 @Override
69 @SuppressWarnings("unchecked")
70 public synchronized boolean remove(final Object value)
71 {
72 int index = getCollection().indexOf(value);
73 if (index >= 0)
74 {
75 addEvent(new RemoveEvent<>(now().si, (E) value, index));
76 getCollection().remove(index);
77 return true;
78 }
79 return false;
80 }
81
82
83 @Override
84 public synchronized E set(final int index, final E value)
85 {
86 E previousValue = getCollection().get(index);
87 if (!getCollection().get(index).equals(value))
88 {
89 remove(index);
90 add(index, value);
91 }
92 return previousValue;
93 }
94
95
96 @Override
97 public boolean addAll(final int index, final Collection<? extends E> c)
98 {
99 int ind = index;
100 for (E e : c)
101 {
102 add(ind, e);
103 ind++;
104 }
105 return !c.isEmpty();
106 }
107
108
109
110
111 @Override
112 public E get(final int index)
113 {
114 return getCollection().get(index);
115 }
116
117
118 @Override
119 public int indexOf(final Object o)
120 {
121 return getCollection().indexOf(o);
122 }
123
124
125 @Override
126 public int lastIndexOf(final Object o)
127 {
128 return getCollection().lastIndexOf(o);
129 }
130
131
132 @Override
133 public ListIterator<E> listIterator()
134 {
135 return listIterator(0);
136 }
137
138
139 @Override
140 public ListIterator<E> listIterator(final int index)
141 {
142 return Collections.unmodifiableList(getCollection()).listIterator(index);
143 }
144
145
146 @Override
147 public List<E> subList(final int fromIndex, final int toIndex)
148 {
149 return Collections.unmodifiableList(getCollection().subList(fromIndex, toIndex));
150 }
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168 public abstract static class EventList<E, L extends List<E>> extends EventCollection<E, L>
169 {
170
171
172 private final int index;
173
174
175
176
177
178
179
180 public EventList(final double time, final E value, final int index)
181 {
182 super(time, value);
183 this.index = index;
184 }
185
186
187
188
189
190 final int getIndex()
191 {
192 return this.index;
193 }
194
195 }
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 public static class AddEvent<E, L extends List<E>> extends EventList<E, L>
212 {
213
214
215
216
217
218
219
220 public AddEvent(final double time, final E value, final int index)
221 {
222 super(time, value, index);
223 }
224
225
226 @Override
227 public void restore(final L list)
228 {
229 list.remove(getIndex());
230 }
231
232
233 @Override
234 public String toString()
235 {
236 return "AddEvent [time=" + getTime() + ", value=" + getValue() + ", index=" + getIndex() + "]";
237 }
238
239 }
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255 public static class RemoveEvent<E, L extends List<E>> extends EventList<E, L>
256 {
257
258
259
260
261
262
263
264 public RemoveEvent(final double time, final E value, final int index)
265 {
266 super(time, value, index);
267 }
268
269
270 @Override
271 public void restore(final L list)
272 {
273 list.add(getIndex(), getValue());
274 }
275
276
277 @Override
278 public String toString()
279 {
280 return "RemoveEvent [time=" + getTime() + ", value=" + getValue() + ", index=" + getIndex() + "]";
281 }
282
283 }
284
285 }