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.Iterator;
029import java.util.Map;
030import java.util.Map.Entry;
031import javafx.beans.binding.Bindings;
032import javafx.beans.binding.MapExpression;
033import javafx.collections.ObservableMap;
034
035/**
036 * Super class for all readonly properties wrapping an {@link javafx.collections.ObservableMap}.
037 *
038 * @see javafx.collections.ObservableMap
039 * @see javafx.beans.value.ObservableMapValue
040 * @see javafx.beans.binding.MapExpression
041 * @see ReadOnlyProperty
042 *
043 * @param <K> the type of the key elements of the map
044 * @param <V> the type of the value elements of the map
045 */
046public abstract class ReadOnlyMapProperty<K, V> extends MapExpression<K, V> implements ReadOnlyProperty<ObservableMap<K, V>>  {
047
048    /**
049     * The constructor of {@code ReadOnlyMapProperty}.
050     */
051    public ReadOnlyMapProperty() {
052    }
053
054    /**
055     * Creates a bidirectional content binding of the {@link javafx.collections.ObservableMap}, that is
056     * wrapped in this {@code ReadOnlyMapProperty}, and another {@code ObservableMap}.
057     * <p>
058     * A bidirectional content binding ensures that the content of two {@code ObservableMaps} is the
059     * same. If the content of one of the maps changes, the other one will be updated automatically.
060     *
061     * @param map the {@code ObservableMap} this property should be bound to
062     * @throws NullPointerException if {@code map} is {@code null}
063     * @throws IllegalArgumentException if {@code map} is the same map that this {@code ReadOnlyMapProperty} points to
064     */
065    public void bindContentBidirectional(ObservableMap<K, V> map) {
066        Bindings.bindContentBidirectional(this, map);
067    }
068
069    /**
070     * Deletes a bidirectional content binding between the {@link javafx.collections.ObservableMap}, that is
071     * wrapped in this {@code ReadOnlyMapProperty}, 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 map that this {@code ReadOnlyMapProperty} 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.ObservableMap}, that is
083     * wrapped in this {@code ReadOnlyMapProperty}, and another {@code ObservableMap}.
084     * <p>
085     * A content binding ensures that the content of the wrapped {@code ObservableMaps} is the
086     * same as that of the other map. If the content of the other map changes, the wrapped map will be updated
087     * automatically. Once the wrapped list is bound to another map, you must not change it directly.
088     *
089     * @param map the {@code ObservableMap} this property should be bound to
090     * @throws NullPointerException if {@code map} is {@code null}
091     * @throws IllegalArgumentException if {@code map} is the same map that this {@code ReadOnlyMapProperty} points to
092     */
093    public void bindContent(ObservableMap<K, V> map) {
094        Bindings.bindContent(this, map);
095    }
096
097    /**
098     * Deletes a content binding between the {@link javafx.collections.ObservableMap}, that is
099     * wrapped in this {@code ReadOnlyMapProperty}, 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 map that this {@code ReadOnlyMapProperty} 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 Map))
115            return false;
116        Map<K,V> m = (Map<K,V>) obj;
117        if (m.size() != size())
118            return false;
119
120        try {
121            for (Entry<K,V> e : entrySet()) {
122                K key = e.getKey();
123                V value = e.getValue();
124                if (value == null) {
125                    if (!(m.get(key)==null && m.containsKey(key)))
126                        return false;
127                } else {
128                    if (!value.equals(m.get(key)))
129                        return false;
130                }
131            }
132        } catch (ClassCastException unused) {
133            return false;
134        } catch (NullPointerException unused) {
135            return false;
136        }
137
138        return true;
139    }
140
141    /**
142     * Returns a hash code for this {@code ReadOnlyMapProperty} object.
143     * @return a hash code for this {@code ReadOnlyMapProperty} object.
144     */
145    @Override
146    public int hashCode() {
147        int h = 0;
148        for (Entry<K,V> e : entrySet()) {
149            h += e.hashCode();
150        }
151        return h;
152    }
153
154    /**
155     * Returns a string representation of this {@code ReadOnlyMapProperty} object.
156     * @return a string representation of this {@code ReadOnlyMapProperty} object.
157     */
158    @Override
159    public String toString() {
160        final Object bean = getBean();
161        final String name = getName();
162        final StringBuilder result = new StringBuilder(
163                "ReadOnlyMapProperty [");
164        if (bean != null) {
165            result.append("bean: ").append(bean).append(", ");
166        }
167        if ((name != null) && !name.equals("")) {
168            result.append("name: ").append(name).append(", ");
169        }
170        result.append("value: ").append(get()).append("]");
171        return result.toString();
172    }
173
174}