/Users/detro/workspace-nb/JAEBI-BlueToothJ2MEClient/src/org/jaebi/midlet/util/Observable.java

Vai alla documentazione di questo file.
00001 /*
00002  * Observable.java
00003  *
00004  * Created on 31 maggio 2005, 13.05
00005  *
00006  * To change this template, choose Tools | Options and locate the template under
00007  * the Source Creation and Management node. Right-click the template and choose
00008  * Open. You can then make changes to the template in the Source Editor.
00009  */
00010 
00011 package org.jaebi.midlet.util;
00012 
00013 import java.util.Enumeration;
00014 import java.util.Vector;
00015 
00020 public class Observable {
00021     
00022     private boolean changed = false;
00023     private Vector obs;
00024     
00026     public Observable() {
00027         obs = new Vector();
00028     }
00029     
00039     public synchronized void addObserver(Observer o) {
00040         if (o == null)
00041             throw new NullPointerException();
00042         if (!obs.contains(o)) {
00043             obs.addElement(o);
00044         }
00045     }
00046     
00052     public synchronized void deleteObserver(Observer o) {
00053         obs.removeElement(o);
00054     }
00055     
00065     public void notifyObservers() {
00066         notifyObservers(null);
00067     }
00068     
00080     public void notifyObservers(Object arg) {
00081         /*
00082          *Collezione degli observer usata come snapshot dello stato degli observer correnti
00083          */
00084         Enumeration observers;
00085         
00086         synchronized (this) {
00087             /*
00088              * Il codice incaricato di estrarre la lista degli observer da notificare
00089              * necessita di sincronizzazione ma la notifica vera e propria no (non dovrebbe)
00090              * Il peggior risultato di una qualsiasi race-condition che possa verificarsi qui (in assenza di sincronizzazione
00091              *  è il seguente:
00092              * 1) a newly-added Observer will miss a
00093              *   notification in progress: siccome anche il metododo addObserver() è
00094              *   dichiarato sinchronized non può verificarsi che un flusso di controllo aggiunga un nuovo observer
00095              *   mentre un altro sta recuperando la lista degli observer per effettuare la notifica
00096              * 2) a recently unregistered Observer will be
00097              *   wrongly notified when it doesn't care: non può verificarsi che un flusso di controllo
00098              *   stia rimuovendo un observer mentre un altro sta recuperando
00099              *   la lista delgi observer a cui inviare la notifica (il metodo deleteObserver() è dichiarato anch'esso
00100              *   synchronized
00101              */
00102             if (!changed)
00103                 return;
00104             observers = obs.elements();
00105             clearChanged();
00106         }
00107         
00108         while (observers.hasMoreElements())
00109             ((Observer)observers.nextElement()).update(this, arg);
00110            
00111     }
00112     
00116     public synchronized void deleteObservers() {
00117         obs.removeAllElements();
00118     }
00119     
00123     protected synchronized void setChanged() {
00124         changed = true;
00125     }
00126     
00136     protected synchronized void clearChanged() {
00137         changed = false;
00138     }
00139     
00150     public synchronized boolean hasChanged() {
00151         return changed;
00152     }
00153     
00159     public synchronized int countObservers() {
00160         return obs.size();
00161     }
00162 }

Generato il Thu Jun 23 00:03:00 2005 per JAEBI - BlueTooth J2ME Midlet Client da  doxygen 1.4.3