tmf: add constant offset synchronization algorithm and algorithm factory
authorMatthew Khouzam <matthew.khouzam@ericsson.com>
Tue, 29 Jul 2014 17:26:37 +0000 (13:26 -0400)
committerMatthew Khouzam <matthew.khouzam@ericsson.com>
Tue, 5 Aug 2014 15:19:29 +0000 (11:19 -0400)
Change-Id: Ia5b2b49c0e49275b6fa78a16655325a57ab04725
Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/30687
Tested-by: Hudson CI
Reviewed-by: Patrick Tasse <patrick.tasse@gmail.com>
org.eclipse.linuxtools.tmf.core/META-INF/MANIFEST.MF
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/synchronization/TmfConstantTransform.java [new file with mode: 0644]
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/synchronization/ITmfTimestampTransform.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/synchronization/TimestampTransformFactory.java [new file with mode: 0644]

index 819a5ca8212b1068ca3397c406c033798121f81b..7e34202d584d196e9fcee5e09039edcbf0f4f467 100644 (file)
@@ -17,6 +17,7 @@ Export-Package: org.eclipse.linuxtools.internal.tmf.core;x-friends:="org.eclipse
  org.eclipse.linuxtools.internal.tmf.core.request;x-friends:="org.eclipse.linuxtools.tmf.core.tests",
  org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.partial;x-friends:="org.eclipse.linuxtools.statesystem.core.tests",
  org.eclipse.linuxtools.internal.tmf.core.statesystem.mipmap;x-friends:="org.eclipse.linuxtools.tmf.core.tests",
+ org.eclipse.linuxtools.internal.tmf.core.synchronization;x-friends:="org.eclipse.linuxtools.tmf.core.tests",
  org.eclipse.linuxtools.internal.tmf.core.trace;x-friends:="org.eclipse.linuxtools.tmf.core.tests",
  org.eclipse.linuxtools.internal.tmf.core.trace.indexer;x-friends:="org.eclipse.linuxtools.tmf.core.tests",
  org.eclipse.linuxtools.tmf.core,
diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/synchronization/TmfConstantTransform.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/synchronization/TmfConstantTransform.java
new file mode 100644 (file)
index 0000000..a144bc3
--- /dev/null
@@ -0,0 +1,106 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Matthew Khouzam - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.internal.tmf.core.synchronization;
+
+import org.eclipse.linuxtools.tmf.core.synchronization.ITmfTimestampTransform;
+import org.eclipse.linuxtools.tmf.core.synchronization.TmfTimestampTransform;
+import org.eclipse.linuxtools.tmf.core.synchronization.TmfTimestampTransformLinear;
+import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
+import org.eclipse.linuxtools.tmf.core.timestamp.TmfNanoTimestamp;
+
+/**
+ * Constant transform, just offset your timestamp with another.
+ *
+ * @author Matthew Khouzam
+ */
+public class TmfConstantTransform implements ITmfTimestampTransform {
+
+    /**
+     * Serial ID
+     */
+    private static final long serialVersionUID = 417299521984404532L;
+    private ITmfTimestamp fOffset;
+
+    /**
+     * Default constructor
+     */
+    public TmfConstantTransform() {
+        fOffset = new TmfNanoTimestamp(0);
+    }
+
+    /**
+     * Constructor with offset
+     *
+     * @param offset
+     *            The offset of the linear transform in nanoseconds
+     */
+    public TmfConstantTransform(long offset) {
+        fOffset = new TmfNanoTimestamp(offset);
+    }
+
+    /**
+     * Constructor with offset timestamp
+     *
+     * @param offset
+     *            The offset of the linear transform
+     */
+    public TmfConstantTransform(ITmfTimestamp offset) {
+        fOffset = offset;
+    }
+
+    @Override
+    public ITmfTimestamp transform(ITmfTimestamp timestamp) {
+        return fOffset.normalize(timestamp.getValue(), timestamp.getScale());
+    }
+
+    /**
+     * {@inheritDoc}
+     *
+     * @param timestamp
+     *            the timestamp in nanoseconds
+     * @return the timestamp in nanoseconds
+     */
+    @Override
+    public long transform(long timestamp) {
+        return fOffset.normalize(timestamp, ITmfTimestamp.NANOSECOND_SCALE).getValue();
+    }
+
+    @Override
+    public ITmfTimestampTransform composeWith(ITmfTimestampTransform composeWith) {
+        if (composeWith.equals(TmfTimestampTransform.IDENTITY)) {
+            /* If composing with identity, just return this */
+            return this;
+        } else if (composeWith instanceof TmfConstantTransform) {
+            TmfConstantTransform tct = (TmfConstantTransform) composeWith;
+            return new TmfConstantTransform(fOffset.getValue() + tct.fOffset.getValue());
+        } else if (composeWith instanceof TmfTimestampTransformLinear) {
+            throw new UnsupportedOperationException("Cannot compose a constant and linear transform yet"); //$NON-NLS-1$
+        } else {
+            /*
+             * We do not know what to do with this kind of transform, just
+             * return this
+             */
+            return this;
+        }
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder builder = new StringBuilder();
+        builder.append("TmfConstantTransform [fOffset="); //$NON-NLS-1$
+        builder.append(fOffset);
+        builder.append("]"); //$NON-NLS-1$
+        return builder.toString();
+    }
+
+}
index 99780f2f45d92f67a42ea5888a5900e1760cca27..9e24bb86cb73bbbbc88dd6dc05cef4359d755eff 100644 (file)
@@ -38,7 +38,7 @@ public interface ITmfTimestampTransform extends Serializable {
      * Transforms a timestamp value
      *
      * @param timestamp
-     *            The timestamp to transform
+     *            The timestamp to transform in nanoseconds
      * @return the transformed value
      */
     long transform(long timestamp);
diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/synchronization/TimestampTransformFactory.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/synchronization/TimestampTransformFactory.java
new file mode 100644 (file)
index 0000000..29d382e
--- /dev/null
@@ -0,0 +1,106 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are made
+ * available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *   Matthew Khouzam - Initial implementation and API
+ *******************************************************************************/
+package org.eclipse.linuxtools.tmf.core.synchronization;
+
+import java.math.BigDecimal;
+
+import org.eclipse.linuxtools.internal.tmf.core.synchronization.TmfConstantTransform;
+import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
+
+/**
+ * A factory to generate timestamp tranforms
+ *
+ * @author Matthew Khouzam
+ * @since 3.1
+ */
+public final class TimestampTransformFactory {
+
+    private TimestampTransformFactory() {
+    }
+
+    /**
+     * Create an offsetted transform
+     *
+     * @param offset
+     *            the offset in long format, nanosecond scale
+     * @return the offsetted transform
+     */
+    public static ITmfTimestampTransform create(long offset) {
+        if (offset == 0) {
+            return TmfTimestampTransform.IDENTITY;
+        }
+        return new TmfConstantTransform(offset);
+    }
+
+    /**
+     * Create an offsetted transform
+     *
+     * @param offset
+     *            the offset in a timestamp with scale
+     * @return the offsetted transform
+     */
+    public static ITmfTimestampTransform create(ITmfTimestamp offset) {
+        if (offset.getValue() == 0) {
+            return TmfTimestampTransform.IDENTITY;
+        }
+        return new TmfConstantTransform(offset);
+    }
+
+    /**
+     * Create an offsetted and sloped transform
+     *
+     * @param factor
+     *            the slope
+     * @param offset
+     *            the offset
+     * @return the transform
+     */
+    public static ITmfTimestampTransform create(double factor, ITmfTimestamp offset) {
+        if (factor == 1.0) {
+            return create(offset);
+        }
+        return new TmfTimestampTransformLinear(factor, offset.normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue());
+    }
+
+    /**
+     * Create an offsetted and sloped transform
+     *
+     * @param factor
+     *            the slope
+     * @param offset
+     *            the offset in nanoseconds
+     * @return the transform
+     */
+    public static ITmfTimestampTransform create(double factor, long offset) {
+        if (factor == 1.0) {
+            return create(offset);
+        }
+        return new TmfTimestampTransformLinear(factor, offset);
+    }
+
+    /**
+     * Create an offsetted and sloped transform using bigDecimals
+     *
+     * @param factor
+     *            the slope
+     * @param offset
+     *            the offset in nanoseconds
+     * @return the transform
+     */
+    public static ITmfTimestampTransform create(BigDecimal factor, BigDecimal offset) {
+        if (factor.equals(BigDecimal.ONE)) {
+            return create(offset.longValueExact());
+        }
+        return new TmfTimestampTransformLinear(factor, offset);
+    }
+
+}
This page took 0.029627 seconds and 5 git commands to generate.