Commit | Line | Data |
---|---|---|
d06468c7 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2012 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.core.event; | |
14 | ||
15 | import java.util.HashMap; | |
16 | ||
17 | /** | |
18 | * <b><u>TmfEventTypeManager</u></b> | |
19 | * <p> | |
20 | * The TmfEventTypeManager acts as a central repository for the available | |
4c564a2d | 21 | * event types. Types are managed in their context space. |
d06468c7 FC |
22 | * <p> |
23 | */ | |
24 | public final class TmfEventTypeManager { | |
25 | ||
26 | // ------------------------------------------------------------------------ | |
27 | // Attributes | |
28 | // ------------------------------------------------------------------------ | |
29 | ||
30 | // The event type manager singleton | |
31 | private static TmfEventTypeManager fEventTypeManager = null; | |
32 | ||
33 | // The available types, per context | |
34 | private HashMap<String, HashMap<String, ITmfEventType>> fEventTypes; | |
35 | ||
36 | // ------------------------------------------------------------------------ | |
37 | // Constructors | |
38 | // ------------------------------------------------------------------------ | |
39 | ||
d7dbf09a FC |
40 | /** |
41 | * The singleton constructor | |
42 | */ | |
d06468c7 FC |
43 | private TmfEventTypeManager() { |
44 | fEventTypes = new HashMap<String, HashMap<String, ITmfEventType>>(); | |
45 | } | |
46 | ||
d7dbf09a FC |
47 | /** |
48 | * @return the TmfEventTypeManager singleton | |
49 | */ | |
d06468c7 FC |
50 | public static synchronized TmfEventTypeManager getInstance() { |
51 | if (fEventTypeManager == null) { | |
52 | fEventTypeManager = new TmfEventTypeManager(); | |
53 | } | |
54 | return fEventTypeManager; | |
55 | } | |
56 | ||
57 | // ------------------------------------------------------------------------ | |
58 | // Operations | |
59 | // ------------------------------------------------------------------------ | |
60 | ||
61 | /** | |
62 | * Add a context:type pair to the available types | |
63 | * | |
64 | * @param context the target context | |
65 | * @param type the type to add | |
66 | */ | |
67 | public synchronized void add(String context, ITmfEventType type) { | |
68 | HashMap<String, ITmfEventType> types = fEventTypes.get(context); | |
69 | if (types == null) { | |
70 | types = new HashMap<String, ITmfEventType>(); | |
71 | } | |
4c564a2d | 72 | types.put(type.getName(), type); |
d06468c7 FC |
73 | fEventTypes.put(context, types); |
74 | } | |
75 | ||
76 | /** | |
77 | * Return the list of currently defined contexts | |
78 | * | |
79 | * @return the list of contexts | |
80 | */ | |
81 | public synchronized String[] getContexts() { | |
82 | return fEventTypes.keySet().toArray(new String[fEventTypes.size()]); | |
83 | } | |
84 | ||
85 | /** | |
86 | * Return the list of types defined for a given context | |
87 | * | |
88 | * @param context the context to look into | |
89 | * @return the list of types defined for that context | |
90 | */ | |
91 | public synchronized ITmfEventType[] getTypes(String context) { | |
92 | HashMap<String, ITmfEventType> types = fEventTypes.get(context); | |
93 | if (types != null) { | |
94 | return types.values().toArray(new ITmfEventType[types.size()]); | |
95 | } | |
96 | return null; | |
97 | } | |
98 | ||
99 | /** | |
100 | * Return an event type | |
101 | * | |
102 | * @param context the context to look into | |
103 | * @param typeId the type ID | |
104 | * @return the corresponding type | |
105 | */ | |
106 | public synchronized ITmfEventType getType(String context, String typeId) { | |
107 | HashMap<String, ITmfEventType> types = fEventTypes.get(context); | |
108 | if (types != null) { | |
109 | return types.get(typeId); | |
110 | } | |
111 | return null; | |
112 | } | |
113 | ||
114 | /** | |
115 | * Remove the types associated to a context | |
116 | * | |
117 | * @param context the context to remove | |
118 | */ | |
119 | public synchronized void clear(String context) { | |
120 | fEventTypes.remove(context); | |
121 | } | |
122 | ||
123 | /** | |
124 | * Remove all contexts and types | |
125 | */ | |
126 | public synchronized void clear() { | |
127 | fEventTypes.clear(); | |
128 | } | |
129 | ||
130 | // ------------------------------------------------------------------------ | |
131 | // Object | |
132 | // ------------------------------------------------------------------------ | |
133 | ||
d7dbf09a FC |
134 | /* (non-Javadoc) |
135 | * @see java.lang.Object#toString() | |
136 | */ | |
d06468c7 FC |
137 | @Override |
138 | @SuppressWarnings("nls") | |
139 | public String toString() { | |
140 | return "TmfEventTypeManager [fEventTypes=" + fEventTypes + "]"; | |
141 | } | |
142 | ||
143 | } |