Update to session creation procedure
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / event / TmfTimestamp.java
index b5aee95c7ef204778c04d27a65141999d37a47f8..299caa5f689e2d93ac00df37ea2b5d3942e33318 100644 (file)
@@ -1,23 +1,27 @@
 /*******************************************************************************
  * Copyright (c) 2009, 2010, 2012 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:
  *   Francois Chouinard - Initial API and implementation
  *   Thomas Gatterweh  - Updated scaling / synchronization
  *   Francois Chouinard - Refactoring to align with TMF Event Model 1.0
+ *   Francois Chouinard - Implement augmented interface
  *******************************************************************************/
 
 package org.eclipse.linuxtools.tmf.core.event;
 
 /**
- * <b><u>TmfTimestamp</u></b>
- * <p>
- * A generic implementation of ITmfTimestamp.
+ * A generic timestamp implementation. The timestamp is represented by the
+ * tuple { value, scale, precision }. By default, timestamps are scaled in
+ * seconds.
+ *
+ * @version 1.1
+ * @author Francois Chouinard
  */
 public class TmfTimestamp implements ITmfTimestamp {
 
@@ -37,6 +41,18 @@ public class TmfTimestamp implements ITmfTimestamp {
     public static final ITmfTimestamp BIG_CRUNCH =
             new TmfTimestamp(Long.MAX_VALUE, Integer.MAX_VALUE, 0);
 
+    /**
+     * A more practical definition of "beginning of time"
+     * @since 2.0
+     */
+    public static final ITmfTimestamp PROJECT_IS_FUNDED = BIG_BANG;
+
+    /**
+     * A more practical definition of "end of time"
+     * @since 2.0
+     */
+    public static final ITmfTimestamp PROJECT_IS_CANNED = BIG_CRUNCH;
+
     /**
      * Zero
      */
@@ -50,17 +66,17 @@ public class TmfTimestamp implements ITmfTimestamp {
     /**
      * The timestamp raw value (mantissa)
      */
-    protected long fValue;
+    private final long fValue;
 
     /**
      * The timestamp scale (magnitude)
      */
-    protected int fScale;
+    private final int fScale;
 
     /**
      * The value precision (tolerance)
      */
-    protected int fPrecision;
+    private final int fPrecision;
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -70,7 +86,7 @@ public class TmfTimestamp implements ITmfTimestamp {
      * Default constructor
      */
     public TmfTimestamp() {
-        this(0, 0, 0);
+        this(0, ITmfTimestamp.SECOND_SCALE, 0);
     }
 
     /**
@@ -79,12 +95,12 @@ public class TmfTimestamp implements ITmfTimestamp {
      * @param value the timestamp value
      */
     public TmfTimestamp(final long value) {
-        this(value, 0, 0);
+        this(value, ITmfTimestamp.SECOND_SCALE, 0);
     }
 
     /**
      * Simple constructor (precision = 0)
-     * 
+     *
      * @param value the timestamp value
      * @param scale the timestamp scale
      */
@@ -94,7 +110,7 @@ public class TmfTimestamp implements ITmfTimestamp {
 
     /**
      * Full constructor
-     * 
+     *
      * @param value the timestamp value
      * @param scale the timestamp scale
      * @param precision the timestamp precision
@@ -107,12 +123,13 @@ public class TmfTimestamp implements ITmfTimestamp {
 
     /**
      * Copy constructor
-     * 
+     *
      * @param timestamp the timestamp to copy
      */
     public TmfTimestamp(final ITmfTimestamp timestamp) {
-        if (timestamp == null)
+        if (timestamp == null) {
             throw new IllegalArgumentException();
+        }
         fValue = timestamp.getValue();
         fScale = timestamp.getScale();
         fPrecision = timestamp.getPrecision();
@@ -172,20 +189,27 @@ public class TmfTimestamp implements ITmfTimestamp {
      * @see org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp#normalize(long, int)
      */
     @Override
-    public ITmfTimestamp normalize(final long offset, final int scale) throws ArithmeticException {
+    public ITmfTimestamp normalize(final long offset, final int scale) {
 
         long value = fValue;
         int precision = fPrecision;
 
         // Handle the trivial case
-        if (fScale == scale && offset == 0)
-            return new TmfTimestamp(this);
+        if (fScale == scale && offset == 0) {
+            return this;
+        }
+
+        // In case of big bang and big crunch just return this (no need to normalize)
+        if (this.equals(BIG_BANG) || this.equals(BIG_CRUNCH)) {
+            return this;
+        }
 
         // First, scale the timestamp
         if (fScale != scale) {
             final int scaleDiff = Math.abs(fScale - scale);
-            if (scaleDiff >= scalingFactors.length)
+            if (scaleDiff >= scalingFactors.length) {
                 throw new ArithmeticException("Scaling exception"); //$NON-NLS-1$
+            }
 
             final long scalingFactor = scalingFactors[scaleDiff];
             if (scale < fScale) {
@@ -198,10 +222,11 @@ public class TmfTimestamp implements ITmfTimestamp {
         }
 
         // Then, apply the offset
-        if (offset < 0)
+        if (offset < 0) {
             value = (value < Long.MIN_VALUE - offset) ? Long.MIN_VALUE : value + offset;
-        else
+        } else {
             value = (value > Long.MAX_VALUE - offset) ? Long.MAX_VALUE : value + offset;
+        }
 
         return new TmfTimestamp(value, scale, precision);
     }
@@ -212,22 +237,26 @@ public class TmfTimestamp implements ITmfTimestamp {
     @Override
     public int compareTo(final ITmfTimestamp ts, final boolean withinPrecision) {
 
-        if (ts == null)
-            return 1;
-
         // Check the corner cases (we can't use equals() because it uses compareTo()...)
-        if (this == ts || (fValue == ts.getValue() && fScale == ts.getScale()))
+        if (ts == null) {
+            return 1;
+        }
+        if (this == ts || (fValue == ts.getValue() && fScale == ts.getScale())) {
             return 0;
-        if ((fValue == BIG_BANG.getValue() && fScale == BIG_BANG.getScale()) || (ts.getValue() == BIG_CRUNCH.getValue() && ts.getScale() == BIG_CRUNCH.getScale()))
+        }
+        if ((fValue == BIG_BANG.getValue() && fScale == BIG_BANG.getScale()) || (ts.getValue() == BIG_CRUNCH.getValue() && ts.getScale() == BIG_CRUNCH.getScale())) {
             return -1;
-        if ((fValue == BIG_CRUNCH.getValue() && fScale == BIG_CRUNCH.getScale()) || (ts.getValue() == BIG_BANG.getValue() && ts.getScale() == BIG_BANG.getScale()))
+        }
+        if ((fValue == BIG_CRUNCH.getValue() && fScale == BIG_CRUNCH.getScale()) || (ts.getValue() == BIG_BANG.getValue() && ts.getScale() == BIG_BANG.getScale())) {
             return 1;
+        }
 
         try {
             final ITmfTimestamp nts = ts.normalize(0, fScale);
             final long delta = fValue - nts.getValue();
-            if ((delta == 0) || (withinPrecision && (Math.abs(delta) <= (fPrecision + nts.getPrecision()))))
+            if ((delta == 0) || (withinPrecision && (Math.abs(delta) <= (fPrecision + nts.getPrecision())))) {
                 return 0;
+            }
             return (delta > 0) ? 1 : -1;
         }
         catch (final ArithmeticException e) {
@@ -235,12 +264,15 @@ public class TmfTimestamp implements ITmfTimestamp {
 
             // First, look at the sign of the mantissa
             final long value = ts.getValue();
-            if (fValue == 0 && value == 0)
+            if (fValue == 0 && value == 0) {
                 return 0;
-            if (fValue  < 0 && value >= 0)
+            }
+            if (fValue < 0 && value >= 0) {
                 return -1;
-            if (fValue >= 0 && value < 0)
+            }
+            if (fValue >= 0 && value < 0) {
                 return 1;
+            }
 
             // Otherwise, just compare the scales
             final int scale = ts.getScale();
@@ -255,27 +287,7 @@ public class TmfTimestamp implements ITmfTimestamp {
     public ITmfTimestamp getDelta(final ITmfTimestamp ts) {
         final ITmfTimestamp nts = ts.normalize(0, fScale);
         final long value = fValue - nts.getValue();
-        return new TmfTimestamp(value, fScale, fPrecision + nts.getPrecision());
-    }
-
-    // ------------------------------------------------------------------------
-    // Cloneable
-    // ------------------------------------------------------------------------
-
-    /* (non-Javadoc)
-     * @see java.lang.Object#clone()
-     */
-    @Override
-    public TmfTimestamp clone() {
-        TmfTimestamp clone = null;
-        try {
-            clone = (TmfTimestamp) super.clone();
-            clone.fValue = fValue;
-            clone.fScale = fScale;
-            clone.fPrecision = fPrecision;
-        } catch (final CloneNotSupportedException e) {
-        }
-        return clone;
+        return new TmfTimestampDelta(value, fScale, fPrecision + nts.getPrecision());
     }
 
     // ------------------------------------------------------------------------
@@ -312,12 +324,15 @@ public class TmfTimestamp implements ITmfTimestamp {
      */
     @Override
     public boolean equals(final Object other) {
-        if (this == other)
+        if (this == other) {
             return true;
-        if (other == null)
+        }
+        if (other == null) {
             return false;
-        if (!(other instanceof TmfTimestamp))
+        }
+        if (!(other instanceof TmfTimestamp)) {
             return false;
+        }
         final TmfTimestamp ts = (TmfTimestamp) other;
         return compareTo(ts, false) == 0;
     }
@@ -326,9 +341,25 @@ public class TmfTimestamp implements ITmfTimestamp {
      * @see java.lang.Object#toString()
      */
     @Override
-    @SuppressWarnings("nls")
     public String toString() {
-        return "TmfTimestamp [fValue=" + fValue + ", fScale=" + fScale + ", fPrecision=" + fPrecision + "]";
+        return toString(TmfTimestampFormat.getDefaulTimeFormat());
+    }
+
+    /* (non-Javadoc)
+     * @see org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp#toString(org.eclipse.linuxtools.tmf.core.event.TmfTimestampFormat)
+     */
+    /**
+     * @since 2.0
+     */
+    @Override
+    public String toString(final TmfTimestampFormat format) {
+        try {
+            ITmfTimestamp ts = normalize(0, ITmfTimestamp.NANOSECOND_SCALE);
+            return format.format(ts.getValue());
+        }
+        catch (ArithmeticException e) {
+            return format.format(0);
+        }
     }
 
 }
This page took 0.030867 seconds and 5 git commands to generate.