| 1 | package com.hammurapi.common; |
| 2 | |
| 3 | /** |
| 4 | * Converts argument to com.hammurapi.common.Observable. If argument is instance |
| 5 | * of com.hammurapi.common.Observable, it is returned as is. If argument is instance |
| 6 | * of java.util.Observable, it is adapted to com.hammurapi.common.Observable. If argument |
| 7 | * is instance of com.hammurapi.common.Adaptable, then its getAdapter() method is invoked to |
| 8 | * get com.hammurapi.common.Observable adapter. |
| 9 | * @author Pavel Vlasov |
| 10 | * |
| 11 | * @param <T> |
| 12 | */ |
| 13 | public class SimpleObservableConverter<T> implements ObservableConverter<T> { |
| 14 | |
| 15 | /** |
| 16 | * @param obj |
| 17 | * @return Observable or null if argument cannot be converted to observable. |
| 18 | */ |
| 19 | @Override |
| 20 | public Observable<T> convert(T obj) { |
| 21 | if (obj instanceof Observable) { |
| 22 | return (Observable<T>) obj; |
| 23 | } |
| 24 | |
| 25 | if (obj instanceof java.util.Observable) { |
| 26 | return (Observable<T>) new ObservableAdapter((java.util.Observable) obj); |
| 27 | } |
| 28 | |
| 29 | if (obj instanceof Adaptable) { |
| 30 | return ((Adaptable) obj).getAdapter(Observable.class, DefaultContext.INSTANCE); |
| 31 | } |
| 32 | |
| 33 | return null; |
| 34 | } |
| 35 | |
| 36 | } |