01: /* Licensed to the Apache Software Foundation (ASF) under one or more
02: * contributor license agreements. See the NOTICE file distributed with
03: * this work for additional information regarding copyright ownership.
04: * The ASF licenses this file to You under the Apache License, Version 2.0
05: * (the "License"); you may not use this file except in compliance with
06: * the License. You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.apache.harmony.luni.platform;
18:
19: import java.util.ArrayList;
20: import java.util.HashMap;
21: import java.util.Iterator;
22: import java.util.List;
23:
24: /**
25: * Adapter Manager
26: *
27: */
28: final class AdapterManager implements IAdapterManager {
29:
30: /*
31: * key is adaptable type, and value is list of adapter factories for that
32: * type.
33: */
34: private final HashMap<Class, List<IAdapterFactory>> factories = new HashMap<Class, List<IAdapterFactory>>();
35:
36: public Object getAdapter(IAdaptable adaptable, Class adapterType) {
37: List factoryList = factories.get(adapterType);
38: if (factoryList != null) {
39: for (Iterator factoryItr = factoryList.iterator(); factoryItr
40: .hasNext();) {
41: IAdapterFactory factory = (IAdapterFactory) factoryItr
42: .next();
43: Object adapter = factory.getAdapter(adaptable,
44: adapterType);
45: if (adapter != null) {
46: return adapter;
47: }
48: }
49: }
50: return null;
51: }
52:
53: public boolean hasAdapter(IAdaptable adaptable, Class adapterType) {
54: return null == getAdapter(adaptable, adapterType);
55: }
56:
57: public void registerAdapters(IAdapterFactory factory,
58: Class adaptable) {
59: List<IAdapterFactory> factoryList = factories.get(adaptable);
60: if (factoryList == null) {
61: factoryList = new ArrayList<IAdapterFactory>();
62: factories.put(adaptable, factoryList);
63: }
64: factoryList.add(factory);
65: }
66:
67: public void unregisterAdapters(IAdapterFactory factory,
68: Class adaptable) {
69: List factoryList = factories.get(adaptable);
70: if (factoryList != null) {
71: factoryList.remove(factory);
72: }
73: }
74:
75: public void unregisterAdapters(IAdapterFactory factory) {
76: for (Iterator<Class> knownAdaptablesItr = factories.keySet()
77: .iterator(); knownAdaptablesItr.hasNext();) {
78: Class adaptable = knownAdaptablesItr.next();
79: unregisterAdapters(factory, adaptable);
80: }
81: }
82: }
|