Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2010, 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.collections;
027
028/**
029 * Interface that receives notifications of changes to an ObservableMap.
030 * @param <K> the key element type
031 * @param <V> the value element type
032 */
033public interface MapChangeListener<K, V> {
034
035    /**
036     * An elementary change done to an ObservableMap.
037     * Change contains information about a put or remove operation.
038     * Note that put operation might remove an element if there was
039     * already a value associated with the same key. In this case
040     * wasAdded() and wasRemoved() will both return true.
041     *
042     * @param <K> key type
043     * @param <V> value type
044     */
045    public static abstract class Change<K, V> {
046
047        private final ObservableMap<K,V> map;
048
049        /**
050         * Constructs a change associated with a map.
051         * @param map the source of the change
052         */
053        public Change(ObservableMap<K, V> map) {
054            this.map = map;
055        }
056
057        /**
058         * An observable map that is associated with the change.
059         * @return the source map
060         */
061        public ObservableMap<K, V> getMap() {
062            return map;
063        }
064
065        /**
066         * If this change is a result of add operation.
067         * @return true if a new value (or key-value) entry was added to the map
068         */
069        public abstract boolean wasAdded();
070
071        /**
072         * If this change is a result of removal operation.
073         * Note that an element might be removed even as a result of put operation.
074         * @return true if an old value (or key-value) entry was removed from the map
075         */
076        public abstract boolean wasRemoved();
077
078        /**
079         * A key associated with the change.
080         * If the change is a remove change, the key no longer exist in a map.
081         * Otherwise, the key got set to a new value.
082         * @return the key that changed
083         */
084        public abstract K getKey();
085
086        /**
087         * Get the new value of the key. Return null if this is a removal.
088         * @return the value that is now associated with the key
089         */
090        public abstract V getValueAdded();
091
092        /**
093         * Get the old value of the key. This is null if and only if the value was
094         * added to the key that was not previously in the map.
095         * @return the value previously associated with the key
096         */
097        public abstract V getValueRemoved();
098
099    }
100
101    /**
102     * Called after a change has been made to an ObservableMap.
103     * This method is called on every elementary change (put/remove) once.
104     * This means, complex changes like keySet().removeAll(Collection) or clear()
105     * may result in more than one call of onChanged method.
106     *
107     * @param change the change that was made
108     */
109    void onChanged(Change<? extends K,? extends V> change);
110}