org/objectweb/proactive/benchmarks/timit/util/observing/RealEventObservable.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.benchmarks.timit.util.observing;
00032 
00033 import java.util.Vector;
00034 
00042 public class RealEventObservable implements EventObservable {
00043     private boolean changed = false;
00044 
00045     private Vector<EventObserver> eventDataObservers;
00046 
00049     public RealEventObservable() {
00050         this.eventDataObservers = new Vector<EventObserver>();
00051     }
00052 
00064     public synchronized void addObserver(EventObserver o) {
00065         if (o == null) {
00066             throw new NullPointerException();
00067         }
00068         if (!this.eventDataObservers.contains(o)) {
00069             this.eventDataObservers.addElement(o);
00070         }
00071     }
00072 
00080     public synchronized void deleteObserver(EventObserver o) {
00081         this.eventDataObservers.removeElement(o);
00082     }
00083 
00099     public void notifyObservers() {
00100         this.notifyObservers(null);
00101     }
00102 
00118     public void notifyObservers(Object arg) {
00119         /*
00120          * a temporary array buffer, used as a snapshot of the state of current
00121          * Observers.
00122          */
00123         Object[] arrLocal;
00124         this.setChanged();
00125         synchronized (this) {
00126             /*
00127              * We don't want the Observer doing callbacks into arbitrary code
00128              * while holding its own Monitor. The code where we extract each
00129              * Observable from the Vector and store the state of the Observer
00130              * needs synchronization, but notifying observers does not (should
00131              * not). The worst result of any potential race-condition here is
00132              * that: 1) a newly-added Observer will miss a notification in
00133              * progress 2) a recently unregistered Observer will be wrongly
00134              * notified when it doesn't care
00135              */
00136             if (!this.changed) {
00137                 return;
00138             }
00139             arrLocal = this.eventDataObservers.toArray();
00140             this.clearChanged();
00141         }
00142 
00143         for (int i = arrLocal.length - 1; i >= 0; i--) {
00144             ((EventObserver) arrLocal[i]).update(this, arg);
00145         }
00146     }
00147 
00151     public synchronized void deleteObservers() {
00152         this.eventDataObservers.removeAllElements();
00153     }
00154 
00159     public synchronized void setChanged() {
00160         this.changed = true;
00161     }
00162 
00173     public synchronized void clearChanged() {
00174         this.changed = false;
00175     }
00176 
00187     public synchronized boolean hasChanged() {
00188         return this.changed;
00189     }
00190 
00196     public synchronized int countObservers() {
00197         return this.eventDataObservers.size();
00198     }
00199 
00206     public synchronized EventDataBag getEventDataBag(int subjectRank) {
00207         EventDataBag result = new EventDataBag(subjectRank);
00208         Vector<EventData> v = new Vector<EventData>();
00209         EventObserver eventDataObserver = null;
00210         EventData eventData = null;
00211         for (int i = 0; i < this.eventDataObservers.size(); i++) {
00212             eventDataObserver = this.eventDataObservers.get(i);
00213             if (eventDataObserver == null) {
00214                 throw new NullPointerException();
00215             } else {
00216                 eventData = eventDataObserver.getEventData();
00217                 if (eventData == null) {
00218                     throw new NullPointerException();
00219                 } else {
00220                     v.add(eventData);
00221                 }
00222             }
00223         }
00224         result.setBag(v);
00225         return result;
00226     }
00227 }

Generated on Mon Jan 22 15:16:04 2007 for ProActive by  doxygen 1.5.1