tmf: Add new interface to get pre-defined data types
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / event / TmfEventTypeManager.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 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 import java.util.Map;
17 import java.util.Set;
18
19 import com.google.common.collect.ImmutableSet;
20
21 /**
22 * A central repository for the available event types. Types are managed by
23 * context space.
24 *
25 * @version 1.0
26 * @author Francois Chouinard
27 *
28 * @see ITmfEventType
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
40 private final Map<String, HashMap<String, ITmfEventType>> fEventTypes;
41
42 // ------------------------------------------------------------------------
43 // Constructors
44 // ------------------------------------------------------------------------
45
46 /**
47 * The singleton constructor
48 */
49 private TmfEventTypeManager() {
50 fEventTypes = new HashMap<>();
51 }
52
53 /**
54 * @return the TmfEventTypeManager singleton
55 */
56 public static synchronized TmfEventTypeManager getInstance() {
57 if (fEventTypeManager == null) {
58 fEventTypeManager = new TmfEventTypeManager();
59 }
60 return fEventTypeManager;
61 }
62
63 // ------------------------------------------------------------------------
64 // Operations
65 // ------------------------------------------------------------------------
66
67 /**
68 * Add a context:type pair to the available types
69 *
70 * @param context the target context
71 * @param type the type to add
72 */
73 public synchronized void add(final String context, final ITmfEventType type) {
74 HashMap<String, ITmfEventType> types = fEventTypes.get(context);
75 if (types == null) {
76 types = new HashMap<>();
77 }
78 types.put(type.getName(), type);
79 fEventTypes.put(context, types);
80 }
81
82 /**
83 * Return the list of currently defined contexts
84 *
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
93 *
94 * @param context the context to look into
95 * @return the list of types defined for that context
96 * @since 3.0
97 */
98 public synchronized Set<ITmfEventType> getTypes(final String context) {
99 final HashMap<String, ITmfEventType> types = fEventTypes.get(context);
100 if (types != null) {
101 return ImmutableSet.copyOf(types.values());
102 }
103 return ImmutableSet.of();
104 }
105
106 /**
107 * Return an event type
108 *
109 * @param context the context to look into
110 * @param typeId the type ID
111 * @return the corresponding type
112 */
113 public synchronized ITmfEventType getType(final String context, final String typeId) {
114 final HashMap<String, ITmfEventType> types = fEventTypes.get(context);
115 if (types != null) {
116 return types.get(typeId);
117 }
118 return null;
119 }
120
121 /**
122 * Remove the types associated to a context
123 *
124 * @param context the context to remove
125 */
126 public synchronized void clear(final String context) {
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 }
This page took 0.03326 seconds and 5 git commands to generate.