dfa470ddbcd818fbd90d2a9503c905a9943ed5f5
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / internal / tmf / core / synchronization / TmfConstantTransform.java
1 /*******************************************************************************
2 * Copyright (c) 2014 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 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.internal.tmf.core.synchronization;
14
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.tracecompass.tmf.core.synchronization.ITmfTimestampTransform;
17 import org.eclipse.tracecompass.tmf.core.synchronization.TimestampTransformFactory;
18 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
19
20 /**
21 * Constant transform, just offset your timestamp with another.
22 *
23 * @author Matthew Khouzam
24 */
25 public class TmfConstantTransform implements ITmfTimestampTransformInvertible {
26
27 /**
28 * Serial ID
29 */
30 private static final long serialVersionUID = 417299521984404532L;
31 private final long fOffset;
32
33 /**
34 * Default constructor
35 */
36 public TmfConstantTransform() {
37 // we really should be using an identity transform here.
38 fOffset = 0;
39 }
40
41 /**
42 * Constructor with offset
43 *
44 * @param offset
45 * The offset of the linear transform in nanoseconds
46 */
47 public TmfConstantTransform(long offset) {
48 fOffset = offset;
49 }
50
51 /**
52 * Constructor with offset timestamp
53 *
54 * @param offset
55 * The offset of the linear transform
56 */
57 public TmfConstantTransform(@NonNull ITmfTimestamp offset) {
58 this(offset.toNanos());
59 }
60
61 @Override
62 public ITmfTimestamp transform(ITmfTimestamp timestamp) {
63 return timestamp.normalize(fOffset, ITmfTimestamp.NANOSECOND_SCALE);
64 }
65
66 /**
67 * {@inheritDoc}
68 *
69 * @param timestamp
70 * the timestamp in nanoseconds
71 * @return the timestamp in nanoseconds
72 */
73 @Override
74 public long transform(long timestamp) {
75 return fOffset + timestamp;
76 }
77
78 @Override
79 public ITmfTimestampTransform composeWith(ITmfTimestampTransform composeWith) {
80 if (composeWith.equals(TmfTimestampTransform.IDENTITY)) {
81 /* If composing with identity, just return this */
82 return this;
83 } else if (composeWith instanceof TmfConstantTransform) {
84 TmfConstantTransform tct = (TmfConstantTransform) composeWith;
85 final long offset = fOffset + tct.fOffset;
86 if (offset == 0) {
87 return TmfTimestampTransform.IDENTITY;
88 }
89 return new TmfConstantTransform(offset);
90 } else if (composeWith instanceof TmfTimestampTransformLinear) {
91 throw new UnsupportedOperationException("Cannot compose a constant and linear transform yet"); //$NON-NLS-1$
92 } else {
93 /*
94 * We do not know what to do with this kind of transform, just
95 * return this
96 */
97 return this;
98 }
99 }
100
101 @Override
102 public String toString() {
103 StringBuilder builder = new StringBuilder();
104 builder.append("TmfConstantTransform [ offset = "); //$NON-NLS-1$
105 builder.append(fOffset);
106 builder.append(" ]"); //$NON-NLS-1$
107 return builder.toString();
108 }
109
110 @Override
111 public ITmfTimestampTransform inverse() {
112 return TimestampTransformFactory.createWithOffset(-1 * fOffset);
113 }
114
115 }
This page took 0.032841 seconds and 4 git commands to generate.