1
2
3
4
5
6
7
8
9
10
11
12
13
14 package net.sf.jgabl2.adapt.impl;
15
16 import net.sf.jgabl2.adapt.IGenericAdaptable;
17 import net.sf.jgabl2.adapt.IGenericAdapterManager;
18 import net.sf.jgabl2.adapt.UnavailableAdapterException;
19
20 /**
21 * A first implementation of an {@link IGenericAdapterManager}.
22 *
23 * @author Alexander Schwartz
24 * @since 0.5.0
25 */
26 public final class GenericAdapterManagerImpl
27 implements IGenericAdapterManager {
28 /** {@inheritDoc} */
29 @SuppressWarnings("unchecked")
30 public <AdapterType> AdapterType getAdapter(
31 final Object object,
32 final Class<?> type)
33 throws UnavailableAdapterException {
34 if (type.isInstance(object)) {
35 return (AdapterType) object;
36 }
37
38 if (object instanceof IGenericAdaptable) {
39 final IGenericAdaptable adaptable = (IGenericAdaptable) object;
40
41 return adaptable.<AdapterType>getAdapter(type);
42 }
43
44 throw new UnavailableAdapterException("No adapter of type '" + type
45 + "' available for '"
46 + object + "'");
47 }
48 }