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