Skip to main content
summaryrefslogtreecommitdiffstats
blob: a9feaa406f17bfcb61fc938464c3d69587193a90 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*******************************************************************************
 * Copyright (c) 2009 Oracle Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Ian Trimble - initial API and implementation
 *******************************************************************************/ 
package org.eclipse.jst.pagedesigner.dtresourceprovider;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.jst.jsf.common.ui.internal.logging.Logger;
import org.eclipse.jst.pagedesigner.PDPlugin;

/**
 * Used to Map a String ID to a List of Classes and provide convenient access.
 * 
 * @author Ian Trimble - Oracle
 */
public class ProviderClassMap {

	private Map<String, List<Class>> idToClassListMap;
	private Logger log = PDPlugin.getLogger(ProviderClassMap.class);

	/**
	 * Creates an instance.
	 */
	public ProviderClassMap() {
		idToClassListMap = new HashMap<String, List<Class>>();
	}

	/**
	 * Gets the List of Classes associated with the specified ID.
	 * 
	 * @param id ID for which to get the List.
	 * @return The List of Classes associated with the specified ID. List may be
	 * empty but will not be null.
	 */
	public List<Class> get(String id) {
		List<Class> classList = idToClassListMap.get(id);
		if (classList == null) {
			classList = new ArrayList<Class>();
			idToClassListMap.put(id, classList);
		}
		return classList;
	}

	/**
	 * Adds the specified Class to the List associated with the specified ID.
	 * 
	 * @param id ID for which to associate the specified Class.
	 * @param clazz Class to add to the List associated with the specified ID.
	 * @return true (as per the general contract of the Collection.add method). 
	 */
	public boolean add(String id, Class clazz) {
		List<Class> classList = get(id);
		return classList.add(clazz);
	}

	/**
	 * Instantiates all classes associated with the specified ID and returns a
	 * List of IDTResourceProvider instances.
	 * 
	 * @param id ID for which to instantiate all Classes.
	 * @return a List of IDTResourceProvider instances associated with the
	 * specified ID. List may be empty but will not be null.
	 */
	public List<IDTResourceProvider> createInstances(String id) {
		List<IDTResourceProvider> providers = new ArrayList<IDTResourceProvider>();
		List<Class> classList = get(id);
		for (Class providerClass: classList) {
			try {
				Object objProvider = providerClass.newInstance();
				if (objProvider instanceof IDTResourceProvider) {
					providers.add((IDTResourceProvider)objProvider);
				} else {
					log.error("Warning.ProviderClassMap.NotIDTResourceProvider", providerClass.getName()); //$NON-NLS-1$
				}
			} catch(IllegalAccessException iae) {
				log.error("Warning.ProviderClassMap.IllegalAccess", providerClass.getName(), id, iae); //$NON-NLS-1$
			} catch(InstantiationException ie) {
				log.error("Warning.ProviderClassMap.Instantiation", providerClass.getName(), id, ie); //$NON-NLS-1$
			}
		}
		return providers;
	}

	/**
	 * Instantiates all registered IDTResourceProviders and returns as a List.
	 * 
	 * @return List of all registered IDTResourceProviders. List may be empty
	 * but will not be null.
	 */
	public List<IDTResourceProvider> createInstances() {
		List<IDTResourceProvider> providers = new ArrayList<IDTResourceProvider>();
		Set<String> idSet = idToClassListMap.keySet();
		for (String curId: idSet) {
			List<IDTResourceProvider> providersForCurId = createInstances(curId);
			providers.addAll(providersForCurId);
		}
		return providers;
	}

}

Back to the top