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;
27 * Implements subscription and notification logic for an observer pattern. This
28 * class is thread safe.
30 public class Observable<ObservableType, Event> {
32 private static final Logger LOGGER = Logger.getLogger(Observable.class);
37 private ObservableType _observable;
40 * Used to notify observers.
42 private ObserverNotifier<ObservableType, Event> _notifier;
45 * Map of subscription to observer.
47 private Map<Long, Observer<ObservableType, Event>> _observers;
50 * Counter for subscriptions. Holds the next subscription.
52 private long _counter;
55 * Constructs the observable.
58 * Observable this instance is used for.
60 * Object used for implementing notification of listeners.
62 public Observable(ObservableType aObservable,
63 ObserverNotifier<ObservableType, Event> aNotifier) {
64 _observable = aObservable;
65 _notifier = aNotifier;
66 _observers = new TreeMap<Long, Observer<ObservableType, Event>>();
71 * Subscribe an obvers.
74 * Observer to subscribe.
75 * @return Event Event to send.
77 public synchronized long subscribe(Observer<ObservableType, Event> aObserver) {
78 long subscription = _counter++; // integer rage is so large it will
80 _observers.put(subscription, aObserver);
85 * Unsubscribe an observer.
87 * @param aSubscription
88 * Subscription which is used
89 * @throws IllegalArgumentException
90 * In case the subscription is not known.
92 public synchronized void unsubscribe(long aSubscription) {
93 Object obj = _observers.remove(aSubscription);
95 throw new IllegalArgumentException("Subscription '" + aSubscription
101 * Gets the number of subscribed observers.
103 * @return Number of subscribed observers.
105 public int getObserverCount() {
106 return _observers.size();
110 * Notifies all subscribed observers.
115 public void send(Event aEvent) {
116 // Make sure we do the notification while not holding the lock to avoid
117 // potential deadlock
119 List<Observer<ObservableType, Event>> observers = new ArrayList<Observer<ObservableType, Event>>();
120 synchronized (this) {
121 observers.addAll(_observers.values());
123 for (Observer<ObservableType, Event> observer : observers) {
124 _notifier.update(observer, _observable, aEvent);
131 * @see java.lang.Object#finalize()
134 protected void finalize() throws Throwable {
135 if (_observers.size() > 0) {
137 .error("Still observers registered at finalization of observer!");
138 for (Observer observer : _observers.values()) {
139 LOGGER.error(" observer: " + observer);