tmf.core: Introduce ITmfTimestamp#toNanos
authorMatthew Khouzam <matthew.khouzam@ericsson.com>
Mon, 22 Feb 2016 19:55:52 +0000 (14:55 -0500)
committerMatthew Khouzam <matthew.khouzam@ericsson.com>
Fri, 26 Feb 2016 02:49:22 +0000 (21:49 -0500)
This method can be used to quickly convert an ITmfTimestamp to a
long using nanoseconds. This should reduce code re-use but also
accelerate the fast path as it saves object creation and avoid
errors in copy pasted code chunks.

Change-Id: I402c419fe2d579cf5e7e07b258665adbf8af8f8b
Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/67090
Reviewed-by: Hudson CI
Reviewed-by: Patrick Tasse <patrick.tasse@gmail.com>
Tested-by: Patrick Tasse <patrick.tasse@gmail.com>
tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/timestamp/ITmfTimestamp.java
tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/timestamp/TmfNanoTimestamp.java

index cc6e79ac049e5dc50ca951216fe72483e77c1841..b636362ea20b2949346b859a7715c7608d780e21 100644 (file)
@@ -77,6 +77,21 @@ public interface ITmfTimestamp extends Comparable<ITmfTimestamp> {
      */
     int getScale();
 
+    /**
+     * Gets the timestamp converted to nanoseconds, if the timestamp is larger
+     * than {@link Long#MAX_VALUE} or smaller than {@link Long#MIN_VALUE} it
+     * will be clamped to those values.
+     *
+     * @return the timestamp converted to a long value of nanoseconds
+     * @since 2.0
+     */
+    default long toNanos(){
+        if (getScale() == NANOSECOND_SCALE) {
+            return getValue();
+        }
+        return normalize(0L, NANOSECOND_SCALE).getValue();
+    }
+
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
index e84e48362eafe7353fd81e22cc06ede8ae4421ad..a277bf2e8632c481d0658df3cbadc141a7f90537 100644 (file)
@@ -32,7 +32,8 @@ public class TmfNanoTimestamp extends TmfTimestamp {
     /**
      * Full constructor
      *
-     * @param value the timestamp value
+     * @param value
+     *            the timestamp value
      */
     public TmfNanoTimestamp(final long value) {
         super(value, ITmfTimestamp.NANOSECOND_SCALE);
@@ -41,8 +42,8 @@ public class TmfNanoTimestamp extends TmfTimestamp {
     /**
      * Copy constructor.
      *
-     * If the parameter is not a TmfNanoTimestamp, the timestamp will be
-     * scaled to nanoseconds, and the precision will be discarded.
+     * If the parameter is not a TmfNanoTimestamp, the timestamp will be scaled
+     * to nanoseconds, and the precision will be discarded.
      *
      * @param timestamp
      *            The timestamp to copy
@@ -80,6 +81,14 @@ public class TmfNanoTimestamp extends TmfTimestamp {
         return super.getDelta(ts);
     }
 
+    /**
+     * @since 2.0
+     */
+    @Override
+    public long toNanos(){
+        return getValue();
+    }
+
     // ------------------------------------------------------------------------
     // Object
     // ------------------------------------------------------------------------
This page took 0.026707 seconds and 5 git commands to generate.