harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r546201 - /harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Observable.java
Date Mon, 11 Jun 2007 17:04:46 GMT
Author: tellison
Date: Mon Jun 11 10:04:45 2007
New Revision: 546201

URL: http://svn.apache.org/viewvc?view=rev&rev=546201
Log:
Apply patch HARMONY-4087 ([classlib][luni] Performance improvement of java.util.Observable)

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Observable.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Observable.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Observable.java?view=diff&rev=546201&r1=546200&r2=546201
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Observable.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Observable.java Mon
Jun 11 10:04:45 2007
@@ -17,117 +17,130 @@
 
 package java.util;
 
-
 /**
  * Observable is used to notify a group of Observer objects when a change
  * occurs.
  */
 public class Observable {
-	
-	Vector<Observer> observers = new Vector<Observer>();
 
-	boolean changed = false;
+    List<Observer> observers = new ArrayList<Observer>();
+
+    boolean changed = false;
+
+    /**
+     * Constructs a new Observable object.
+     */
+    public Observable() {
+        super();
+    }
+
+    /**
+     * Adds the specified Observer to the list of observers.
+     * 
+     * @param observer
+     *            the Observer to add
+     */
+    public void addObserver(Observer observer) {
+        if (observer == null) {
+            throw new NullPointerException();
+        }
+        synchronized (this) {
+            if (!observers.contains(observer))
+                observers.add(observer);
+        }
+    }
+
+    /**
+     * Clears the changed flag for this Observable. After calling
+     * <code>clearChanged()</code>, <code>hasChanged()</code> will
return
+     * false.
+     */
+    protected void clearChanged() {
+        changed = false;
+    }
+
+    /**
+     * Answers the number of Observers in the list of observers.
+     * 
+     * @return the number of observers
+     */
+    public int countObservers() {
+        return observers.size();
+    }
 
-	/**
-	 * Constructs a new Observable object.
-	 */
-	public Observable() {
-		super();
-	}
-
-	/**
-	 * Adds the specified Observer to the list of observers.
-	 * 
-	 * @param observer
-	 *            the Observer to add
-	 */
-	public synchronized void addObserver(Observer observer) {
-		if (observer == null) {
-			throw new NullPointerException();
-		}
-		if (!observers.contains(observer))
-			observers.addElement(observer);
-	}
-
-	/**
-	 * Clears the changed flag for this Observable.  After calling <code>clearChanged()</code>,
<code>hasChanged()</code> will return false.
-	 */
-	protected synchronized void clearChanged() {
-		changed = false;
-	}
-
-	/**
-	 * Answers the number of Observers in the list of observers.
-	 * 
-	 * @return the number of observers
-	 */
-	public synchronized int countObservers() {
-		return observers.size();
-	}
-
-	/**
-	 * Removes the specified Observer from the list of observers.
-	 * 
-	 * @param observer
-	 *            the Observer to remove
-	 */
-	public synchronized void deleteObserver(Observer observer) {
-		observers.removeElement(observer);
-	}
-
-	/**
-	 * Removes all Observers from the list of observers.
-	 */
-	public synchronized void deleteObservers() {
-		observers.setSize(0);
-	}
-
-	/**
-	 * Answers the changed flag for this Observable.
-	 * 
-	 * @return true when the changed flag for this Observable is set, false
-	 *         otherwise
-	 */
-	public synchronized boolean hasChanged() {
-		return changed;
-	}
-
-	/**
-	 * If <code>hasChanged()</code> returns true, calls the <code>update()</code>
method for
-	 * every Observer in the list of observers using null as the argument.
-	 * Afterwards calls <code>clearChanged()</code>.
+    /**
+     * Removes the specified Observer from the list of observers.
+     * 
+     * @param observer
+     *            the Observer to remove
+     */
+    public synchronized void deleteObserver(Observer observer) {
+        observers.remove(observer);
+    }
+
+    /**
+     * Removes all Observers from the list of observers.
+     */
+    public synchronized void deleteObservers() {
+        observers.clear();
+    }
+
+    /**
+     * Answers the changed flag for this Observable.
+     * 
+     * @return true when the changed flag for this Observable is set, false
+     *         otherwise
+     */
+    public boolean hasChanged() {
+        return changed;
+    }
+
+    /**
+     * If <code>hasChanged()</code> returns true, calls the
+     * <code>update()</code> method for every Observer in the list of
+     * observers using null as the argument. Afterwards calls
+     * <code>clearChanged()</code>.
      * 
      * Equivalent to calling <code>notifyObservers(null)</code>
-	 */
-	public void notifyObservers() {
-		notifyObservers(null);
-	}
-
-	/**
-	 * If <code>hasChanged()</code> returns true, calls the <code>update()</code>
method for
-	 * every Observer in the list of observers using the specified argument.
-	 * Afterwards calls <code>clearChanged()</code>.
-	 * 
-	 * @param data
-	 *            the argument passed to update()
-	 */
+     */
+    public void notifyObservers() {
+        notifyObservers(null);
+    }
+
+    /**
+     * If <code>hasChanged()</code> returns true, calls the
+     * <code>update()</code> method for every Observer in the list of
+     * observers using the specified argument. Afterwards calls
+     * <code>clearChanged()</code>.
+     * 
+     * @param data
+     *            the argument passed to update()
+     */
     @SuppressWarnings("unchecked")
     public void notifyObservers(Object data) {
-		if (hasChanged()) {
-			// Must clone the vector in case deleteObserver is called
-			Vector<Observer> clone = (Vector<Observer>)observers.clone();
-			int size = clone.size();
-			for (int i = 0; i < size; i++) {
-				clone.elementAt(i).update(this, data);
+        int size = 0;
+        Observer[] arrays = null;
+        synchronized (this) {
+            if (hasChanged()) {
+                clearChanged();
+                size = observers.size();
+                arrays = new Observer[size];
+                observers.toArray(arrays);
+            }
+        }
+        if (arrays != null) {
+            for (Observer observer : arrays) {
+                observer.update(this, data);
             }
-			clearChanged();
-		}
-	}
-
-	/**
-	 * Sets the changed flag for this Observable. After calling <code>setChanged()</code>,
<code>hasChanged()</code> will return true.
-	 */
-	protected synchronized void setChanged() {
-		changed = true;
-	}
+        }
+    }
+
+    /**
+     * Sets the changed flag for this Observable. After calling
+     * <code>setChanged()</code>, <code>hasChanged()</code> will
return
+     * true.
+     */
+    protected void setChanged() {
+        changed = true;
+    }
 }



Mime
View raw message