tmf: Set ITmfTraceWithPreDefinedEvents to @NonNull
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / trace / TmfTraceAdapterManager.java
CommitLineData
7a732e67
PT
1/*******************************************************************************
2 * Copyright (c) 2015 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 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.tracecompass.tmf.core.trace;
14
15import java.util.ArrayList;
16import java.util.Collection;
17import java.util.List;
18import java.util.Map.Entry;
19
20import org.eclipse.core.runtime.IAdapterFactory;
4c4e2816 21import org.eclipse.jdt.annotation.Nullable;
7a732e67
PT
22
23import com.google.common.collect.HashMultimap;
24import com.google.common.collect.Multimap;
25
26/**
27 * This class manages adapter factories for traces. An adapter can be specific
28 * to a given trace type id, or to traces of a given trace class.
29 *
30 * @since 2.0
31 */
32public class TmfTraceAdapterManager {
33
34 private static Multimap<String, IAdapterFactory> fFactoriesById = HashMultimap.create();
35 private static Multimap<Class<? extends ITmfTrace>, IAdapterFactory> fFactoriesByClass = HashMultimap.create();
36
37 /**
38 * Registers the given adapter factory as extending traces with the given
39 * trace type id.
40 * </p>
41 *
42 * @param factory
43 * the adapter factory
44 * @param traceTypeId
45 * the trace type id of traces being extended
46 */
47 public static void registerFactory(IAdapterFactory factory, String traceTypeId) {
48 fFactoriesById.put(traceTypeId, factory);
49 }
50
51 /**
52 * Registers the given adapter factory as extending traces of the given
53 * class.
54 * <p>
55 * If the trace class being extended is a class, the given factory's
56 * adapters are available on instances of that class and any of its
57 * subclasses. If it is an interface, the adapters are available to all
58 * classes that directly or indirectly implement that interface.
59 * </p>
60 *
61 * @param factory
62 * the adapter factory
63 * @param traceClass
64 * the class of traces being extended
65 */
66 public static void registerFactory(IAdapterFactory factory, Class<? extends ITmfTrace> traceClass) {
67 fFactoriesByClass.put(traceClass, factory);
68 }
69
70 /**
71 * Removes the given adapter factory completely from the list of registered
72 * factories.
73 *
74 * @param factory
75 * the adapter factory to remove
76 * @see #registerFactory(IAdapterFactory, Class)
77 * @see #registerFactory(IAdapterFactory, String)
78 */
79 public static void unregisterFactory(IAdapterFactory factory) {
80 fFactoriesById.values().remove(factory);
81 fFactoriesByClass.values().remove(factory);
82 }
83
84 /**
85 * Returns a list of object which are instances of the given class
86 * associated with the given trace. Returns an empty list if no such object
87 * can be found.
88 * <p>
89 *
90 * @param trace
91 * the trace being queried
92 * @param adapterType
93 * the type of adapter to look up
94 * @return a list of objects of the given adapter type
95 */
96 public static <T> List<T> getAdapters(ITmfTrace trace, Class<T> adapterType) {
97 Collection<IAdapterFactory> factoriesById = fFactoriesById.get(trace.getTraceTypeId());
98 Collection<Entry<Class<? extends ITmfTrace>, IAdapterFactory>> entries = fFactoriesByClass.entries();
99 List<T> adapters = new ArrayList<>(factoriesById.size() + entries.size());
100 for (IAdapterFactory factory : factoriesById) {
4c4e2816 101 @Nullable T adapter = factory.getAdapter(trace, adapterType);
7a732e67
PT
102 if (adapter != null) {
103 adapters.add(adapter);
104 }
105 }
106 for (Entry<Class<? extends ITmfTrace>, IAdapterFactory> entry : entries) {
107 if (entry.getKey().isInstance(trace)) {
4c4e2816 108 @Nullable T adapter = entry.getValue().getAdapter(trace, adapterType);
7a732e67
PT
109 if (adapter != null) {
110 adapters.add(adapter);
111 }
112 }
113 }
114 return adapters;
115 }
116
117}
This page took 0.035082 seconds and 5 git commands to generate.