001 /*
002 * SPDX-License-Identifier: Apache-2.0
003 *
004 * Copyright 2008-2017 the original author or authors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package griffon.javafx.collections;
019
020 import javafx.collections.ObservableSet;
021 import javafx.collections.SetChangeListener;
022 import javafx.collections.WeakSetChangeListener;
023
024 import javax.annotation.Nonnull;
025 import java.util.Collection;
026 import java.util.Iterator;
027
028 import static java.util.Objects.requireNonNull;
029
030 /**
031 * @author Andres Almiray
032 * @since 2.9.0
033 */
034 public abstract class DelegatingObservableSet<E> extends ObservableSetBase<E> implements ObservableSet<E> {
035 private final ObservableSet<E> delegate;
036 private SetChangeListener<E> sourceListener;
037
038 public DelegatingObservableSet(@Nonnull ObservableSet<E> delegate) {
039 this.delegate = requireNonNull(delegate, "Argument 'delegate' must not be null");
040 this.delegate.addListener(new WeakSetChangeListener<>(getListener()));
041 }
042
043 @Nonnull
044 protected ObservableSet<E> getDelegate() {
045 return delegate;
046 }
047
048 private SetChangeListener<E> getListener() {
049 if (sourceListener == null) {
050 sourceListener = DelegatingObservableSet.this::sourceChanged;
051 }
052 return sourceListener;
053 }
054
055 protected abstract void sourceChanged(@Nonnull SetChangeListener.Change<? extends E> c);
056
057 // --== Delegate methods ==--
058
059 @Override
060 public int size() {
061 return getDelegate().size();
062 }
063
064 @Override
065 public boolean isEmpty() {
066 return getDelegate().isEmpty();
067 }
068
069 @Override
070 public boolean contains(Object o) {
071 return getDelegate().contains(o);
072 }
073
074 @Override
075 public Iterator<E> iterator() {
076 return getDelegate().iterator();
077 }
078
079 @Override
080 public Object[] toArray() {
081 return getDelegate().toArray();
082 }
083
084 @Override
085 public <T> T[] toArray(T[] a) {
086 return getDelegate().toArray(a);
087 }
088
089 @Override
090 public boolean add(E e) {
091 return getDelegate().add(e);
092 }
093
094 @Override
095 public boolean remove(Object o) {
096 return getDelegate().remove(o);
097 }
098
099 @Override
100 public boolean containsAll(Collection<?> c) {
101 return getDelegate().containsAll(c);
102 }
103
104 @Override
105 public boolean addAll(Collection<? extends E> c) {
106 return getDelegate().addAll(c);
107 }
108
109 @Override
110 public boolean retainAll(Collection<?> c) {
111 return getDelegate().retainAll(c);
112 }
113
114 @Override
115 public boolean removeAll(Collection<?> c) {
116 return getDelegate().removeAll(c);
117 }
118
119 @Override
120 public void clear() {
121 getDelegate().clear();
122 }
123
124 @Override
125 public boolean equals(Object o) {
126 return getDelegate().equals(o);
127 }
128
129 @Override
130 public int hashCode() {
131 return getDelegate().hashCode();
132 }
133 }
|