timing: Add a generic table view for any segment provider
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / trace / AbstractTmfTraceAdapterFactory.java
CommitLineData
dc95f516
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.Collection;
16import java.util.Map;
17
18import org.eclipse.core.runtime.IAdapterFactory;
19import org.eclipse.jdt.annotation.NonNull;
20import org.eclipse.jdt.annotation.Nullable;
21import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
22import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
23import org.eclipse.tracecompass.tmf.core.signal.TmfTraceClosedSignal;
24
25import com.google.common.collect.HashBasedTable;
26import com.google.common.collect.Table;
27
28/**
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}.
32 *
33 * @since 2.0
34 */
35public abstract class AbstractTmfTraceAdapterFactory implements IAdapterFactory {
36
37 /**
38 * Interface for trace adapters that manage resources which must be freed
39 * when the trace is closed.
40 */
41 public interface IDisposableAdapter {
42 /**
43 * Disposes of this trace adapter. All resources must be freed.
44 */
45 void dispose();
46 }
47
48 private final Table<ITmfTrace, Class<?>, Object> fAdapters = HashBasedTable.create();
49
50 /**
51 * Constructor.
52 */
53 public AbstractTmfTraceAdapterFactory() {
54 TmfSignalManager.register(this);
55 }
56
57 /**
58 * Disposes the trace adapter factory's resources and all of its adapters.
59 */
60 public synchronized void dispose() {
61 TmfSignalManager.deregister(this);
62 disposeAdapters(fAdapters.values());
63 fAdapters.clear();
64 }
65
66 private static void disposeAdapters(Collection<Object> adapters) {
67 for (Object adapter : adapters) {
68 if (adapter instanceof IDisposableAdapter) {
69 ((IDisposableAdapter) adapter).dispose();
70 }
71 }
72 }
73
74 @Override
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);
81 }
82 if (adapter != null) {
83 fAdapters.put(trace, adapterType, adapter);
84 return adapterType.cast(adapter);
85 }
86 }
87 return null;
88 }
89
90 /**
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.
93 *
94 * @param trace
95 * the trace being adapted
96 * @param adapterType
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
100 */
101 protected abstract <T> @Nullable T getTraceAdapter(@NonNull ITmfTrace trace, Class<T> adapterType);
102
103 /**
104 * Signal handler for the trace closed signal.
105 *
106 * @param signal
107 * the trace closed signal
108 */
109 @TmfSignalHandler
110 public synchronized void traceClosed(TmfTraceClosedSignal signal) {
111 Map<Class<?>, Object> row = fAdapters.row(signal.getTrace());
112 disposeAdapters(row.values());
113 row.clear();
114 }
115}
This page took 0.046095 seconds and 5 git commands to generate.