1 /*
2 * Copyright (c) 2006-2007 Creative Sphere Limited.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *
10 * Creative Sphere - initial API and implementation
11 *
12 */
13 package org.abstracthorizon.danube.adapter;
14
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.HashSet;
18
19 /**
20 * This class is insipired by the similar from eclipse project
21 *
22 * @author Daniel Sendula
23 */
24 public class AdapterManager implements AdapterFactory {
25
26 /** Parent adapter manager */
27 private AdapterManager parentAdapterManager;
28
29 /** List of factories */
30 private Collection<AdapterFactory> adapterFactories = new ArrayList<AdapterFactory>();
31
32 /**
33 * Constructor
34 */
35 public AdapterManager() {
36 }
37
38 /**
39 * Constructor
40 * @param parentAdapterManager parent adapter manager
41 */
42 public AdapterManager(AdapterManager parentAdapterManager) {
43 this.parentAdapterManager = parentAdapterManager;
44 }
45
46 /**
47 * Returns parent adapter manager
48 * @return parent adapter manager
49 */
50 public AdapterManager getParentAdapterManager() {
51 return parentAdapterManager;
52 }
53
54 /**
55 * Sets parent adapter manager
56 * @param parentAdapterManager parent adapter manager
57 */
58 public void setParentAdapterManager(AdapterManager parentAdapterManager) {
59 this.parentAdapterManager = parentAdapterManager;
60 }
61
62 /**
63 * Returns collection of adapter factories
64 * @return collection of adapter factories
65 */
66 public Collection<AdapterFactory> getAdapterFactories() {
67 return adapterFactories;
68 }
69
70 /**
71 * Sets the collection of adapter factories
72 * @param adapterFactories collection of adapter factories
73 */
74 public void setAdapterFactories(Collection<AdapterFactory> adapterFactories) {
75 this.adapterFactories = adapterFactories;
76 }
77
78 /**
79 * Adopts given object to the instance of the asked class
80 * @param object object to he adopted
81 * @param cls asked class
82 * @return adopted given object to the instance of the asked class
83 */
84 public <T> T adapt(T object, Class<T> cls) {
85 if (adapterFactories != null) {
86 // TODO this can be optimised by caching what classes are available by
87 // what adapters...
88 for (AdapterFactory adapterFactory : adapterFactories) {
89 T res = adapterFactory.adapt(object, cls);
90 if (res != null) {
91 return res;
92 }
93 }
94 }
95 if (parentAdapterManager != null) {
96 return parentAdapterManager.adapt(object, cls);
97 } else {
98 return null;
99 }
100 }
101
102 /**
103 * Returns list of classes to which given object can be adopted to by this adopter factory
104 * @return list of classes to which given object can be adopted to
105 */
106 @SuppressWarnings("unchecked")
107 public <T> Class<T>[] getAdaptingClasses(T object) {
108 HashSet<Class<T>> classes = new HashSet<Class<T>>();
109 if (adapterFactories != null) {
110 for (AdapterFactory adapterFactory : adapterFactories) {
111 Class<T>[] clss = adapterFactory.getAdaptingClasses(object);
112 for (Class<T> c : clss) {
113 classes.add(c);
114 }
115 }
116 }
117 if (parentAdapterManager != null) {
118 Class<T>[] clss = parentAdapterManager.getAdaptingClasses(object);
119 for (Class<T> c : clss) {
120 classes.add(c);
121 }
122 }
123 Class<T>[] res = new Class[classes.size()];
124 res = classes.toArray(res);
125 return res;
126 }
127
128 }