2a663e421b5648c98967a00d7be44456310a7304
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / trace / TmfTraceContext.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2016 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 * Alexandre Montplaisir - Initial API and implementation
11 * Patrick Tasse - Support selection range
12 * Xavier Raynaud - Support filters tracking
13 *******************************************************************************/
14
15 package org.eclipse.tracecompass.tmf.core.trace;
16
17 import java.util.HashMap;
18 import java.util.Map;
19
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.eclipse.tracecompass.tmf.core.filter.ITmfFilter;
25 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
26 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
27
28 import com.google.common.collect.ImmutableMap;
29
30 /**
31 * Context of a trace, which is the representation of the "view" the user
32 * currently has on this trace (window time range, selected time or time range).
33 *
34 * TODO could be extended to support the notion of current location too.
35 *
36 * FIXME Is this really the right place for the Editor File ?
37 *
38 * @author Alexandre Montplaisir
39 * @since 1.0
40 */
41 @NonNullByDefault
42 public class TmfTraceContext implements ITraceContextSignalHandler {
43
44 static final TmfTraceContext NULL_CONTEXT = new TmfTraceContext(new TmfTimeRange(TmfTimestamp.BIG_CRUNCH, TmfTimestamp.BIG_CRUNCH),
45 TmfTimeRange.NULL_RANGE, null, null);
46
47 private final TmfTimeRange fSelection;
48 private final TmfTimeRange fWindowRange;
49 private final @Nullable IFile fEditorFile;
50 private final @Nullable ITmfFilter fFilter;
51 private final Map<@NonNull String, @NonNull Object> fData;
52
53 /**
54 * Build a new trace context.
55 *
56 * @param selection
57 * The selected time range
58 * @param windowRange
59 * The visible window's time range
60 * @param editorFile
61 * The file representing the selected editor
62 * @param filter
63 * The currently applied filter. 'null' for none.
64 */
65 public TmfTraceContext(TmfTimeRange selection, TmfTimeRange windowRange,
66 @Nullable IFile editorFile, @Nullable ITmfFilter filter) {
67 fSelection = selection;
68 fWindowRange = windowRange;
69 fEditorFile = editorFile;
70 fFilter = filter;
71 fData = new HashMap<>();
72 }
73
74 /**
75 * Constructs a new trace context with data taken from a builder.
76 *
77 * @param builder
78 * the builder
79 * @since 2.3
80 */
81 public TmfTraceContext(Builder builder) {
82 fSelection = builder.selection;
83 fWindowRange = builder.windowRange;
84 fEditorFile = builder.editorFile;
85 fFilter = builder.filter;
86 fData = new HashMap<>(builder.data);
87 }
88
89 /**
90 * Return the time range representing the current active selection.
91 *
92 * @return The selected time range
93 */
94 public TmfTimeRange getSelectionRange() {
95 return fSelection;
96 }
97
98 /**
99 * Return the current window time range.
100 *
101 * @return The current window time range
102 */
103 public TmfTimeRange getWindowRange() {
104 return fWindowRange;
105 }
106
107 /**
108 * Get the editor's file
109 *
110 * @return The editor file
111 */
112 public @Nullable IFile getEditorFile() {
113 return fEditorFile;
114 }
115
116 /**
117 * Gets the filter applied to the current trace
118 *
119 * @return The current filter, or <code>null</code> if there is none
120 */
121 public @Nullable ITmfFilter getFilter() {
122 return fFilter;
123 }
124
125 /**
126 * Store a data for the trace
127 *
128 * @param key
129 * The id of the data
130 * @param value
131 * The value of the data
132 * @since 2.1
133 * @deprecated Use
134 * {@link TmfTraceManager#updateTraceContext(ITmfTrace, java.util.function.UnaryOperator)}
135 * and apply {@link Builder#setData(String, Object)} instead.
136 */
137 @Deprecated
138 public synchronized void setData(String key, Object value) {
139 fData.put(key, value);
140 }
141
142 /**
143 * Copy data into the data map
144 *
145 * @param data
146 * The map of data to copy
147 * @since 2.1
148 * @deprecated Use
149 * {@link TmfTraceManager#updateTraceContext(ITmfTrace, java.util.function.UnaryOperator)}
150 * and apply {@link Builder#setData(Map)} instead.
151 */
152 @Deprecated
153 public synchronized void setData(Map<String, Object> data) {
154 fData.putAll(data);
155 }
156
157 /**
158 * Get the data for the specific key
159 *
160 * @param key
161 * The id of the data
162 * @return The data or null if the key do not exist
163 * @since 2.1
164 */
165 public synchronized @Nullable Object getData(String key) {
166 return fData.get(key);
167 }
168
169 /**
170 * Get a copy of the data map
171 *
172 * @return The data map copy
173 * @since 2.1
174 */
175 public synchronized Map<String, Object> getData() {
176 return ImmutableMap.copyOf(fData);
177 }
178
179 /**
180 * Returns a new builder that is initialized with the data from this trace
181 * context.
182 *
183 * @return the builder
184 * @since 2.3
185 */
186 public Builder builder() {
187 return new Builder(this);
188 }
189
190 /**
191 * A builder for creating trace context instances.
192 *
193 * @since 2.3
194 */
195 public static class Builder {
196 private TmfTimeRange selection;
197 private TmfTimeRange windowRange;
198 private @Nullable IFile editorFile;
199 private @Nullable ITmfFilter filter;
200 private Map<String, Object> data;
201
202 /**
203 * Constructor
204 *
205 * @param ctx
206 * the trace context used to initialize the builder
207 */
208 public Builder(TmfTraceContext ctx) {
209 this.selection = ctx.fSelection;
210 this.windowRange = ctx.fWindowRange;
211 this.editorFile = ctx.fEditorFile;
212 this.filter = ctx.fFilter;
213 this.data = new HashMap<>(ctx.fData);
214 }
215
216 /**
217 * Build the trace context.
218 *
219 * @return a trace context
220 */
221 public TmfTraceContext build() {
222 return new TmfTraceContext(this);
223 }
224
225 /**
226 * Sets the selected time range.
227 *
228 * @param selection
229 * the selected time range
230 * @return this {@code Builder} object
231 */
232 public Builder setSelection(TmfTimeRange selection) {
233 this.selection = selection;
234 return this;
235 }
236
237 /**
238 * Sets the window range.
239 *
240 * @param windowRange
241 * the window range
242 * @return this {@code Builder} object
243 */
244 public Builder setWindowRange(TmfTimeRange windowRange) {
245 this.windowRange = windowRange;
246 return this;
247 }
248
249 /**
250 * Sets the current filter.
251 *
252 * @param filter
253 * the current filter
254 * @return this {@code Builder} object
255 */
256 public Builder setFilter(@Nullable ITmfFilter filter) {
257 this.filter = filter;
258 return this;
259 }
260
261 /**
262 * Sets a data mapping.
263 *
264 * @param key
265 * The key of the data
266 * @param value
267 * The value of the data
268 * @return this {@code Builder} object
269 */
270 public Builder setData(String key, Object value) {
271 this.data.put(key, value);
272 return this;
273 }
274
275 /**
276 * Sets data mappings.
277 *
278 * @param data
279 * The map of data
280 * @return this {@code Builder} object
281 */
282 public Builder setData(Map<String, Object> data) {
283 this.data.putAll(data);
284 return this;
285 }
286 }
287
288 @Override
289 public String toString() {
290 return getClass().getSimpleName() + "[fSelection=" + fSelection + //$NON-NLS-1$
291 ", fWindowRange=" + fWindowRange + ']'; //$NON-NLS-1$
292 }
293
294 }
This page took 0.036504 seconds and 4 git commands to generate.