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 | ||
40 | private TmfEventTypeManager() { | |
41 | fEventTypes = new HashMap<String, HashMap<String, ITmfEventType>>(); | |
42 | } | |
43 | ||
44 | public static synchronized TmfEventTypeManager getInstance() { | |
45 | if (fEventTypeManager == null) { | |
46 | fEventTypeManager = new TmfEventTypeManager(); | |
47 | } | |
48 | return fEventTypeManager; | |
49 | } | |
50 | ||
51 | // ------------------------------------------------------------------------ | |
52 | // Operations | |
53 | // ------------------------------------------------------------------------ | |
54 | ||
55 | /** | |
56 | * Add a context:type pair to the available types | |
57 | * | |
58 | * @param context the target context | |
59 | * @param type the type to add | |
60 | */ | |
61 | public synchronized void add(String context, ITmfEventType type) { | |
62 | HashMap<String, ITmfEventType> types = fEventTypes.get(context); | |
63 | if (types == null) { | |
64 | types = new HashMap<String, ITmfEventType>(); | |
65 | } | |
4c564a2d | 66 | types.put(type.getName(), type); |
d06468c7 FC |
67 | fEventTypes.put(context, types); |
68 | } | |
69 | ||
70 | /** | |
71 | * Return the list of currently defined contexts | |
72 | * | |
73 | * @return the list of contexts | |
74 | */ | |
75 | public synchronized String[] getContexts() { | |
76 | return fEventTypes.keySet().toArray(new String[fEventTypes.size()]); | |
77 | } | |
78 | ||
79 | /** | |
80 | * Return the list of types defined for a given context | |
81 | * | |
82 | * @param context the context to look into | |
83 | * @return the list of types defined for that context | |
84 | */ | |
85 | public synchronized ITmfEventType[] getTypes(String context) { | |
86 | HashMap<String, ITmfEventType> types = fEventTypes.get(context); | |
87 | if (types != null) { | |
88 | return types.values().toArray(new ITmfEventType[types.size()]); | |
89 | } | |
90 | return null; | |
91 | } | |
92 | ||
93 | /** | |
94 | * Return an event type | |
95 | * | |
96 | * @param context the context to look into | |
97 | * @param typeId the type ID | |
98 | * @return the corresponding type | |
99 | */ | |
100 | public synchronized ITmfEventType getType(String context, String typeId) { | |
101 | HashMap<String, ITmfEventType> types = fEventTypes.get(context); | |
102 | if (types != null) { | |
103 | return types.get(typeId); | |
104 | } | |
105 | return null; | |
106 | } | |
107 | ||
108 | /** | |
109 | * Remove the types associated to a context | |
110 | * | |
111 | * @param context the context to remove | |
112 | */ | |
113 | public synchronized void clear(String context) { | |
114 | fEventTypes.remove(context); | |
115 | } | |
116 | ||
117 | /** | |
118 | * Remove all contexts and types | |
119 | */ | |
120 | public synchronized void clear() { | |
121 | fEventTypes.clear(); | |
122 | } | |
123 | ||
124 | // ------------------------------------------------------------------------ | |
125 | // Object | |
126 | // ------------------------------------------------------------------------ | |
127 | ||
128 | @Override | |
129 | @SuppressWarnings("nls") | |
130 | public String toString() { | |
131 | return "TmfEventTypeManager [fEventTypes=" + fEventTypes + "]"; | |
132 | } | |
133 | ||
134 | } |