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
.Collection
;
18 import org
.eclipse
.core
.runtime
.IAdapterFactory
;
19 import org
.eclipse
.jdt
.annotation
.NonNull
;
20 import org
.eclipse
.jdt
.annotation
.Nullable
;
21 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
22 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
23 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceClosedSignal
;
25 import com
.google
.common
.collect
.HashBasedTable
;
26 import com
.google
.common
.collect
.Table
;
29 * Base class for a trace adapter factory. The factory creates a single instance
30 * of each adapter type per trace, and disposes the adapter when the trace is
31 * closed, if it is an instance of {@link IDisposableAdapter}.
35 public abstract class AbstractTmfTraceAdapterFactory
implements IAdapterFactory
{
38 * Interface for trace adapters that manage resources which must be freed
39 * when the trace is closed.
41 public interface IDisposableAdapter
{
43 * Disposes of this trace adapter. All resources must be freed.
48 private final Table
<ITmfTrace
, Class
<?
>, Object
> fAdapters
= HashBasedTable
.create();
53 public AbstractTmfTraceAdapterFactory() {
54 TmfSignalManager
.register(this);
58 * Disposes the trace adapter factory's resources and all of its adapters.
60 public synchronized void dispose() {
61 TmfSignalManager
.deregister(this);
62 disposeAdapters(fAdapters
.values());
66 private static void disposeAdapters(Collection
<Object
> adapters
) {
67 for (Object adapter
: adapters
) {
68 if (adapter
instanceof IDisposableAdapter
) {
69 ((IDisposableAdapter
) adapter
).dispose();
75 public synchronized <T
> T
getAdapter(Object adaptableObject
, Class
<T
> adapterType
) {
76 if (adaptableObject
instanceof ITmfTrace
) {
77 ITmfTrace trace
= (ITmfTrace
) adaptableObject
;
78 Object adapter
= fAdapters
.get(trace
, adapterType
);
79 if (adapter
== null) {
80 adapter
= getTraceAdapter(trace
, adapterType
);
82 if (adapter
!= null) {
83 fAdapters
.put(trace
, adapterType
, adapter
);
84 return adapterType
.cast(adapter
);
91 * Returns an object which is an instance of the given class associated with
92 * the given trace. Returns null if no such object can be found.
95 * the trace being adapted
97 * the type of adapter to look up
98 * @return a object of the given adapter type, or null if this factory does
99 * not have an adapter of the given type for the given trace
101 protected abstract <T
> @Nullable T
getTraceAdapter(@NonNull ITmfTrace trace
, Class
<T
> adapterType
);
104 * Signal handler for the trace closed signal.
107 * the trace closed signal
110 public synchronized void traceClosed(TmfTraceClosedSignal signal
) {
111 Map
<Class
<?
>, Object
> row
= fAdapters
.row(signal
.getTrace());
112 disposeAdapters(row
.values());