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