/
ListExpression.java
403 lines (352 loc) · 13 KB
/
ListExpression.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
* Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javafx.beans.binding;
import com.sun.javafx.binding.StringFormatter;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.ReadOnlyIntegerProperty;
import javafx.beans.value.ObservableIntegerValue;
import javafx.beans.value.ObservableListValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
/**
* {@code ListExpression} is an
* {@link javafx.beans.value.ObservableListValue} plus additional convenience
* methods to generate bindings in a fluent style.
* <p>
* A concrete sub-class of {@code ListExpression} has to implement the method
* {@link javafx.beans.value.ObservableListValue#get()}, which provides the
* actual value of this expression.
* <p>
* If the wrapped list of a {@code ListExpression} is {@code null}, all methods implementing the {@code List}
* interface will behave as if they were applied to an immutable empty list.
*
* @param <E> the type of the {@code List} elements.
* @since JavaFX 2.1
*/
public abstract class ListExpression<E> implements ObservableListValue<E> {
private static final ObservableList EMPTY_LIST = FXCollections.emptyObservableList();
/**
* Creates a default {@code ListExpression}.
*/
public ListExpression() {
}
@Override
public ObservableList<E> getValue() {
return get();
}
/**
* Returns a {@code ListExpression} that wraps a
* {@link javafx.beans.value.ObservableListValue}. If the
* {@code ObservableListValue} is already a {@code ListExpression}, it
* will be returned. Otherwise a new
* {@link javafx.beans.binding.ListBinding} is created that is bound to
* the {@code ObservableListValue}.
*
* @param <E> the type of the wrapped {@code List}
* @param value
* The source {@code ObservableListValue}
* @return A {@code ListExpression} that wraps the
* {@code ObservableListValue} if necessary
* @throws NullPointerException
* if {@code value} is {@code null}
*/
public static <E> ListExpression<E> listExpression(final ObservableListValue<E> value) {
if (value == null) {
throw new NullPointerException("List must be specified.");
}
return value instanceof ListExpression ? (ListExpression<E>) value
: new ListBinding<E>() {
{
super.bind(value);
}
@Override
public void dispose() {
super.unbind(value);
}
@Override
protected ObservableList<E> computeValue() {
return value.get();
}
@Override
public ObservableList<ObservableListValue<E>> getDependencies() {
return FXCollections.singletonObservableList(value);
}
};
}
/**
* The size of the list
* @return the size
*/
public int getSize() {
return size();
}
/**
* An integer property that represents the size of the list.
* @return the property
*/
public abstract ReadOnlyIntegerProperty sizeProperty();
/**
* A boolean property that is {@code true}, if the list is empty.
* @return the {@code ReadOnlyBooleanProperty}
*
*/
public abstract ReadOnlyBooleanProperty emptyProperty();
/**
* Creates a new {@link ObjectBinding} that contains the element at the specified position.
* If {@code index} points behind the list, the {@code ObjectBinding} contains {@code null}.
*
* @param index the index of the element
* @return the {@code ObjectBinding}
* @throws IllegalArgumentException if {@code index < 0}
*/
public ObjectBinding<E> valueAt(int index) {
return Bindings.valueAt(this, index);
}
/**
* Creates a new {@link ObjectBinding} that contains the element at the specified position.
* If {@code index} points outside of the list, the {@code ObjectBinding} contains {@code null}.
*
* @param index the index of the element
* @return the {@code ObjectBinding}
* @throws NullPointerException if {@code index} is {@code null}
*/
public ObjectBinding<E> valueAt(ObservableIntegerValue index) {
return Bindings.valueAt(this, index);
}
/**
* Creates a new {@link BooleanBinding} that holds {@code true} if this list is equal to
* another {@link javafx.collections.ObservableList}.
*
* @param other
* the other {@code ObservableList}
* @return the new {@code BooleanBinding}
* @throws NullPointerException
* if {@code other} is {@code null}
*/
public BooleanBinding isEqualTo(final ObservableList<?> other) {
return Bindings.equal(this, other);
}
/**
* Creates a new {@link BooleanBinding} that holds {@code true} if this list is not equal to
* another {@link javafx.collections.ObservableList}.
*
* @param other
* the other {@code ObservableList}
* @return the new {@code BooleanBinding}
* @throws NullPointerException
* if {@code other} is {@code null}
*/
public BooleanBinding isNotEqualTo(final ObservableList<?> other) {
return Bindings.notEqual(this, other);
}
/**
* Creates a new {@link BooleanBinding} that holds {@code true} if the wrapped list is {@code null}.
*
* @return the new {@code BooleanBinding}
*/
public BooleanBinding isNull() {
return Bindings.isNull(this);
}
/**
* Creates a new {@link BooleanBinding} that holds {@code true} if the wrapped list is not {@code null}.
*
* @return the new {@code BooleanBinding}
*/
public BooleanBinding isNotNull() {
return Bindings.isNotNull(this);
}
/**
* Creates a {@link javafx.beans.binding.StringBinding} that holds the value
* of the {@code ListExpression} turned into a {@code String}. If the
* value of this {@code ListExpression} changes, the value of the
* {@code StringBinding} will be updated automatically.
*
* @return the new {@code StringBinding}
*/
public StringBinding asString() {
return (StringBinding) StringFormatter.convert(this);
}
@Override
public int size() {
final ObservableList<E> list = get();
return (list == null)? EMPTY_LIST.size() : list.size();
}
@Override
public boolean isEmpty() {
final ObservableList<E> list = get();
return (list == null)? EMPTY_LIST.isEmpty() : list.isEmpty();
}
@Override
public boolean contains(Object obj) {
final ObservableList<E> list = get();
return (list == null)? EMPTY_LIST.contains(obj) : list.contains(obj);
}
@Override
public Iterator<E> iterator() {
final ObservableList<E> list = get();
return (list == null)? EMPTY_LIST.iterator() : list.iterator();
}
@Override
public Object[] toArray() {
final ObservableList<E> list = get();
return (list == null)? EMPTY_LIST.toArray() : list.toArray();
}
@Override
public <T> T[] toArray(T[] array) {
final ObservableList<E> list = get();
return (list == null)? (T[]) EMPTY_LIST.toArray(array) : list.toArray(array);
}
@Override
public boolean add(E element) {
final ObservableList<E> list = get();
return (list == null)? EMPTY_LIST.add(element) : list.add(element);
}
@Override
public boolean remove(Object obj) {
final ObservableList<E> list = get();
return (list == null)? EMPTY_LIST.remove(obj) : list.remove(obj);
}
@Override
public boolean containsAll(Collection<?> objects) {
final ObservableList<E> list = get();
return (list == null)? EMPTY_LIST.contains(objects) : list.containsAll(objects);
}
@Override
public boolean addAll(Collection<? extends E> elements) {
final ObservableList<E> list = get();
return (list == null)? EMPTY_LIST.addAll(elements) : list.addAll(elements);
}
@Override
public boolean addAll(int i, Collection<? extends E> elements) {
final ObservableList<E> list = get();
return (list == null)? EMPTY_LIST.addAll(i, elements) : list.addAll(i, elements);
}
@Override
public boolean removeAll(Collection<?> objects) {
final ObservableList<E> list = get();
return (list == null)? EMPTY_LIST.removeAll(objects) : list.removeAll(objects);
}
@Override
public boolean retainAll(Collection<?> objects) {
final ObservableList<E> list = get();
return (list == null)? EMPTY_LIST.retainAll(objects) : list.retainAll(objects);
}
@Override
public void clear() {
final ObservableList<E> list = get();
if (list == null) {
EMPTY_LIST.clear();
} else {
list.clear();
}
}
@Override
public E get(int i) {
final ObservableList<E> list = get();
return (list == null)? (E) EMPTY_LIST.get(i) : list.get(i);
}
@Override
public E set(int i, E element) {
final ObservableList<E> list = get();
return (list == null)? (E) EMPTY_LIST.set(i, element) : list.set(i, element);
}
@Override
public void add(int i, E element) {
final ObservableList<E> list = get();
if (list == null) {
EMPTY_LIST.add(i, element);
} else {
list.add(i, element);
}
}
@Override
public E remove(int i) {
final ObservableList<E> list = get();
return (list == null)? (E) EMPTY_LIST.remove(i) : list.remove(i);
}
@Override
public int indexOf(Object obj) {
final ObservableList<E> list = get();
return (list == null)? EMPTY_LIST.indexOf(obj) : list.indexOf(obj);
}
@Override
public int lastIndexOf(Object obj) {
final ObservableList<E> list = get();
return (list == null)? EMPTY_LIST.lastIndexOf(obj) : list.lastIndexOf(obj);
}
@Override
public ListIterator<E> listIterator() {
final ObservableList<E> list = get();
return (list == null)? EMPTY_LIST.listIterator() : list.listIterator();
}
@Override
public ListIterator<E> listIterator(int i) {
final ObservableList<E> list = get();
return (list == null)? EMPTY_LIST.listIterator(i) : list.listIterator(i);
}
@Override
public List<E> subList(int from, int to) {
final ObservableList<E> list = get();
return (list == null)? EMPTY_LIST.subList(from, to) : list.subList(from, to);
}
@Override
public boolean addAll(E... elements) {
final ObservableList<E> list = get();
return (list == null)? EMPTY_LIST.addAll(elements) : list.addAll(elements);
}
@Override
public boolean setAll(E... elements) {
final ObservableList<E> list = get();
return (list == null)? EMPTY_LIST.setAll(elements) : list.setAll(elements);
}
@Override
public boolean setAll(Collection<? extends E> elements) {
final ObservableList<E> list = get();
return (list == null)? EMPTY_LIST.setAll(elements) : list.setAll(elements);
}
@Override
public boolean removeAll(E... elements) {
final ObservableList<E> list = get();
return (list == null)? EMPTY_LIST.removeAll(elements) : list.removeAll(elements);
}
@Override
public boolean retainAll(E... elements) {
final ObservableList<E> list = get();
return (list == null)? EMPTY_LIST.retainAll(elements) : list.retainAll(elements);
}
@Override
public void remove(int from, int to) {
final ObservableList<E> list = get();
if (list == null) {
EMPTY_LIST.remove(from, to);
} else {
list.remove(from, to);
}
}
}