-
Notifications
You must be signed in to change notification settings - Fork 587
/
AbstractMemoryEfficientMutableList.java
278 lines (238 loc) · 8.47 KB
/
AbstractMemoryEfficientMutableList.java
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Copyright (c) 2016 Goldman Sachs.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.impl.list.fixed;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.ListIterator;
import java.util.RandomAccess;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.list.FixedSizeList;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.impl.block.factory.Comparators;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.list.mutable.AbstractMutableList;
import org.eclipse.collections.impl.utility.Iterate;
public abstract class AbstractMemoryEfficientMutableList<T>
extends AbstractMutableList<T>
implements FixedSizeList<T>, RandomAccess
{
@Override
public FixedSizeList<T> clone()
{
return (FixedSizeList<T>) super.clone();
}
@Override
public FixedSizeList<T> tap(Procedure<? super T> procedure)
{
this.each(procedure);
return this;
}
@Override
public boolean add(T o)
{
throw new UnsupportedOperationException("Cannot add to a fixed size list: " + this.getClass());
}
@Override
public void add(int index, T element)
{
throw new UnsupportedOperationException("Cannot add to a fixed size list: " + this.getClass());
}
@Override
public boolean addAll(Collection<? extends T> collection)
{
throw new UnsupportedOperationException("Cannot add to a fixed size list: " + this.getClass());
}
@Override
public boolean addAll(int index, Collection<? extends T> collection)
{
throw new UnsupportedOperationException("Cannot add to a fixed size list: " + this.getClass());
}
@Override
public boolean addAllIterable(Iterable<? extends T> iterable)
{
throw new UnsupportedOperationException("Cannot add to a fixed size list: " + this.getClass());
}
@Override
public boolean remove(Object o)
{
throw new UnsupportedOperationException("Cannot remove from a fixed size list: " + this.getClass());
}
@Override
public T remove(int index)
{
throw new UnsupportedOperationException("Cannot remove from a fixed size list: " + this.getClass());
}
@Override
public boolean removeAll(Collection<?> collection)
{
throw new UnsupportedOperationException("Cannot remove from a fixed size list: " + this.getClass());
}
@Override
public boolean removeAllIterable(Iterable<?> iterable)
{
throw new UnsupportedOperationException("Cannot remove from a fixed size list: " + this.getClass());
}
@Override
public boolean removeIf(Predicate<? super T> predicate)
{
throw new UnsupportedOperationException("Cannot remove from a fixed size list: " + this.getClass());
}
@Override
public <P> boolean removeIfWith(Predicate2<? super T, ? super P> predicate, P parameter)
{
throw new UnsupportedOperationException("Cannot removeIfWith from a fixed size list: " + this.getClass());
}
@Override
public boolean retainAll(Collection<?> collection)
{
throw new UnsupportedOperationException("Cannot remove from a fixed size list: " + this.getClass());
}
@Override
public boolean retainAllIterable(Iterable<?> iterable)
{
throw new UnsupportedOperationException("Cannot remove from a fixed size list: " + this.getClass());
}
@Override
public void clear()
{
throw new UnsupportedOperationException("Cannot clear a fixed size list: " + this.getClass());
}
/**
* This method checks if comparator is null and use a ComparableComparator if it is.
*
* @since 10.0
*/
@Override
public void sort(Comparator<? super T> comparator)
{
this.insertionSort(Comparators.comparableComparatorIfNull(comparator));
}
private void insertionSort(Comparator<? super T> comparator)
{
for (int i = 0; i < this.size(); i++)
{
for (int j = i; this.isPreviousGreaterThanCurrent(comparator, j); j--)
{
Collections.swap(this, j - 1, j);
}
}
}
private boolean isPreviousGreaterThanCurrent(Comparator<? super T> comparator, int index)
{
return index > 0 && comparator.compare(this.get(index - 1), this.get(index)) > 0;
}
@Override
public FixedSizeList<T> toReversed()
{
FixedSizeList<T> result = Lists.fixedSize.withAll(this);
result.reverseThis();
return result;
}
@Override
public MutableList<T> subList(int fromIndex, int toIndex)
{
return new SubList<>(this, fromIndex, toIndex);
}
@Override
public MutableList<T> without(T element)
{
if (this.contains(element))
{
return Lists.fixedSize.ofAll(this.toList().without(element));
}
return this;
}
@Override
public MutableList<T> withAll(Iterable<? extends T> elements)
{
if (Iterate.isEmpty(elements))
{
return this;
}
return Lists.fixedSize.ofAll(this.toList().withAll(elements));
}
@Override
public MutableList<T> withoutAll(Iterable<? extends T> elements)
{
if (Iterate.isEmpty(elements))
{
return this;
}
return Lists.fixedSize.ofAll(this.toList().withoutAll(elements));
}
private static class SubList<T>
extends AbstractMutableList.SubList<T>
{
// Not important since it uses writeReplace()
private static final long serialVersionUID = 1L;
protected SubList(AbstractMutableList<T> list, int fromIndex, int toIndex)
{
super(list, fromIndex, toIndex);
}
@Override
public boolean remove(Object o)
{
throw new UnsupportedOperationException("Cannot remove from a fixed size list: " + this.getClass());
}
@Override
public boolean removeAll(Collection<?> collection)
{
throw new UnsupportedOperationException("Cannot remove from a fixed size list: " + this.getClass());
}
@Override
public boolean removeAllIterable(Iterable<?> iterable)
{
throw new UnsupportedOperationException("Cannot remove from a fixed size list: " + this.getClass());
}
@Override
public boolean retainAll(Collection<?> collection)
{
throw new UnsupportedOperationException("Cannot remove from a fixed size list: " + this.getClass());
}
@Override
public boolean retainAllIterable(Iterable<?> iterable)
{
throw new UnsupportedOperationException("Cannot remove from a fixed size list: " + this.getClass());
}
@Override
public boolean removeIf(Predicate<? super T> predicate)
{
throw new UnsupportedOperationException("Cannot remove from a fixed size list: " + this.getClass());
}
@Override
public <P> boolean removeIfWith(Predicate2<? super T, ? super P> predicate, P parameter)
{
throw new UnsupportedOperationException("Cannot removeIfWith from a fixed size list: " + this.getClass());
}
@Override
public boolean addAll(Collection<? extends T> collection)
{
throw new UnsupportedOperationException("Cannot add to a fixed size list: " + this.getClass());
}
@Override
public boolean addAllIterable(Iterable<? extends T> iterable)
{
throw new UnsupportedOperationException("Cannot add to a fixed size list: " + this.getClass());
}
}
@Override
public ListIterator<T> listIterator(int index)
{
return new FixedSizeListIteratorAdapter<>(super.listIterator(index));
}
@Override
public ListIterator<T> listIterator()
{
return new FixedSizeListIteratorAdapter<>(super.listIterator());
}
}