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 | ||
15 | import org.eclipse.linuxtools.tmf.core.synchronization.ITmfTimestampTransform; | |
16 | import org.eclipse.linuxtools.tmf.core.synchronization.TmfTimestampTransform; | |
17 | import org.eclipse.linuxtools.tmf.core.synchronization.TmfTimestampTransformLinear; | |
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 | */ | |
26 | public class TmfConstantTransform implements ITmfTimestampTransform { | |
27 | ||
28 | /** | |
29 | * Serial ID | |
30 | */ | |
31 | private static final long serialVersionUID = 417299521984404532L; | |
32 | private ITmfTimestamp fOffset; | |
33 | ||
34 | /** | |
35 | * Default constructor | |
36 | */ | |
37 | public TmfConstantTransform() { | |
38 | fOffset = new TmfNanoTimestamp(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 = new TmfNanoTimestamp(offset); | |
49 | } | |
50 | ||
51 | /** | |
52 | * Constructor with offset timestamp | |
53 | * | |
54 | * @param offset | |
55 | * The offset of the linear transform | |
56 | */ | |
57 | public TmfConstantTransform(ITmfTimestamp offset) { | |
58 | fOffset = offset; | |
59 | } | |
60 | ||
61 | @Override | |
62 | public ITmfTimestamp transform(ITmfTimestamp timestamp) { | |
63 | return fOffset.normalize(timestamp.getValue(), timestamp.getScale()); | |
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.normalize(timestamp, ITmfTimestamp.NANOSECOND_SCALE).getValue(); | |
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 | return new TmfConstantTransform(fOffset.getValue() + tct.fOffset.getValue()); | |
86 | } else if (composeWith instanceof TmfTimestampTransformLinear) { | |
87 | throw new UnsupportedOperationException("Cannot compose a constant and linear transform yet"); //$NON-NLS-1$ | |
88 | } else { | |
89 | /* | |
90 | * We do not know what to do with this kind of transform, just | |
91 | * return this | |
92 | */ | |
93 | return this; | |
94 | } | |
95 | } | |
96 | ||
97 | @Override | |
98 | public String toString() { | |
99 | StringBuilder builder = new StringBuilder(); | |
100 | builder.append("TmfConstantTransform [fOffset="); //$NON-NLS-1$ | |
101 | builder.append(fOffset); | |
102 | builder.append("]"); //$NON-NLS-1$ | |
103 | return builder.toString(); | |
104 | } | |
105 | ||
106 | } |