1 /* 2 * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javafx.beans.binding; 27 28 import com.sun.javafx.binding.StringFormatter; 29 import javafx.beans.InvalidationListener; 30 import javafx.beans.property.ReadOnlyBooleanProperty; 31 import javafx.beans.property.ReadOnlyIntegerProperty; 32 import javafx.beans.value.ObservableSetValue; 33 import javafx.collections.FXCollections; 34 import javafx.collections.ObservableList; 35 import javafx.collections.ObservableSet; 36 import javafx.collections.SetChangeListener; 37 38 import java.util.AbstractSet; 39 import java.util.Collection; 40 import java.util.Iterator; 41 import java.util.NoSuchElementException; 42 43 /** 44 * {@code SetExpression} is an 45 * {@link javafx.beans.value.ObservableSetValue} plus additional convenience 46 * methods to generate bindings in a fluent style. 47 * <p> 48 * A concrete sub-class of {@code SetExpression} has to implement the method 49 * {@link javafx.beans.value.ObservableSetValue#get()}, which provides the 50 * actual value of this expression. 51 * <p> 52 * If the wrapped list of a {@code SetExpression} is {@code null}, all methods implementing the {@code Set} 53 * interface will behave as if they were applied to an immutable empty set. 54 * 55 * @param <E> the type of the {@code Set} elements 56 * @since JavaFX 2.1 57 */ 58 public abstract class SetExpression<E> implements ObservableSetValue<E> { 59 60 private static final ObservableSet EMPTY_SET = new EmptyObservableSet(); 61 62 private static class EmptyObservableSet<E> extends AbstractSet<E> implements ObservableSet<E> { 63 64 private static final Iterator iterator = new Iterator() { 65 @Override 66 public boolean hasNext() { 67 return false; 68 } 69 70 @Override 71 public Object next() { 72 throw new NoSuchElementException(); 73 } 74 75 @Override 76 public void remove() { 77 throw new UnsupportedOperationException(); 78 79 } 80 }; 81 82 @Override 83 public Iterator<E> iterator() { 84 return iterator; 85 } 86 87 @Override 88 public int size() { 89 return 0; 90 } 91 92 @Override 93 public void addListener(SetChangeListener<? super E> setChangeListener) { 94 // no-op 95 } 96 97 @Override 98 public void removeListener(SetChangeListener<? super E> setChangeListener) { 99 // no-op 100 } 101 102 @Override 103 public void addListener(InvalidationListener listener) { 104 // no-op 105 } 106 107 @Override 108 public void removeListener(InvalidationListener listener) { 109 // no-op 110 } 111 } 112 113 @Override 114 public ObservableSet<E> getValue() { 115 return get(); 116 } 117 118 /** 119 * Returns a {@code SetExpression} that wraps a 120 * {@link javafx.beans.value.ObservableSetValue}. If the 121 * {@code ObservableSetValue} is already a {@code SetExpression}, it 122 * will be returned. Otherwise a new 123 * {@link javafx.beans.binding.SetBinding} is created that is bound to 124 * the {@code ObservableSetValue}. 125 * 126 * @param <E> the type of the {@code Set} elements 127 * @param value 128 * The source {@code ObservableSetValue} 129 * @return A {@code SetExpression} that wraps the 130 * {@code ObservableSetValue} if necessary 131 * @throws NullPointerException 132 * if {@code value} is {@code null} 133 */ 134 public static <E> SetExpression<E> setExpression(final ObservableSetValue<E> value) { 135 if (value == null) { 136 throw new NullPointerException("Set must be specified."); 137 } 138 return value instanceof SetExpression ? (SetExpression<E>) value 139 : new SetBinding<E>() { 140 { 141 super.bind(value); 142 } 143 144 @Override 145 public void dispose() { 146 super.unbind(value); 147 } 148 149 @Override 150 protected ObservableSet<E> computeValue() { 151 return value.get(); 152 } 153 154 @Override 155 public ObservableList<?> getDependencies() { 156 return FXCollections.singletonObservableList(value); 157 } 158 }; 159 } 160 161 /** 162 * The size of the set 163 * @return the size 164 */ 165 public int getSize() { 166 return size(); 167 } 168 169 /** 170 * An integer property that represents the size of the set. 171 * @return the property 172 */ 173 public abstract ReadOnlyIntegerProperty sizeProperty(); 174 175 /** 176 * A boolean property that is {@code true}, if the set is empty. 177 * @return the {@code ReadOnlyBooleanProperty} 178 */ 179 public abstract ReadOnlyBooleanProperty emptyProperty(); 180 181 /** 182 * Creates a new {@link BooleanBinding} that holds {@code true} if this set is equal to 183 * another {@link javafx.collections.ObservableSet}. 184 * 185 * @param other 186 * the other {@code ObservableSet} 187 * @return the new {@code BooleanBinding} 188 * @throws NullPointerException 189 * if {@code other} is {@code null} 190 */ 191 public BooleanBinding isEqualTo(final ObservableSet<?> other) { 192 return Bindings.equal(this, other); 193 } 194 195 /** 196 * Creates a new {@link BooleanBinding} that holds {@code true} if this set is not equal to 197 * another {@link javafx.collections.ObservableSet}. 198 * 199 * @param other 200 * the other {@code ObservableSet} 201 * @return the new {@code BooleanBinding} 202 * @throws NullPointerException 203 * if {@code other} is {@code null} 204 */ 205 public BooleanBinding isNotEqualTo(final ObservableSet<?> other) { 206 return Bindings.notEqual(this, other); 207 } 208 209 /** 210 * Creates a new {@link BooleanBinding} that holds {@code true} if the wrapped set is {@code null}. 211 * 212 * @return the new {@code BooleanBinding} 213 */ 214 public BooleanBinding isNull() { 215 return Bindings.isNull(this); 216 } 217 218 /** 219 * Creates a new {@link BooleanBinding} that holds {@code true} if the wrapped set is not {@code null}. 220 * 221 * @return the new {@code BooleanBinding} 222 */ 223 public BooleanBinding isNotNull() { 224 return Bindings.isNotNull(this); 225 } 226 227 /** 228 * Creates a {@link javafx.beans.binding.StringBinding} that holds the value 229 * of the {@code SetExpression} turned into a {@code String}. If the 230 * value of this {@code SetExpression} changes, the value of the 231 * {@code StringBinding} will be updated automatically. 232 * 233 * @return the new {@code StringBinding} 234 */ 235 public StringBinding asString() { 236 return (StringBinding) StringFormatter.convert(this); 237 } 238 239 @Override 240 public int size() { 241 final ObservableSet<E> set = get(); 242 return (set == null)? EMPTY_SET.size() : set.size(); 243 } 244 245 @Override 246 public boolean isEmpty() { 247 final ObservableSet<E> set = get(); 248 return (set == null)? EMPTY_SET.isEmpty() : set.isEmpty(); 249 } 250 251 @Override 252 public boolean contains(Object obj) { 253 final ObservableSet<E> set = get(); 254 return (set == null)? EMPTY_SET.contains(obj) : set.contains(obj); 255 } 256 257 @Override 258 public Iterator<E> iterator() { 259 final ObservableSet<E> set = get(); 260 return (set == null)? EMPTY_SET.iterator() : set.iterator(); 261 } 262 263 @Override 264 public Object[] toArray() { 265 final ObservableSet<E> set = get(); 266 return (set == null)? EMPTY_SET.toArray() : set.toArray(); 267 } 268 269 @Override 270 public <T> T[] toArray(T[] array) { 271 final ObservableSet<E> set = get(); 272 return (set == null)? (T[]) EMPTY_SET.toArray(array) : set.toArray(array); 273 } 274 275 @Override 276 public boolean add(E element) { 277 final ObservableSet<E> set = get(); 278 return (set == null)? EMPTY_SET.add(element) : set.add(element); 279 } 280 281 @Override 282 public boolean remove(Object obj) { 283 final ObservableSet<E> set = get(); 284 return (set == null)? EMPTY_SET.remove(obj) : set.remove(obj); 285 } 286 287 @Override 288 public boolean containsAll(Collection<?> objects) { 289 final ObservableSet<E> set = get(); 290 return (set == null)? EMPTY_SET.contains(objects) : set.containsAll(objects); 291 } 292 293 @Override 294 public boolean addAll(Collection<? extends E> elements) { 295 final ObservableSet<E> set = get(); 296 return (set == null)? EMPTY_SET.addAll(elements) : set.addAll(elements); 297 } 298 299 @Override 300 public boolean removeAll(Collection<?> objects) { 301 final ObservableSet<E> set = get(); 302 return (set == null)? EMPTY_SET.removeAll(objects) : set.removeAll(objects); 303 } 304 305 @Override 306 public boolean retainAll(Collection<?> objects) { 307 final ObservableSet<E> set = get(); 308 return (set == null)? EMPTY_SET.retainAll(objects) : set.retainAll(objects); 309 } 310 311 @Override 312 public void clear() { 313 final ObservableSet<E> set = get(); 314 if (set == null) { 315 EMPTY_SET.clear(); 316 } else { 317 set.clear(); 318 } 319 } 320 321 }