DelegatingObservableList.java
001 /*
002  * Copyright 2008-2016 the original author or authors.
003  *
004  * Licensed under the Apache License, Version 2.0 (the "License");
005  * you may not use this file except in compliance with the License.
006  * You may obtain a copy of the License at
007  *
008  *     http://www.apache.org/licenses/LICENSE-2.0
009  *
010  * Unless required by applicable law or agreed to in writing, software
011  * distributed under the License is distributed on an "AS IS" BASIS,
012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  * See the License for the specific language governing permissions and
014  * limitations under the License.
015  */
016 package griffon.javafx.support;
017 
018 import javafx.collections.ListChangeListener;
019 import javafx.collections.ObservableList;
020 import javafx.collections.ObservableListBase;
021 import javafx.collections.WeakListChangeListener;
022 import javafx.collections.transformation.FilteredList;
023 import javafx.collections.transformation.SortedList;
024 
025 import javax.annotation.Nonnull;
026 import java.util.Collection;
027 import java.util.Comparator;
028 import java.util.Iterator;
029 import java.util.List;
030 import java.util.ListIterator;
031 import java.util.Spliterator;
032 import java.util.function.Consumer;
033 import java.util.function.Predicate;
034 import java.util.function.UnaryOperator;
035 import java.util.stream.Stream;
036 
037 import static java.util.Objects.requireNonNull;
038 
039 /**
040  @author Andres Almiray
041  @since 2.6.0
042  */
043 public abstract class DelegatingObservableList<E> extends ObservableListBase<E> implements ObservableList<E> {
044     private final ObservableList<E> delegate;
045     private ListChangeListener<E> sourceListener;
046 
047     public DelegatingObservableList(@Nonnull ObservableList<E> delegate) {
048         this.delegate = requireNonNull(delegate, "Argument 'delegate' must not be null");
049         this.delegate.addListener(new WeakListChangeListener<>(getListener()));
050     }
051 
052     @Nonnull
053     protected ObservableList<E> getDelegate() {
054         return delegate;
055     }
056 
057     private ListChangeListener<E> getListener() {
058         if (sourceListener == null) {
059             sourceListener = DelegatingObservableList.this::sourceChanged;
060         }
061         return sourceListener;
062     }
063 
064     protected abstract void sourceChanged(@Nonnull ListChangeListener.Change<? extends E> c);
065 
066     // --== Delegate methods ==--
067 
068     public boolean removeAll(E... elements) {
069         return getDelegate().removeAll(elements);
070     }
071 
072     public boolean removeIf(Predicate<? super E> filter) {
073         return getDelegate().removeIf(filter);
074     }
075 
076     public void remove(int from, int to) {
077         getDelegate().remove(from, to);
078     }
079 
080     public E remove(int index) {
081         return getDelegate().remove(index);
082     }
083 
084     public int size() {
085         return getDelegate().size();
086     }
087 
088     public int lastIndexOf(Object o) {
089         return getDelegate().lastIndexOf(o);
090     }
091 
092     public boolean isEmpty() {
093         return getDelegate().isEmpty();
094     }
095 
096     public SortedList<E> sorted() {
097         return getDelegate().sorted();
098     }
099 
100     public Stream<E> parallelStream() {
101         return getDelegate().parallelStream();
102     }
103 
104     public boolean addAll(E... elements) {
105         return getDelegate().addAll(elements);
106     }
107 
108     public List<E> subList(int fromIndex, int toIndex) {
109         return getDelegate().subList(fromIndex, toIndex);
110     }
111 
112     public Spliterator<E> spliterator() {
113         return getDelegate().spliterator();
114     }
115 
116     public void sort(Comparator<? super E> c) {
117         getDelegate().sort(c);
118     }
119 
120     public E set(int index, E element) {
121         return getDelegate().set(index, element);
122     }
123 
124     public void add(int index, E element) {
125         getDelegate().add(index, element);
126     }
127 
128     public void replaceAll(UnaryOperator<E> operator) {
129         getDelegate().replaceAll(operator);
130     }
131 
132     public boolean containsAll(Collection<?> c) {
133         return getDelegate().containsAll(c);
134     }
135 
136     public void clear() {
137         getDelegate().clear();
138     }
139 
140     public Iterator<E> iterator() {
141         return getDelegate().iterator();
142     }
143 
144     public boolean removeAll(Collection<?> c) {
145         return getDelegate().removeAll(c);
146     }
147 
148     public <T> T[] toArray(T[] a) {
149         return getDelegate().toArray(a);
150     }
151 
152     public boolean remove(Object o) {
153         return getDelegate().remove(o);
154     }
155 
156     public boolean addAll(Collection<? extends E> c) {
157         return getDelegate().addAll(c);
158     }
159 
160     public boolean retainAll(E... elements) {
161         return getDelegate().retainAll(elements);
162     }
163 
164     public boolean retainAll(Collection<?> c) {
165         return getDelegate().retainAll(c);
166     }
167 
168     public SortedList<E> sorted(Comparator<E> comparator) {
169         return getDelegate().sorted(comparator);
170     }
171 
172     public FilteredList<E> filtered(Predicate<E> predicate) {
173         return getDelegate().filtered(predicate);
174     }
175 
176     public Stream<E> stream() {
177         return getDelegate().stream();
178     }
179 
180     public boolean contains(Object o) {
181         return getDelegate().contains(o);
182     }
183 
184     public boolean setAll(Collection<? extends E> col) {
185         return getDelegate().setAll(col);
186     }
187 
188     public ListIterator<E> listIterator(int index) {
189         return getDelegate().listIterator(index);
190     }
191 
192     public boolean add(E e) {
193         return getDelegate().add(e);
194     }
195 
196     public boolean addAll(int index, Collection<? extends E> c) {
197         return getDelegate().addAll(index, c);
198     }
199 
200     public Object[] toArray() {
201         return getDelegate().toArray();
202     }
203 
204     public ListIterator<E> listIterator() {
205         return getDelegate().listIterator();
206     }
207 
208     public void forEach(Consumer<? super E> action) {
209         getDelegate().forEach(action);
210     }
211 
212     public E get(int index) {
213         return getDelegate().get(index);
214     }
215 
216     public boolean setAll(E... elements) {
217         return getDelegate().setAll(elements);
218     }
219 
220     public int indexOf(Object o) {
221         return getDelegate().indexOf(o);
222     }
223 }