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