From: Matthew Khouzam Date: Mon, 22 Feb 2016 19:55:52 +0000 (-0500) Subject: tmf.core: Introduce ITmfTimestamp#toNanos X-Git-Url: http://git.efficios.com/?a=commitdiff_plain;h=42e8594025de9059671ed1c26d3ea6f4ec89dcbe;p=deliverable%2Ftracecompass.git tmf.core: Introduce ITmfTimestamp#toNanos 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 Reviewed-on: https://git.eclipse.org/r/67090 Reviewed-by: Hudson CI Reviewed-by: Patrick Tasse Tested-by: Patrick Tasse --- diff --git a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/timestamp/ITmfTimestamp.java b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/timestamp/ITmfTimestamp.java index cc6e79ac04..b636362ea2 100644 --- a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/timestamp/ITmfTimestamp.java +++ b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/timestamp/ITmfTimestamp.java @@ -77,6 +77,21 @@ public interface ITmfTimestamp extends Comparable { */ 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 // ------------------------------------------------------------------------ diff --git a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/timestamp/TmfNanoTimestamp.java b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/timestamp/TmfNanoTimestamp.java index e84e48362e..a277bf2e86 100644 --- a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/timestamp/TmfNanoTimestamp.java +++ b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/timestamp/TmfNanoTimestamp.java @@ -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 // ------------------------------------------------------------------------