    001package com.hammurapi.common;
    002
    003import java.util.Map;
    004import java.util.concurrent.ConcurrentHashMap;
    005
    006/**
    007 * Wraps java.util.Observable into com.hammurapi.Observable.
    008 * @author Pavel Vlasov
    009 *
    010 */
    011public class ObservableAdapter implements Observable<java.util.Observable> {
    012
    013        private java.util.Observable observable;
    014
    015
    016        public ObservableAdapter(java.util.Observable observable) {
    017                this.observable = observable;
    018        }
    019
    020        private Map<Observer<?>, java.util.Observer> observerMap = new ConcurrentHashMap<Observer<?>, java.util.Observer>();
    021
    022
    023        @Override
    024        public void addObserver(final Observer<? super java.util.Observable> observer) {
    025                java.util.Observer uo = new java.util.Observer() {
    026
    027                                        @Override
    028                                        public void update(java.util.Observable o, Object arg) {
    029                                                observer.update(observable);
    030                                        }
    031
    032                                };
    033                observerMap.put(observer, uo);
    034        }
    035
    036        @Override
    037        public void deleteObserver(Observer<? super java.util.Observable> o) {
    038                observable.deleteObserver(observerMap.remove(o));
    039        }
    040}