diff options
author | Paul Fisher <rao@gnu.org> | 2000-03-15 21:31:53 +0000 |
---|---|---|
committer | Paul Fisher <rao@gnu.org> | 2000-03-15 21:31:53 +0000 |
commit | a982c170976d65285bfa4a7e501c6b23f735bff8 (patch) | |
tree | e47e7cf411027c25ae634ae0a2e42935ac12de8f /java/util/Observable.java | |
parent | b19436a3cb0b71bafb35585701bfd7e4210a9b15 (diff) | |
download | classpath-a982c170976d65285bfa4a7e501c6b23f735bff8.tar.gz |
Make thread-safe.
Diffstat (limited to 'java/util/Observable.java')
-rw-r--r-- | java/util/Observable.java | 224 |
1 files changed, 117 insertions, 107 deletions
diff --git a/java/util/Observable.java b/java/util/Observable.java index 681cbf6ca..1a57865ed 100644 --- a/java/util/Observable.java +++ b/java/util/Observable.java @@ -1,117 +1,127 @@ -/** - * Observable.java - * - * Extended when an object wants to be Observable. - * - * @author Daniel Rall (<a href="mailto:dlr@west.net">dlr@west.net</a>, <a href="http://www.finemaltcoding.com/">Fine Malt Coding</a>) - * @version 1.0b - */ +/* java.util.Observable + Copyright (C) 1999, 2000 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA +02111-1307 USA. + +As a special exception, if you link this library with other files to +produce an executable, this library does not by itself cause the +resulting executable to be covered by the GNU General Public License. +This exception does not however invalidate any other reasons why the +executable file might be covered by the GNU General Public License. */ -///////////////////////////////////////////////////////////////////////////// -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU Library General Public License as published -// by the Free Software Foundation, version 2. (see COPYING.LIB) -// -// This program is distributed in the hope that it will be useful, but -// WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Library General Public License for more details. -// -// You should have received a copy of the GNU Library General Public License -// along with this program; if not, write to the Free Software Foundation -// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA -///////////////////////////////////////////////////////////////////////////// package java.util; public class Observable { - private static final short INITIAL_SIZE = 4; - - private boolean changed; - private Vector observers; - - /** - * Constructs an Observable with 0 Observers. A resizable buffer is used to - * store Observer references, and is initialized to a small capacity. - */ - public Observable() - { - changed = false; - observers = new Vector(INITIAL_SIZE); - } + private boolean changed; + private Vector observers; - /** - * Adds an Observer. Does not allow duplicates. - * - * @param observer Observer to add. - */ - public void addObserver( Observer observer ) - { - if ( !observers.contains(observer) ) { observers.add(observer); } - } + /** + * Constructs an Observable with zero Observers. + */ + public Observable () + { + changed = false; + observers = new Vector (); + } - /** - * Reset this Observable's state to unchanged. - */ - protected void clearChanged() { changed = false; } + /** + * Adds an Observer. + * + * @param observer Observer to add. + */ + public void addObserver (Observer observer) + { + observers.add (observer); + } + + /** + * Reset this Observable's state to unchanged. + */ + protected void clearChanged () + { + changed = false; + } + + /** + * @return Number of Observers for this Observable. + */ + public int countObservers() + { + return observers.size (); + } + + /** + * Deletes an Observer of this Observable. + * + * @param victim Observer to delete. + */ + public void deleteObserver (Observer victim) + { + observers.remove (victim); + } + + /** + * Deletes all Observers of this Observable. + */ + public void deleteObservers () + { + observers.removeAllElements (); + } + + /** + * @return Whether or not this Observable has changed. + */ + public boolean hasChanged () + { + return changed; + } + + /** + * Tell Observers that this Observable has changed, then + * resets state to unchanged. + */ + public void notifyObservers () + { + notifyObservers (null); + } + + /** + * @param obj Arguement to Observer's update method. + */ + public void notifyObservers (Object obj) + { + if (!changed) return; + Vector ob1 = (Vector) observers.clone (); + + for (int i = 0; i < ob1.size (); i++) + ((Observer)ob1.elementAt (i)).update (this, obj); + + changed = false; + } - /** - * @return Number of Observers for this Observable. - */ - public int countObservers() { return observers.size(); } - - /** - * Deletes an Observer of this Observable. - * - * @param victim Observer to delete. - */ - public void deleteObserver( Observer victim ) { observers.remove(victim); } - - /** - * Deletes all Observers of this Observable. - */ - public void deleteObservers() - { - Enumeration enum = observers.elements(); - - while ( enum.hasMoreElements() ) - { - deleteObserver( (Observer)enum.nextElement() ); - } - } + /** + * Marks this Observable as having changed. + */ + protected void setChanged () + { + changed = true; + } +} - /** - * @return Whether or not this Observable has changed. - */ - public boolean hasChanged() { return changed; } - - /** - * Tell Observers that this Observable has changed, then - * resets state to unchanged. - */ - public void notifyObservers() { notifyObservers(null); } - - /** - * @param obj Arguement to Observer's update method. - */ - public void notifyObservers( Object obj ) - { - if ( hasChanged() ) - { - Enumeration enum = observers.elements(); - - while ( enum.hasMoreElements() ) - { - ( (Observer)enum.nextElement() ).update(this, obj);; - } - - clearChanged(); - } - } - - /** - * Marks this Observable as having changed. - */ - protected void setChanged() { changed = true; } -}
\ No newline at end of file |