    /*
     * JBoss, the OpenSource J2EE webOS
     *
     * Distributable under LGPL license.
     * See terms of license at gnu.org.
     */
    package org.jboss.aop.patterns.observable;

    import java.util.Collections;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;

    /**
     * The subject implementation.
     *
     * @author <a href="adrian@jboss.com">Adrian Brock</a>
     * @version $Revision: 22230 $
     */
    public class SubjectImpl implements Subject
    {
       /** The observers */
       private Set observers = Collections.synchronizedSet(new HashSet());

       /** The subject */
       private Object subject;

       public SubjectImpl(Object subject)
       {
          this.subject = subject;
       }

       public void addObserver(Observer observer)
       {
          observers.add(observer);
       }

       public void removeObserver(Observer observer)
       {
          observers.remove(observer);
       }

       public void notifyObservers()
       {
          Subject obj = (Subject) subject;

          synchronized (observers)
          {
             for (Iterator i = observers.iterator(); i.hasNext();)
             {
                Observer observer = (Observer) i.next();
                observer.fireChange(obj);
             }
          }
       }
    }