package com.hammurapi.common;

/**
 * Converts argument to com.hammurapi.common.Observable. If argument is instance
 * of com.hammurapi.common.Observable, it is returned as is. If argument is instance 
 * of java.util.Observable, it is adapted to com.hammurapi.common.Observable. If argument
 * is instance of com.hammurapi.common.Adaptable, then its getAdapter() method is invoked to
 * get com.hammurapi.common.Observable adapter.
 * @author Pavel Vlasov
 *
 * @param <T>
 */
public class SimpleObservableConverter<T> implements ObservableConverter<T> {

	/**
	 * @param obj
	 * @return Observable or null if argument cannot be converted to observable.
	 */
	@Override
	public Observable<T> convert(T obj) {
		if (obj instanceof Observable) {
			return (Observable<T>) obj;
		}
		
		if (obj instanceof java.util.Observable) {
			return (Observable<T>) new ObservableAdapter((java.util.Observable) obj);
		}
		
		if (obj instanceof Adaptable) {
			return ((Adaptable) obj).getAdapter(Observable.class, DefaultContext.INSTANCE);
		}
		
		return null;
	}

}
