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; | |
b9e37ffd | 16 | import java.util.Map; |
d06468c7 FC |
17 | |
18 | /** | |
f7703ed6 FC |
19 | * A central repository for the available event types. Types are managed by |
20 | * context space. | |
b9e37ffd | 21 | * |
b9e37ffd FC |
22 | * @version 1.0 |
23 | * @author Francois Chouinard | |
f7703ed6 | 24 | * |
b9e37ffd | 25 | * @see ITmfEventType |
d06468c7 FC |
26 | */ |
27 | public final class TmfEventTypeManager { | |
28 | ||
29 | // ------------------------------------------------------------------------ | |
30 | // Attributes | |
31 | // ------------------------------------------------------------------------ | |
32 | ||
33 | // The event type manager singleton | |
34 | private static TmfEventTypeManager fEventTypeManager = null; | |
35 | ||
36 | // The available types, per context | |
b9e37ffd | 37 | private final Map<String, HashMap<String, ITmfEventType>> fEventTypes; |
d06468c7 FC |
38 | |
39 | // ------------------------------------------------------------------------ | |
40 | // Constructors | |
41 | // ------------------------------------------------------------------------ | |
42 | ||
d7dbf09a FC |
43 | /** |
44 | * The singleton constructor | |
45 | */ | |
d06468c7 FC |
46 | private TmfEventTypeManager() { |
47 | fEventTypes = new HashMap<String, HashMap<String, ITmfEventType>>(); | |
48 | } | |
49 | ||
d7dbf09a FC |
50 | /** |
51 | * @return the TmfEventTypeManager singleton | |
52 | */ | |
d06468c7 | 53 | public static synchronized TmfEventTypeManager getInstance() { |
b9e37ffd | 54 | if (fEventTypeManager == null) { |
d06468c7 | 55 | fEventTypeManager = new TmfEventTypeManager(); |
b9e37ffd | 56 | } |
d06468c7 FC |
57 | return fEventTypeManager; |
58 | } | |
59 | ||
60 | // ------------------------------------------------------------------------ | |
61 | // Operations | |
62 | // ------------------------------------------------------------------------ | |
63 | ||
64 | /** | |
65 | * Add a context:type pair to the available types | |
66 | * | |
67 | * @param context the target context | |
68 | * @param type the type to add | |
69 | */ | |
085d898f | 70 | public synchronized void add(final String context, final ITmfEventType type) { |
d06468c7 | 71 | HashMap<String, ITmfEventType> types = fEventTypes.get(context); |
b9e37ffd | 72 | if (types == null) { |
d06468c7 | 73 | types = new HashMap<String, ITmfEventType>(); |
b9e37ffd | 74 | } |
4c564a2d | 75 | types.put(type.getName(), type); |
d06468c7 FC |
76 | fEventTypes.put(context, types); |
77 | } | |
78 | ||
79 | /** | |
80 | * Return the list of currently defined contexts | |
81 | * | |
82 | * @return the list of contexts | |
83 | */ | |
84 | public synchronized String[] getContexts() { | |
85 | return fEventTypes.keySet().toArray(new String[fEventTypes.size()]); | |
86 | } | |
87 | ||
88 | /** | |
89 | * Return the list of types defined for a given context | |
90 | * | |
91 | * @param context the context to look into | |
92 | * @return the list of types defined for that context | |
93 | */ | |
085d898f FC |
94 | public synchronized ITmfEventType[] getTypes(final String context) { |
95 | final HashMap<String, ITmfEventType> types = fEventTypes.get(context); | |
b9e37ffd | 96 | if (types != null) { |
d06468c7 | 97 | return types.values().toArray(new ITmfEventType[types.size()]); |
b9e37ffd | 98 | } |
8b63ffb9 | 99 | return new ITmfEventType[0]; |
d06468c7 FC |
100 | } |
101 | ||
102 | /** | |
103 | * Return an event type | |
104 | * | |
105 | * @param context the context to look into | |
106 | * @param typeId the type ID | |
107 | * @return the corresponding type | |
108 | */ | |
085d898f FC |
109 | public synchronized ITmfEventType getType(final String context, final String typeId) { |
110 | final HashMap<String, ITmfEventType> types = fEventTypes.get(context); | |
b9e37ffd | 111 | if (types != null) { |
d06468c7 | 112 | return types.get(typeId); |
b9e37ffd | 113 | } |
d06468c7 FC |
114 | return null; |
115 | } | |
116 | ||
117 | /** | |
118 | * Remove the types associated to a context | |
119 | * | |
120 | * @param context the context to remove | |
121 | */ | |
085d898f | 122 | public synchronized void clear(final String context) { |
d06468c7 FC |
123 | fEventTypes.remove(context); |
124 | } | |
125 | ||
126 | /** | |
127 | * Remove all contexts and types | |
128 | */ | |
129 | public synchronized void clear() { | |
130 | fEventTypes.clear(); | |
131 | } | |
132 | ||
133 | // ------------------------------------------------------------------------ | |
134 | // Object | |
135 | // ------------------------------------------------------------------------ | |
136 | ||
d7dbf09a FC |
137 | /* (non-Javadoc) |
138 | * @see java.lang.Object#toString() | |
139 | */ | |
d06468c7 FC |
140 | @Override |
141 | @SuppressWarnings("nls") | |
142 | public String toString() { | |
143 | return "TmfEventTypeManager [fEventTypes=" + fEventTypes + "]"; | |
144 | } | |
145 | ||
146 | } |