package com.hammurapi.common;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Wraps java.util.Observable into com.hammurapi.Observable.
 * @author Pavel Vlasov
 *
 */
public class ObservableAdapter implements Observable<java.util.Observable> {
	
	private java.util.Observable observable;

	
	public ObservableAdapter(java.util.Observable observable) {
		this.observable = observable;
	}
	
	private Map<Observer<?>, java.util.Observer> observerMap = new ConcurrentHashMap<Observer<?>, java.util.Observer>();


	@Override
	public void addObserver(final Observer<? super java.util.Observable> observer) {
		java.util.Observer uo = new java.util.Observer() {
		
					@Override
					public void update(java.util.Observable o, Object arg) {
						observer.update(observable);
					}
					
				};
		observerMap.put(observer, uo);
	}

	@Override
	public void deleteObserver(Observer<? super java.util.Observable> o) {
		observable.deleteObserver(observerMap.remove(o));		
	}
}
