jdk/src/share/classes/java/util/Observable.java
author darcy
Fri, 19 Aug 2011 17:42:24 -0700
changeset 10350 6d009f117062
parent 5506 202f599c92aa
child 12448 b95438b17098
permissions -rw-r--r--
4748706: typos in java.util.Observable documentation Reviewed-by: iris
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10350
6d009f117062 4748706: typos in java.util.Observable documentation
darcy
parents: 5506
diff changeset
     2
 * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * This class represents an observable object, or "data"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * in the model-view paradigm. It can be subclassed to represent an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * object that the application wants to have observed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * An observable object can have one or more observers. An observer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * may be any object that implements interface <tt>Observer</tt>. After an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * observable instance changes, an application calling the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <code>Observable</code>'s <code>notifyObservers</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * causes all of its observers to be notified of the change by a call
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * to their <code>update</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * The order in which notifications will be delivered is unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * The default implementation provided in the Observable class will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * notify Observers in the order in which they registered interest, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * subclasses may change this order, use no guaranteed order, deliver
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * notifications on separate threads, or may guarantee that their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * subclass follows this order, as they choose.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
10350
6d009f117062 4748706: typos in java.util.Observable documentation
darcy
parents: 5506
diff changeset
    47
 * Note that this notification mechanism has nothing to do with threads
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * and is completely separate from the <tt>wait</tt> and <tt>notify</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * mechanism of class <tt>Object</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * When an observable object is newly created, its set of observers is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * empty. Two observers are considered the same if and only if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <tt>equals</tt> method returns true for them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author  Chris Warth
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @see     java.util.Observable#notifyObservers()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @see     java.util.Observable#notifyObservers(java.lang.Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see     java.util.Observer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see     java.util.Observer#update(java.util.Observable, java.lang.Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
public class Observable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private boolean changed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private Vector obs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /** Construct an Observable with zero Observers. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public Observable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        obs = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Adds an observer to the set of observers for this object, provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * that it is not the same as some observer already in the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * The order in which notifications will be delivered to multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * observers is not specified. See the class comment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param   o   an observer to be added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @throws NullPointerException   if the parameter o is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public synchronized void addObserver(Observer o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (o == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (!obs.contains(o)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            obs.addElement(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Deletes an observer from the set of observers of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Passing <CODE>null</CODE> to this method will have no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param   o   the observer to be deleted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public synchronized void deleteObserver(Observer o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        obs.removeElement(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * If this object has changed, as indicated by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * <code>hasChanged</code> method, then notify all of its observers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * and then call the <code>clearChanged</code> method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * indicate that this object has no longer changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Each observer has its <code>update</code> method called with two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * arguments: this observable object and <code>null</code>. In other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * words, this method is equivalent to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <blockquote><tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * notifyObservers(null)</tt></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @see     java.util.Observable#clearChanged()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @see     java.util.Observable#hasChanged()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @see     java.util.Observer#update(java.util.Observable, java.lang.Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public void notifyObservers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        notifyObservers(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * If this object has changed, as indicated by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * <code>hasChanged</code> method, then notify all of its observers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * and then call the <code>clearChanged</code> method to indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * that this object has no longer changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Each observer has its <code>update</code> method called with two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * arguments: this observable object and the <code>arg</code> argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @param   arg   any object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @see     java.util.Observable#clearChanged()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @see     java.util.Observable#hasChanged()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @see     java.util.Observer#update(java.util.Observable, java.lang.Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public void notifyObservers(Object arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
         * a temporary array buffer, used as a snapshot of the state of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
         * current Observers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        Object[] arrLocal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            /* We don't want the Observer doing callbacks into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
             * arbitrary code while holding its own Monitor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
             * The code where we extract each Observable from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
             * the Vector and store the state of the Observer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
             * needs synchronization, but notifying observers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
             * does not (should not).  The worst result of any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
             * potential race-condition here is that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
             * 1) a newly-added Observer will miss a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
             *   notification in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
             * 2) a recently unregistered Observer will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
             *   wrongly notified when it doesn't care
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            if (!changed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            arrLocal = obs.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            clearChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        for (int i = arrLocal.length-1; i>=0; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            ((Observer)arrLocal[i]).update(this, arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Clears the observer list so that this object no longer has any observers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public synchronized void deleteObservers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        obs.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Marks this <tt>Observable</tt> object as having been changed; the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <tt>hasChanged</tt> method will now return <tt>true</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    protected synchronized void setChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        changed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Indicates that this object has no longer changed, or that it has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * already notified all of its observers of its most recent change,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * so that the <tt>hasChanged</tt> method will now return <tt>false</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * This method is called automatically by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * <code>notifyObservers</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @see     java.util.Observable#notifyObservers()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @see     java.util.Observable#notifyObservers(java.lang.Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    protected synchronized void clearChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        changed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Tests if this object has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @return  <code>true</code> if and only if the <code>setChanged</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *          method has been called more recently than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *          <code>clearChanged</code> method on this object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *          <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @see     java.util.Observable#clearChanged()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @see     java.util.Observable#setChanged()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public synchronized boolean hasChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return changed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Returns the number of observers of this <tt>Observable</tt> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @return  the number of observers of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public synchronized int countObservers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return obs.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
}