2 * Copyright 2005 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.observer;
18 import org.apache.log4j.Logger;
20 import java.util.ArrayList;
21 import java.util.List;
23 import java.util.TreeMap;
27 * Implements subscription and notification logic for an observer pattern. This
28 * class is thread safe.
30 public class Observable<ObservableType, Event> {
31 private static final Logger LOGGER = Logger.getLogger(Observable.class);
36 private ObservableType observable;
39 * Used to notify observers.
41 private ObserverNotifier<ObservableType, Event> notifier;
44 * Map of subscription to observer.
46 private Map<Long, Observer<ObservableType, Event>> observers;
49 * Counter for subscriptions. Holds the next subscription.
54 * Constructs the observable.
57 * Observable this instance is used for.
59 * Object used for implementing notification of listeners.
61 public Observable(ObservableType aObservable,
62 ObserverNotifier<ObservableType, Event> aNotifier) {
63 observable = aObservable;
65 observers = new TreeMap<Long, Observer<ObservableType, Event>>();
70 * Subscribe an obvers.
73 * Observer to subscribe.
74 * @return Event Event to send.
76 public synchronized long subscribe(
77 Observer<ObservableType, Event> aObserver) {
78 long subscription = counter++; // integer rage is so large it will
81 observers.put(subscription, aObserver);
87 * Unsubscribe an observer.
89 * @param aSubscription
90 * Subscription which is used
91 * @throws IllegalArgumentException
92 * In case the subscription is not known.
94 public synchronized void unsubscribe(long aSubscription) {
95 Object obj = observers.remove(aSubscription);
98 throw new IllegalArgumentException("Subscription '" +
104 * Gets the number of subscribed observers.
106 * @return Number of subscribed observers.
108 public int getObserverCount() {
109 return observers.size();
113 * Notifies all subscribed observers.
118 public void send(Event aEvent) {
119 // Make sure we do the notification while not holding the lock to avoid
120 // potential deadlock
122 List<Observer<ObservableType, Event>> myObservers = new ArrayList<Observer<ObservableType, Event>>();
124 synchronized (this) {
125 myObservers.addAll(observers.values());
128 for (Observer<ObservableType, Event> observer : myObservers) {
129 notifier.update(observer, observable, aEvent);
136 * @see java.lang.Object#finalize()
139 protected void finalize() throws Throwable {
140 if (observers.size() > 0) {
142 "Still observers registered at finalization of observer!");
144 for (Observer observer : observers.values()) {
145 LOGGER.error(" observer: " + observer);