1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
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
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.core
.trace
;
15 import java
.util
.ArrayList
;
16 import java
.util
.Collection
;
17 import java
.util
.List
;
18 import java
.util
.Map
.Entry
;
20 import org
.eclipse
.core
.runtime
.IAdapterFactory
;
21 import org
.eclipse
.jdt
.annotation
.Nullable
;
23 import com
.google
.common
.collect
.HashMultimap
;
24 import com
.google
.common
.collect
.Multimap
;
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.
32 public class TmfTraceAdapterManager
{
34 private static Multimap
<String
, IAdapterFactory
> fFactoriesById
= HashMultimap
.create();
35 private static Multimap
<Class
<?
extends ITmfTrace
>, IAdapterFactory
> fFactoriesByClass
= HashMultimap
.create();
38 * Registers the given adapter factory as extending traces with the given
45 * the trace type id of traces being extended
47 public static void registerFactory(IAdapterFactory factory
, String traceTypeId
) {
48 fFactoriesById
.put(traceTypeId
, factory
);
52 * Registers the given adapter factory as extending traces of the given
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.
64 * the class of traces being extended
66 public static void registerFactory(IAdapterFactory factory
, Class
<?
extends ITmfTrace
> traceClass
) {
67 fFactoriesByClass
.put(traceClass
, factory
);
71 * Removes the given adapter factory completely from the list of registered
75 * the adapter factory to remove
76 * @see #registerFactory(IAdapterFactory, Class)
77 * @see #registerFactory(IAdapterFactory, String)
79 public static void unregisterFactory(IAdapterFactory factory
) {
80 fFactoriesById
.values().remove(factory
);
81 fFactoriesByClass
.values().remove(factory
);
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
91 * the trace being queried
93 * the type of adapter to look up
94 * @return a list of objects of the given adapter type
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
) {
101 @Nullable T adapter
= factory
.getAdapter(trace
, adapterType
);
102 if (adapter
!= null) {
103 adapters
.add(adapter
);
106 for (Entry
<Class
<?
extends ITmfTrace
>, IAdapterFactory
> entry
: entries
) {
107 if (entry
.getKey().isInstance(trace
)) {
108 @Nullable T adapter
= entry
.getValue().getAdapter(trace
, adapterType
);
109 if (adapter
!= null) {
110 adapters
.add(adapter
);
This page took 0.032309 seconds and 5 git commands to generate.