Update to session creation procedure
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / event / TmfTimestamp.java
index 601ac3f82993b5024c11235dd76d90dfd6c7fb83..299caa5f689e2d93ac00df37ea2b5d3942e33318 100644 (file)
@@ -1,28 +1,29 @@
 /*******************************************************************************
  * 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;
 
-
 /**
  * A generic timestamp implementation. The timestamp is represented by the
- * tuple { value, scale, precision }.
- * 
- * @version 1.0
+ * tuple { value, scale, precision }. By default, timestamps are scaled in
+ * seconds.
+ *
+ * @version 1.1
  * @author Francois Chouinard
  */
-public class TmfTimestamp implements ITmfTimestamp, Cloneable {
+public class TmfTimestamp implements ITmfTimestamp {
 
     // ------------------------------------------------------------------------
     // Constants
@@ -40,6 +41,18 @@ public class TmfTimestamp implements ITmfTimestamp, Cloneable {
     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
      */
@@ -53,17 +66,17 @@ public class TmfTimestamp implements ITmfTimestamp, Cloneable {
     /**
      * The timestamp raw value (mantissa)
      */
-    private long fValue;
+    private final long fValue;
 
     /**
      * The timestamp scale (magnitude)
      */
-    private int fScale;
+    private final int fScale;
 
     /**
      * The value precision (tolerance)
      */
-    private int fPrecision;
+    private final int fPrecision;
 
     // ------------------------------------------------------------------------
     // Constructors
@@ -73,7 +86,7 @@ public class TmfTimestamp implements ITmfTimestamp, Cloneable {
      * Default constructor
      */
     public TmfTimestamp() {
-        this(0, 0, 0);
+        this(0, ITmfTimestamp.SECOND_SCALE, 0);
     }
 
     /**
@@ -82,12 +95,12 @@ public class TmfTimestamp implements ITmfTimestamp, Cloneable {
      * @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
      */
@@ -97,7 +110,7 @@ public class TmfTimestamp implements ITmfTimestamp, Cloneable {
 
     /**
      * Full constructor
-     * 
+     *
      * @param value the timestamp value
      * @param scale the timestamp scale
      * @param precision the timestamp precision
@@ -110,7 +123,7 @@ public class TmfTimestamp implements ITmfTimestamp, Cloneable {
 
     /**
      * Copy constructor
-     * 
+     *
      * @param timestamp the timestamp to copy
      */
     public TmfTimestamp(final ITmfTimestamp timestamp) {
@@ -122,16 +135,6 @@ public class TmfTimestamp implements ITmfTimestamp, Cloneable {
         fPrecision = timestamp.getPrecision();
     }
 
-    // ------------------------------------------------------------------------
-    // Setters
-    // ------------------------------------------------------------------------
-
-    protected void setValue(long value, int scale, int precision) {
-        fValue = value;
-        fScale = scale;
-        fPrecision = precision;
-    }
-
     // ------------------------------------------------------------------------
     // ITmfTimestamp
     // ------------------------------------------------------------------------
@@ -193,7 +196,12 @@ public class TmfTimestamp implements ITmfTimestamp, Cloneable {
 
         // Handle the trivial case
         if (fScale == scale && offset == 0) {
-            return new TmfTimestamp(this);
+            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
@@ -279,27 +287,7 @@ public class TmfTimestamp implements ITmfTimestamp, Cloneable {
     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());
     }
 
     // ------------------------------------------------------------------------
@@ -353,9 +341,25 @@ public class TmfTimestamp implements ITmfTimestamp, Cloneable {
      * @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.026769 seconds and 5 git commands to generate.