View Javadoc

1   /* This file is part of COPAL (COntext Provisioning for All).
2    *
3    * COPAL is a part of SM4All (Smart hoMes for All) project.
4    *
5    * COPAL is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * COPAL is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with COPAL. If not, see <http://www.gnu.org/licenses/>.
17   */
18  package at.ac.tuwien.infosys.sm4all.copal.api.util;
19  
20  import java.util.Collection;
21  import java.util.Collections;
22  import java.util.LinkedList;
23  import java.util.List;
24  import java.util.concurrent.CopyOnWriteArrayList;
25  import at.ac.tuwien.infosys.sm4all.copal.api.query.QueryObserver;
26  
27  /**
28   * The class which represents an object that can be observed.
29   * 
30   * @param <E> the type of change represented as an {@link Enum}.
31   * @param <T> the type of changed object.
32   * @param <O> the type of {@link Observer} that can attach and detach to this
33   *        {@link BaseObservable}.
34   * @author sanjin
35   */
36  public abstract class BaseObservable<E extends Enum<?>, T, O extends Observer<E, T>>
37          implements Observable<E, T, O> {
38  
39      private final List<O> observers = new CopyOnWriteArrayList<O>();
40  
41      /**
42       * Attaches specified {@link Observer} so it will in future receive
43       * notifications on changes.
44       * 
45       * @param observer the {@link Observer}.
46       * @throws NullPointerException if specified observer is <code>null</code>.
47       */
48      @Override
49      public void attach(final O observer) {
50          if (null == observer) {
51              throw new NullPointerException("Observer cannot be null.");
52          }
53  
54          this.observers.add(observer);
55      }
56  
57      /**
58       * Detach specified {@link Observer} so it will not receive any future
59       * notifications on changes.
60       * 
61       * @param observer the {@link QueryObserver}.
62       * @throws NullPointerException if specified observer is <code>null</code>.
63       */
64      @Override
65      public void detach(final O observer) {
66          if (null == observer) {
67              throw new NullPointerException("Observer cannot be null.");
68          }
69  
70          this.observers.remove(observer);
71      }
72  
73      /**
74       * Notifies all attached {@link Observer}s that specified change happened on
75       * specified object.
76       * 
77       * @param change the change.
78       * @param object the changed object.
79       * @throws NullPointerException if specified change is <code>null</code>.
80       */
81      protected void notifyAll(final E change, final T object) {
82          if (null == change) {
83              throw new NullPointerException("Change cannot be null.");
84          }
85  
86          for (final O observer : this.observers) {
87              observer.update(change, object);
88          }
89      }
90  
91      /**
92       * Detaches all attached {@link Observer}s.
93       */
94      protected void detachAll() {
95          this.observers.clear();
96      }
97  
98      /**
99       * Returns all attached {@link Observer}s.
100      * 
101      * @return all attached {@link Observer}s.
102      */
103     protected Collection<O> getObservers() {
104         return Collections.unmodifiableCollection(new LinkedList<O>(
105                 this.observers));
106     }
107 }