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