tmf: Add equals/hashCode to TmfConstantTransform
[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 java.util.Objects;
16
17 import org.eclipse.jdt.annotation.NonNull;
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;
21
22 /**
23 * Constant transform, just offset your timestamp with another.
24 *
25 * @author Matthew Khouzam
26 */
27 public class TmfConstantTransform implements ITmfTimestampTransformInvertible {
28
29 /**
30 * Serial ID
31 */
32 private static final long serialVersionUID = 417299521984404532L;
33 private final long fOffset;
34
35 /**
36 * Default constructor
37 */
38 public TmfConstantTransform() {
39 // we really should be using an identity transform here.
40 fOffset = 0;
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) {
50 fOffset = offset;
51 }
52
53 /**
54 * Constructor with offset timestamp
55 *
56 * @param offset
57 * The offset of the linear transform
58 */
59 public TmfConstantTransform(@NonNull ITmfTimestamp offset) {
60 this(offset.toNanos());
61 }
62
63 @Override
64 public ITmfTimestamp transform(ITmfTimestamp timestamp) {
65 return timestamp.normalize(fOffset, ITmfTimestamp.NANOSECOND_SCALE);
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) {
77 return fOffset + timestamp;
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;
87 final long offset = fOffset + tct.fOffset;
88 if (offset == 0) {
89 return TmfTimestampTransform.IDENTITY;
90 }
91 return new TmfConstantTransform(offset);
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
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
128 @Override
129 public String toString() {
130 StringBuilder builder = new StringBuilder();
131 builder.append("TmfConstantTransform [ offset = "); //$NON-NLS-1$
132 builder.append(fOffset);
133 builder.append(" ]"); //$NON-NLS-1$
134 return builder.toString();
135 }
136
137 }
This page took 0.033683 seconds and 5 git commands to generate.