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.
17 package org.wamblee.observer;
19 import java.util.List;
20 import java.util.ArrayList;
22 import java.util.TreeMap;
24 import org.apache.log4j.Logger;
28 * Implements subscription and notification logic for an observer pattern.
29 * This class is thread safe.
31 public class Observable<ObservableType, Event> {
33 private static final Logger LOGGER = Logger.getLogger(Observable.class);
38 private ObservableType _observable;
41 * Used to notify observers.
43 private ObserverNotifier<ObservableType,Event> _notifier;
46 * Map of subscription to observer.
48 private Map<Long, Observer<ObservableType,Event>> _observers;
51 * Counter for subscriptions. Holds the next subscription.
53 private long _counter;
56 * Constructs the observable.
57 * @param aObservable Observable this instance is used for.
58 * @param aNotifier Object used for implementing notification of listeners.
60 public Observable(ObservableType aObservable, ObserverNotifier<ObservableType,Event> aNotifier) {
61 _observable = aObservable;
62 _notifier = aNotifier;
63 _observers = new TreeMap<Long, Observer<ObservableType, Event>>();
68 * Subscribe an obvers.
69 * @param aObserver Observer to subscribe.
70 * @return Event Event to send.
72 public synchronized long subscribe(Observer<ObservableType, Event> aObserver) {
73 long subscription = _counter++; // integer rage is so large it will never roll over.
74 _observers.put(subscription, aObserver);
79 * Unsubscribe an observer.
80 * @param aSubscription Subscription which is used
81 * @throws IllegalArgumentException In case the subscription is not known.
83 public synchronized void unsubscribe(long aSubscription) {
84 Object obj = _observers.remove(aSubscription);
86 throw new IllegalArgumentException("Subscription '" + aSubscription + "'");
91 * Gets the number of subscribed observers.
93 public int getObserverCount() {
94 return _observers.size();
98 * Notifies all subscribed observers.
99 * @param aEvent Event to send.
101 public void send(Event aEvent) {
102 // Make sure we do the notification while not holding the lock to avoid potential deadlock
104 List<Observer<ObservableType,Event>> observers = new ArrayList<Observer<ObservableType, Event>>();
105 synchronized (this) {
106 observers.addAll(_observers.values());
108 for (Observer<ObservableType,Event> observer: observers) {
109 _notifier.update(observer, _observable, aEvent);
114 * @see java.lang.Object#finalize()
117 protected void finalize() throws Throwable {
118 if ( _observers.size() > 0 ) {
119 LOGGER.error("Still observers registered at finalization of observer!");
120 for (Observer observer: _observers.values()) {
121 LOGGER.error(" observer: " + observer);