1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
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
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.tmf
.core
.synchronization
;
15 import java
.util
.Objects
;
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
;
23 * Constant transform, just offset your timestamp with another.
25 * @author Matthew Khouzam
27 public class TmfConstantTransform
implements ITmfTimestampTransformInvertible
{
32 private static final long serialVersionUID
= 417299521984404532L;
33 private final long fOffset
;
38 public TmfConstantTransform() {
39 // we really should be using an identity transform here.
44 * Constructor with offset
47 * The offset of the linear transform in nanoseconds
49 public TmfConstantTransform(long offset
) {
54 * Constructor with offset timestamp
57 * The offset of the linear transform
59 public TmfConstantTransform(@NonNull ITmfTimestamp offset
) {
60 this(offset
.toNanos());
64 public ITmfTimestamp
transform(ITmfTimestamp timestamp
) {
65 return timestamp
.normalize(fOffset
, ITmfTimestamp
.NANOSECOND_SCALE
);
72 * the timestamp in nanoseconds
73 * @return the timestamp in nanoseconds
76 public long transform(long timestamp
) {
77 return fOffset
+ timestamp
;
81 public ITmfTimestampTransform
composeWith(ITmfTimestampTransform composeWith
) {
82 if (composeWith
.equals(TmfTimestampTransform
.IDENTITY
)) {
83 /* If composing with identity, just return this */
85 } else if (composeWith
instanceof TmfConstantTransform
) {
86 TmfConstantTransform tct
= (TmfConstantTransform
) composeWith
;
87 final long offset
= fOffset
+ tct
.fOffset
;
89 return TmfTimestampTransform
.IDENTITY
;
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$
96 * We do not know what to do with this kind of transform, just
104 public ITmfTimestampTransform
inverse() {
105 return TimestampTransformFactory
.createWithOffset(-1 * fOffset
);
109 public int hashCode() {
110 return Objects
.hash(fOffset
);
114 public boolean equals(Object obj
) {
121 if (getClass() != obj
.getClass()) {
124 TmfConstantTransform other
= (TmfConstantTransform
) obj
;
125 return (fOffset
== other
.fOffset
);
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();