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.beans.value;
027
028import javafx.beans.InvalidationListener;
029import javafx.beans.Observable;
030
031/**
032 * An {@code ObservableValue} is an entity that wraps a value and allows to
033 * observe the value for changes. In general this interface should not be
034 * implemented directly but one of its sub-interfaces (
035 * {@code ObservableBooleanValue} etc.).
036 * <p>
037 * The value of the {@code ObservableValue} can be requested with
038 * {@link #getValue()}.
039 * <p>
040 * An implementation of {@code ObservableValue} may support lazy evaluation,
041 * which means that the value is not immediately recomputed after changes, but
042 * lazily the next time the value is requested. All bindings and properties in
043 * this library support lazy evaluation.
044 * <p>
045 * An {@code ObservableValue} generates two types of events: change events and
046 * invalidation events. A change event indicates that the value has changed. An
047 * invalidation event is generated, if the current value is not valid anymore.
048 * This distinction becomes important, if the {@code ObservableValue} supports
049 * lazy evaluation, because for a lazily evaluated value one does not know if an
050 * invalid value really has changed until it is recomputed. For this reason,
051 * generating change events requires eager evaluation while invalidation events
052 * can be generated for eager and lazy implementations.
053 * <p>
054 * Implementations of this class should strive to generate as few events as
055 * possible to avoid wasting too much time in event handlers. Implementations in
056 * this library mark themselves as invalid when the first invalidation event
057 * occurs. They do not generate anymore invalidation events until their value is
058 * recomputed and valid again.
059 * <p>
060 * Two types of listeners can be attached to an {@code ObservableValue}:
061 * {@link InvalidationListener} to listen to invalidation events and
062 * {@link ChangeListener} to listen to change events.
063 * <p>
064 * Important note: attaching a {@code ChangeListener} enforces eager computation
065 * even if the implementation of the {@code ObservableValue} supports lazy
066 * evaluation.
067 * 
068 * @param <T>
069 *            The type of the wrapped value.
070 * 
071 * @see ObservableBooleanValue
072 * @see ObservableDoubleValue
073 * @see ObservableFloatValue
074 * @see ObservableIntegerValue
075 * @see ObservableLongValue
076 * @see ObservableNumberValue
077 * @see ObservableObjectValue
078 * @see ObservableStringValue
079 * 
080 * 
081 */
082public interface ObservableValue<T> extends Observable {
083
084    /**
085     * Adds a {@link ChangeListener} which will be notified whenever the value
086     * of the {@code ObservableValue} changes. If the same listener is added
087     * more than once, then it will be notified more than once. That is, no
088     * check is made to ensure uniqueness.
089     * <p>
090     * Note that the same actual {@code ChangeListener} instance may be safely
091     * registered for different {@code ObservableValues}.
092     * <p>
093     * The {@code ObservableValue} stores a strong reference to the listener
094     * which will prevent the listener from being garbage collected and may
095     * result in a memory leak. It is recommended to either unregister a
096     * listener by calling {@link #removeListener(ChangeListener)
097     * removeListener} after use or to use an instance of
098     * {@link WeakChangeListener} avoid this situation.
099     * 
100     * @see #removeListener(ChangeListener)
101     * 
102     * @param listener
103     *            The listener to register
104     * @throws NullPointerException
105     *             if the listener is null
106     */
107    void addListener(ChangeListener<? super T> listener);
108
109    /**
110     * Removes the given listener from the list of listeners, that are notified
111     * whenever the value of the {@code ObservableValue} changes.
112     * <p>
113     * If the given listener has not been previously registered (i.e. it was
114     * never added) then this method call is a no-op. If it had been previously
115     * added then it will be removed. If it had been added more than once, then
116     * only the first occurrence will be removed.
117     * 
118     * @see #addListener(ChangeListener)
119     * 
120     * @param listener
121     *            The listener to remove
122     * @throws NullPointerException
123     *             if the listener is null
124     */
125    void removeListener(ChangeListener<? super T> listener);
126
127    /**
128     * Returns the current value of this {@code ObservableValue}
129     * 
130     * @return The current value
131     */
132    T getValue();
133}