001package com.hammurapi.common;
002
003/**
004 * Converts argument to com.hammurapi.common.Observable. If argument is instance
005 * of com.hammurapi.common.Observable, it is returned as is. If argument is instance 
006 * of java.util.Observable, it is adapted to com.hammurapi.common.Observable. If argument
007 * is instance of com.hammurapi.common.Adaptable, then its getAdapter() method is invoked to
008 * get com.hammurapi.common.Observable adapter.
009 * @author Pavel Vlasov
010 *
011 * @param <T>
012 */
013public class SimpleObservableConverter<T> implements ObservableConverter<T> {
014
015        /**
016         * @param obj
017         * @return Observable or null if argument cannot be converted to observable.
018         */
019        @Override
020        public Observable<T> convert(T obj) {
021                if (obj instanceof Observable) {
022                        return (Observable<T>) obj;
023                }
024                
025                if (obj instanceof java.util.Observable) {
026                        return (Observable<T>) new ObservableAdapter((java.util.Observable) obj);
027                }
028                
029                if (obj instanceof Adaptable) {
030                        return ((Adaptable) obj).getAdapter(Observable.class, DefaultContext.INSTANCE);
031                }
032                
033                return null;
034        }
035
036}