tmf.all: use ITmfTimestamp#toNanos when possible
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / synchronization / TimestampTransformFactory.java
1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * 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 * Matthew Khouzam - Initial implementation and API
11 *******************************************************************************/
12 package org.eclipse.tracecompass.tmf.core.synchronization;
13
14 import java.io.File;
15 import java.io.FileInputStream;
16 import java.io.FileOutputStream;
17 import java.io.IOException;
18 import java.io.ObjectInputStream;
19 import java.io.ObjectOutputStream;
20 import java.math.BigDecimal;
21
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.eclipse.tracecompass.common.core.NonNullUtils;
27 import org.eclipse.tracecompass.internal.tmf.core.Activator;
28 import org.eclipse.tracecompass.internal.tmf.core.synchronization.TmfConstantTransform;
29 import org.eclipse.tracecompass.internal.tmf.core.synchronization.TmfTimestampTransform;
30 import org.eclipse.tracecompass.internal.tmf.core.synchronization.TmfTimestampTransformLinearFast;
31 import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
32 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
33
34 /**
35 * A factory to generate timestamp tranforms
36 *
37 * @author Matthew Khouzam
38 */
39 @NonNullByDefault
40 public final class TimestampTransformFactory {
41
42 private static final String SYNCHRONIZATION_FORMULA_FILE = "sync_formula"; //$NON-NLS-1$
43
44 private TimestampTransformFactory() {
45 }
46
47 /**
48 * Creates the identity timestamp transform
49 *
50 * @return The identity timestamp transform
51 */
52 public static ITmfTimestampTransform getDefaultTransform() {
53 return TmfTimestampTransform.IDENTITY;
54 }
55
56 /**
57 * Create an offsetted transform
58 *
59 * @param offset
60 * the offset in long format, nanosecond scale
61 * @return the offsetted transform
62 */
63 public static ITmfTimestampTransform createWithOffset(long offset) {
64 if (offset == 0) {
65 return TmfTimestampTransform.IDENTITY;
66 }
67 return new TmfConstantTransform(offset);
68 }
69
70 /**
71 * Create an offsetted transform
72 *
73 * @param offset
74 * the offset in a timestamp with scale
75 * @return the offsetted transform
76 */
77 public static ITmfTimestampTransform createWithOffset(ITmfTimestamp offset) {
78 if (offset.getValue() == 0) {
79 return TmfTimestampTransform.IDENTITY;
80 }
81 return new TmfConstantTransform(offset);
82 }
83
84 /**
85 * Create a timestamp transform corresponding to a linear equation, with
86 * slope and offset. The expected timestamp transform is such that f(t) =
87 * m*x + b, where m is the slope and b the offset.
88 *
89 * @param factor
90 * the slope
91 * @param offset
92 * the offset
93 * @return the transform
94 */
95 public static ITmfTimestampTransform createLinear(double factor, ITmfTimestamp offset) {
96 if (factor == 1.0) {
97 return createWithOffset(offset);
98 }
99 return new TmfTimestampTransformLinearFast(factor, offset.toNanos());
100 }
101
102 /**
103 * Create a timestamp transform corresponding to a linear equation, with
104 * slope and offset. The expected timestamp transform is such that f(t) =
105 * m*x + b, where m is the slope and b the offset.
106 *
107 * @param factor
108 * the slope
109 * @param offset
110 * the offset in nanoseconds
111 * @return the transform
112 */
113 public static ITmfTimestampTransform createLinear(double factor, long offset) {
114 if (factor == 1.0) {
115 return createWithOffset(offset);
116 }
117 return new TmfTimestampTransformLinearFast(factor, offset);
118 }
119
120 /**
121 * Create a timestamp transform corresponding to a linear equation, with
122 * slope and offset expressed in BigDecimal. The expected timestamp
123 * transform is such that f(t) = m*x + b, where m is the slope and b the
124 * offset.
125 *
126 * @param factor
127 * the slope
128 * @param offset
129 * the offset in nanoseconds
130 * @return the transform
131 */
132 public static ITmfTimestampTransform createLinear(BigDecimal factor, BigDecimal offset) {
133 if (factor.equals(BigDecimal.ONE)) {
134 return createWithOffset(offset.longValueExact());
135 }
136 return new TmfTimestampTransformLinearFast(factor, offset);
137 }
138
139 /**
140 * Returns the file resource used to store synchronization formula. The file
141 * may not exist.
142 *
143 * @param resource
144 * the trace resource
145 * @return the synchronization file
146 */
147 private static @Nullable File getSyncFormulaFile(@Nullable IResource resource) {
148 if (resource == null) {
149 return null;
150 }
151 try {
152 String supplDirectory = resource.getPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER);
153 return new File(supplDirectory + File.separator + SYNCHRONIZATION_FORMULA_FILE);
154 } catch (CoreException e) {
155 /* Ignored */
156 }
157 return null;
158 }
159
160 /**
161 * Returns the timestamp transform for a trace resource
162 *
163 * @param resource
164 * the trace resource
165 * @return the timestamp transform
166 */
167 public static ITmfTimestampTransform getTimestampTransform(@Nullable IResource resource) {
168 File syncFile = getSyncFormulaFile(resource);
169 if (syncFile != null && syncFile.exists()) {
170 /* Read the serialized object from file */
171 try (FileInputStream fis = new FileInputStream(syncFile);
172 ObjectInputStream ois = new ObjectInputStream(fis);) {
173 return NonNullUtils.checkNotNull((ITmfTimestampTransform) ois.readObject());
174 } catch (ClassNotFoundException | IOException e) {
175 }
176 }
177 return TimestampTransformFactory.getDefaultTransform();
178 }
179
180 /**
181 * Sets the trace resource's timestamp transform
182 *
183 * @param resource
184 * the trace resource
185 * @param tt
186 * The timestamp transform for all timestamps of this trace, or
187 * null to clear it
188 */
189 public static void setTimestampTransform(@Nullable IResource resource, @Nullable ITmfTimestampTransform tt) {
190 /* Save the timestamp transform to a file */
191 File syncFile = getSyncFormulaFile(resource);
192 if (syncFile != null) {
193 if (syncFile.exists()) {
194 syncFile.delete();
195 }
196 if (tt == null) {
197 return;
198 }
199 /* Write the serialized object to file */
200 try (FileOutputStream fos = new FileOutputStream(syncFile, false);
201 ObjectOutputStream oos = new ObjectOutputStream(fos);) {
202 oos.writeObject(tt);
203 } catch (IOException e1) {
204 Activator.logError("Error writing timestamp transform for trace", e1); //$NON-NLS-1$
205 }
206 }
207 }
208
209 }
This page took 0.036361 seconds and 6 git commands to generate.