From 7a732e673423cdb1ed49e7d6051a3fab09373775 Mon Sep 17 00:00:00 2001 From: Patrick Tasse Date: Fri, 23 Oct 2015 18:55:05 -0400 Subject: [PATCH] tmf: Add TmfTraceAdapterManager This adapter manager can handle adapters that are specific to a particular trace type id, to provide specific adapters even if the trace instance is of the same class. Unlike AdapterManager, this manager will return a list of all matching adapters that adapt the given trace. Change-Id: I4c8d6e6d725cf80a2e9ab865ea3217ae05f8cc73 Signed-off-by: Patrick Tasse Reviewed-on: https://git.eclipse.org/r/58863 Reviewed-by: Hudson CI --- .../core/trace/TmfTraceAdapterManager.java | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/trace/TmfTraceAdapterManager.java diff --git a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/trace/TmfTraceAdapterManager.java b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/trace/TmfTraceAdapterManager.java new file mode 100644 index 0000000000..7baceda683 --- /dev/null +++ b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/trace/TmfTraceAdapterManager.java @@ -0,0 +1,116 @@ +/******************************************************************************* + * Copyright (c) 2015 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Patrick Tasse - Initial API and implementation + *******************************************************************************/ + +package org.eclipse.tracecompass.tmf.core.trace; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map.Entry; + +import org.eclipse.core.runtime.IAdapterFactory; + +import com.google.common.collect.HashMultimap; +import com.google.common.collect.Multimap; + +/** + * This class manages adapter factories for traces. An adapter can be specific + * to a given trace type id, or to traces of a given trace class. + * + * @since 2.0 + */ +public class TmfTraceAdapterManager { + + private static Multimap fFactoriesById = HashMultimap.create(); + private static Multimap, IAdapterFactory> fFactoriesByClass = HashMultimap.create(); + + /** + * Registers the given adapter factory as extending traces with the given + * trace type id. + *

+ * + * @param factory + * the adapter factory + * @param traceTypeId + * the trace type id of traces being extended + */ + public static void registerFactory(IAdapterFactory factory, String traceTypeId) { + fFactoriesById.put(traceTypeId, factory); + } + + /** + * Registers the given adapter factory as extending traces of the given + * class. + *

+ * If the trace class being extended is a class, the given factory's + * adapters are available on instances of that class and any of its + * subclasses. If it is an interface, the adapters are available to all + * classes that directly or indirectly implement that interface. + *

+ * + * @param factory + * the adapter factory + * @param traceClass + * the class of traces being extended + */ + public static void registerFactory(IAdapterFactory factory, Class traceClass) { + fFactoriesByClass.put(traceClass, factory); + } + + /** + * Removes the given adapter factory completely from the list of registered + * factories. + * + * @param factory + * the adapter factory to remove + * @see #registerFactory(IAdapterFactory, Class) + * @see #registerFactory(IAdapterFactory, String) + */ + public static void unregisterFactory(IAdapterFactory factory) { + fFactoriesById.values().remove(factory); + fFactoriesByClass.values().remove(factory); + } + + /** + * Returns a list of object which are instances of the given class + * associated with the given trace. Returns an empty list if no such object + * can be found. + *

+ * + * @param trace + * the trace being queried + * @param adapterType + * the type of adapter to look up + * @return a list of objects of the given adapter type + */ + public static List getAdapters(ITmfTrace trace, Class adapterType) { + Collection factoriesById = fFactoriesById.get(trace.getTraceTypeId()); + Collection, IAdapterFactory>> entries = fFactoriesByClass.entries(); + List adapters = new ArrayList<>(factoriesById.size() + entries.size()); + for (IAdapterFactory factory : factoriesById) { + T adapter = factory.getAdapter(trace, adapterType); + if (adapter != null) { + adapters.add(adapter); + } + } + for (Entry, IAdapterFactory> entry : entries) { + if (entry.getKey().isInstance(trace)) { + T adapter = entry.getValue().getAdapter(trace, adapterType); + if (adapter != null) { + adapters.add(adapter); + } + } + } + return adapters; + } + +} -- 2.34.1