Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.beans.property;
027
028import java.util.Collection;
029import java.util.Iterator;
030import java.util.Set;
031import javafx.beans.binding.Bindings;
032import javafx.beans.binding.SetExpression;
033import javafx.collections.ObservableList;
034import javafx.collections.ObservableSet;
035
036/**
037 * Super class for all readonly properties wrapping an {@link javafx.collections.ObservableSet}.
038 *
039 * @see javafx.collections.ObservableSet
040 * @see javafx.beans.value.ObservableSetValue
041 * @see javafx.beans.binding.SetExpression
042 * @see ReadOnlyProperty
043 *
044 * @param <E> the type of the {@code Set} elements
045 */
046public abstract class ReadOnlySetProperty<E> extends SetExpression<E> implements ReadOnlyProperty<ObservableSet<E>>  {
047
048    /**
049     * The constructor of {@code ReadOnlySetProperty}.
050     */
051    public ReadOnlySetProperty() {
052    }
053
054    /**
055     * Creates a bidirectional content binding of the {@link javafx.collections.ObservableSet}, that is
056     * wrapped in this {@code ReadOnlySetProperty}, and another {@code ObservableSet}.
057     * <p>
058     * A bidirectional content binding ensures that the content of two {@code ObservableSets} is the
059     * same. If the content of one of the sets changes, the other one will be updated automatically.
060     *
061     * @param set the {@code ObservableSet} this property should be bound to
062     * @throws NullPointerException if {@code set} is {@code null}
063     * @throws IllegalArgumentException if {@code set} is the same set that this {@code ReadOnlySetProperty} points to
064     */
065    public void bindContentBidirectional(ObservableSet<E> set) {
066        Bindings.bindContentBidirectional(this, set);
067    }
068
069    /**
070     * Deletes a bidirectional content binding between the {@link javafx.collections.ObservableSet}, that is
071     * wrapped in this {@code ReadOnlySetProperty}, and another {@code Object}.
072     *
073     * @param object the {@code Object} to which the bidirectional binding should be removed
074     * @throws NullPointerException if {@code object} is {@code null}
075     * @throws IllegalArgumentException if {@code object} is the same set that this {@code ReadOnlySetProperty} points to
076     */
077    public void unbindContentBidirectional(Object object) {
078        Bindings.unbindContentBidirectional(this, object);
079    }
080
081    /**
082     * Creates a content binding between the {@link javafx.collections.ObservableSet}, that is
083     * wrapped in this {@code ReadOnlySetProperty}, and another {@code ObservableSet}.
084     * <p>
085     * A content binding ensures that the content of the wrapped {@code ObservableSets} is the
086     * same as that of the other set. If the content of the other set changes, the wrapped set will be updated
087     * automatically. Once the wrapped set is bound to another set, you must not change it directly.
088     *
089     * @param set the {@code ObservableSet} this property should be bound to
090     * @throws NullPointerException if {@code set} is {@code null}
091     * @throws IllegalArgumentException if {@code set} is the same set that this {@code ReadOnlySetProperty} points to
092     */
093    public void bindContent(ObservableSet<E> set) {
094        Bindings.bindContent(this, set);
095    }
096
097    /**
098     * Deletes a content binding between the {@link javafx.collections.ObservableSet}, that is
099     * wrapped in this {@code ReadOnlySetProperty}, and another {@code Object}.
100     *
101     * @param object the {@code Object} to which the binding should be removed
102     * @throws NullPointerException if {@code object} is {@code null}
103     * @throws IllegalArgumentException if {@code object} is the same set that this {@code ReadOnlySetProperty} points to
104     */
105    public void unbindContent(Object object) {
106        Bindings.unbindContent(this, object);
107    }
108
109    @Override
110    public boolean equals(Object obj) {
111        if (obj == this)
112            return true;
113
114        if (!(obj instanceof Set))
115            return false;
116        Set c = (Set) obj;
117        if (c.size() != size())
118            return false;
119        try {
120            return containsAll(c);
121        } catch (ClassCastException unused)   {
122            return false;
123        } catch (NullPointerException unused) {
124            return false;
125        }
126    }
127
128    /**
129     * Returns a hash code for this {@code ReadOnlySetProperty} object.
130     * @return a hash code for this {@code ReadOnlySetProperty} object.
131     */
132    @Override
133    public int hashCode() {
134        int h = 0;
135        for (E e : get()) {
136            if (e != null)
137                h += e.hashCode();
138        }
139        return h;
140    }
141
142    /**
143     * Returns a string representation of this {@code ReadOnlySetProperty} object.
144     * @return a string representation of this {@code ReadOnlySetProperty} object.
145     */
146    @Override
147    public String toString() {
148        final Object bean = getBean();
149        final String name = getName();
150        final StringBuilder result = new StringBuilder(
151                "ReadOnlySetProperty [");
152        if (bean != null) {
153            result.append("bean: ").append(bean).append(", ");
154        }
155        if ((name != null) && !name.equals("")) {
156            result.append("name: ").append(name).append(", ");
157        }
158        result.append("value: ").append(get()).append("]");
159        return result.toString();
160    }
161
162}