Author: adrianc
Date: Mon Apr 22 05:37:31 2013
New Revision: 1470389

URL: http://svn.apache.org/r1470389
Log:
Added an Observable class and Observer interface. These will be used in an 
improved GenericEntity implementation.

Added:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Observable.java   (with 
props)
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Observer.java   (with 
props)

Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Observable.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Observable.java?rev=1470389&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Observable.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Observable.java Mon Apr 
22 05:37:31 2013
@@ -0,0 +1,121 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ 
*******************************************************************************/
+package org.ofbiz.base.util;
+
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+/**
+ * An observable object.
+ * <p>This class is similar to <code>java.util.Observable</code> but it has 
some differences:
+ * <ul>
+ * <li>It has improved concurrency</li>
+ * <li>It cannot be subclassed</li>
+ * <li>The <code>notifyObservers</code> method does not clear the changed 
flag</li>
+ * <li>Protected methods have been made public</li>
+ * </ul></p>
+ *
+ */
+public final class Observable {
+
+    private final AtomicBoolean changed = new AtomicBoolean();;
+    private final CopyOnWriteArrayList<Observer> observers = new 
CopyOnWriteArrayList<Observer>();
+
+    public Observable() {}
+
+    public Observable(Observable observable) {
+        Assert.notNull("observable", observable);
+        changed.set(observable.changed.get());
+        observers.addAll(observable.observers);
+    }
+
+    /**
+     * Adds an observer to the set of observers for this object. 
+     *
+     * @param observer the observer to be added.
+     */
+    public void addObserver(Observer observer) {
+        Assert.notNull("observer", observer);
+        observers.addIfAbsent(observer);
+    }
+
+    /**
+     * Clears the changed flag. 
+     */
+    public void clearChanged() {
+        changed.set(false);
+    }
+
+    /**
+     * Deletes an observer from the set of observers of this object. 
+     * Passing <code>null</code> to this method will have no effect.
+     * 
+     * @param observer the observer to be deleted.
+     */
+    public void deleteObserver(Observer observer) {
+        observers.remove(observer);
+    }
+
+    /**
+     * Clears the observer list so that this object no longer has any 
observers.
+     */
+    public void deleteObservers() {
+        observers.clear();
+    }
+
+    /**
+     * Returns <code>true</code> if this object has changed. 
+     *
+     */
+    public boolean hasChanged() {
+        return changed.get();
+    }
+
+    /**
+     * Notify all of the observers. 
+     * <p>Each <code>Observer</code> has its <code>update</code> method called 
with two
+     * arguments: this observable object and <code>null</code>. In other 
+     * words, this method is equivalent to:
+     * <blockquote><tt>
+     * notifyObservers(null)</tt></blockquote></p>
+     *
+     */
+    public void notifyObservers() {
+        notifyObservers(null);
+    }
+
+    /**
+     * Notify all of the observers. 
+     * <p>Each observer has its <code>update</code> method called with two
+     * arguments: this observable object and the <code>arg</code> argument.</p>
+     *
+     */
+    public void notifyObservers(Object arg) {
+        for (Observer observer : observers) {
+            observer.update(this, arg);
+        }
+    }
+
+    /**
+     * Sets the changed flag to <code>true</code>. 
+     */
+    public void setChanged() {
+        changed.set(true);
+    }
+}

Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Observable.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Observable.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Rev URL

Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Observer.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Observer.java?rev=1470389&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Observer.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Observer.java Mon Apr 22 
05:37:31 2013
@@ -0,0 +1,33 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ 
*******************************************************************************/
+package org.ofbiz.base.util;
+
+/**
+ * An <code>Observable</code> observer.
+ *
+ */
+public interface Observer {
+    /**
+     * Called when <code>Observable.notifyObservers</code> is invoked.
+     * 
+     * @param observable
+     * @param arg
+     */
+    void update(Observable observable, Object arg);
+}

Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Observer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/Observer.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Rev URL


Reply via email to