1 //---------------------------------------------------------------------- 2 // 3 // PerfectJPattern: "Design patterns are good but components are better!" 4 // Subject.java Copyright (c) 2007 Giovanni Azua Garcia 5 // bravegag@hotmail.com 6 // 7 // This program is free software; you can redistribute it and/or 8 // modify it under the terms of the GNU General Public License 9 // as published by the Free Software Foundation; either version 2 10 // of the License, or (at your option) any later version. 11 // 12 // This program is distributed in the hope that it will be useful, 13 // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 // GNU General Public License for more details. 16 // 17 // You should have received a copy of the GNU General Public License 18 // along with this program; if not, write to the Free Software 19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 20 // MA 02110-1301, USA. 21 // 22 //---------------------------------------------------------------------- 23 package perfectjpattern.core.behavioral.observer; 24 25 import java.util.ArrayList; 26 import java.util.Arrays; 27 import java.util.Collection; 28 29 import org.apache.commons.lang.Validate; 30 31 import perfectjpattern.core.api.behavioral.observer.IObserver; 32 import perfectjpattern.core.api.behavioral.observer.ISubject; 33 import perfectjpattern.core.api.behavioral.observer.data.IEventData; 34 35 /*** 36 * Base core implementation of <code>ISubject</code> interface. 37 * <br> 38 * 39 * @see ISubject 40 * 41 * @param <E> Type of event data this <code>ISubject</code> 42 * notifies with. 43 * 44 * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a> 45 * @version $Revision: 1.0 $ $Date: Jun 18, 2007 9:06:39 PM $ 46 */ 47 public 48 class Subject<E extends IEventData> 49 implements ISubject<E> 50 { 51 //------------------------------------------------------------------------ 52 // public 53 //------------------------------------------------------------------------ 54 /* (non-Javadoc) 55 * @see perfectjpattern.core.api.behavioral.observer.ISubject# 56 * attach(perfectjpattern.core.api.behavioral.observer.IObserver<E>[]) 57 */ 58 public void 59 attach(IObserver<E>... anObservers) 60 { 61 Validate.notNull(anObservers, "'anObservers' must not be null."); 62 63 theObservers.addAll(Arrays.asList(anObservers)); 64 } 65 66 //------------------------------------------------------------------------ 67 /* (non-Javadoc) 68 * @see perfectjpattern.core.api.behavioral.observer. 69 * ISubject#clear() 70 */ 71 public void 72 clear() 73 { 74 theObservers.clear(); 75 } 76 77 //------------------------------------------------------------------------ 78 /* (non-Javadoc) 79 * @see perfectjpattern.core.api.behavioral.observer.ISubject# 80 * detach(perfectjpattern.core.api.behavioral.observer.IObserver<E>[]) 81 */ 82 public void 83 detach(IObserver<E>... anObservers) 84 { 85 Validate.notNull(anObservers, "'anObservers' must not be null."); 86 87 theObservers.removeAll(Arrays.asList(anObservers)); 88 } 89 90 //------------------------------------------------------------------------ 91 /* (non-Javadoc) 92 * @see perfectjpattern.core.api.behavioral.observer.ISubject# 93 * notifyObservers(perfectjpattern.core.api.behavioral.observer.data. 94 * IEventData) 95 */ 96 public void 97 notifyObservers(E anEventData) 98 { 99 Validate.notNull(anEventData, "'anEventData' must not be null."); 100 101 for (IObserver<E> myObserver : theObservers) 102 { 103 myObserver.update(anEventData); 104 } 105 } 106 107 //------------------------------------------------------------------------ 108 /* (non-Javadoc) 109 * @see perfectjpattern.core.api.behavioral.observer.ISubject#size() 110 */ 111 public int 112 size() 113 { 114 return theObservers.size(); 115 } 116 117 //------------------------------------------------------------------------ 118 // members 119 //------------------------------------------------------------------------ 120 /*** 121 * Collection of <code>IObserver</code> instances. 122 */ 123 private final Collection<IObserver<E>> theObservers = 124 new ArrayList<IObserver<E>>(); 125 }