Commit | Line | Data |
---|---|---|
e5cfb647 MK |
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 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.internal.tmf.core.synchronization; |
e5cfb647 | 14 | |
4a1d13c4 AM |
15 | import java.util.Objects; |
16 | ||
741f4099 | 17 | import org.eclipse.jdt.annotation.NonNull; |
2bdf0193 AM |
18 | import org.eclipse.tracecompass.tmf.core.synchronization.ITmfTimestampTransform; |
19 | import org.eclipse.tracecompass.tmf.core.synchronization.TimestampTransformFactory; | |
20 | import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp; | |
e5cfb647 MK |
21 | |
22 | /** | |
23 | * Constant transform, just offset your timestamp with another. | |
24 | * | |
25 | * @author Matthew Khouzam | |
26 | */ | |
ac29c59b | 27 | public class TmfConstantTransform implements ITmfTimestampTransformInvertible { |
e5cfb647 MK |
28 | |
29 | /** | |
30 | * Serial ID | |
31 | */ | |
32 | private static final long serialVersionUID = 417299521984404532L; | |
741f4099 | 33 | private final long fOffset; |
e5cfb647 MK |
34 | |
35 | /** | |
36 | * Default constructor | |
37 | */ | |
38 | public TmfConstantTransform() { | |
741f4099 MK |
39 | // we really should be using an identity transform here. |
40 | fOffset = 0; | |
e5cfb647 MK |
41 | } |
42 | ||
43 | /** | |
44 | * Constructor with offset | |
45 | * | |
46 | * @param offset | |
47 | * The offset of the linear transform in nanoseconds | |
48 | */ | |
49 | public TmfConstantTransform(long offset) { | |
741f4099 | 50 | fOffset = offset; |
e5cfb647 MK |
51 | } |
52 | ||
53 | /** | |
54 | * Constructor with offset timestamp | |
55 | * | |
56 | * @param offset | |
57 | * The offset of the linear transform | |
58 | */ | |
741f4099 | 59 | public TmfConstantTransform(@NonNull ITmfTimestamp offset) { |
b2c971ec | 60 | this(offset.toNanos()); |
e5cfb647 MK |
61 | } |
62 | ||
63 | @Override | |
64 | public ITmfTimestamp transform(ITmfTimestamp timestamp) { | |
741f4099 | 65 | return timestamp.normalize(fOffset, ITmfTimestamp.NANOSECOND_SCALE); |
e5cfb647 MK |
66 | } |
67 | ||
68 | /** | |
69 | * {@inheritDoc} | |
70 | * | |
71 | * @param timestamp | |
72 | * the timestamp in nanoseconds | |
73 | * @return the timestamp in nanoseconds | |
74 | */ | |
75 | @Override | |
76 | public long transform(long timestamp) { | |
741f4099 | 77 | return fOffset + timestamp; |
e5cfb647 MK |
78 | } |
79 | ||
80 | @Override | |
81 | public ITmfTimestampTransform composeWith(ITmfTimestampTransform composeWith) { | |
82 | if (composeWith.equals(TmfTimestampTransform.IDENTITY)) { | |
83 | /* If composing with identity, just return this */ | |
84 | return this; | |
85 | } else if (composeWith instanceof TmfConstantTransform) { | |
86 | TmfConstantTransform tct = (TmfConstantTransform) composeWith; | |
741f4099 MK |
87 | final long offset = fOffset + tct.fOffset; |
88 | if (offset == 0) { | |
89 | return TmfTimestampTransform.IDENTITY; | |
90 | } | |
91 | return new TmfConstantTransform(offset); | |
e5cfb647 MK |
92 | } else if (composeWith instanceof TmfTimestampTransformLinear) { |
93 | throw new UnsupportedOperationException("Cannot compose a constant and linear transform yet"); //$NON-NLS-1$ | |
94 | } else { | |
95 | /* | |
96 | * We do not know what to do with this kind of transform, just | |
97 | * return this | |
98 | */ | |
99 | return this; | |
100 | } | |
101 | } | |
102 | ||
4a1d13c4 AM |
103 | @Override |
104 | public ITmfTimestampTransform inverse() { | |
105 | return TimestampTransformFactory.createWithOffset(-1 * fOffset); | |
106 | } | |
107 | ||
108 | @Override | |
109 | public int hashCode() { | |
110 | return Objects.hash(fOffset); | |
111 | } | |
112 | ||
113 | @Override | |
114 | public boolean equals(Object obj) { | |
115 | if (this == obj) { | |
116 | return true; | |
117 | } | |
118 | if (obj == null) { | |
119 | return false; | |
120 | } | |
121 | if (getClass() != obj.getClass()) { | |
122 | return false; | |
123 | } | |
124 | TmfConstantTransform other = (TmfConstantTransform) obj; | |
125 | return (fOffset == other.fOffset); | |
126 | } | |
127 | ||
e5cfb647 MK |
128 | @Override |
129 | public String toString() { | |
130 | StringBuilder builder = new StringBuilder(); | |
741f4099 | 131 | builder.append("TmfConstantTransform [ offset = "); //$NON-NLS-1$ |
e5cfb647 | 132 | builder.append(fOffset); |
741f4099 | 133 | builder.append(" ]"); //$NON-NLS-1$ |
e5cfb647 MK |
134 | return builder.toString(); |
135 | } | |
136 | ||
137 | } |