/*******************************************************************************
* 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
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
*/
/**
* 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
* Default constructor
*/
public TmfTimestamp() {
- this(0, 0, 0);
+ this(0, ITmfTimestamp.SECOND_SCALE, 0);
}
/**
* @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
*/
/**
* Full constructor
- *
+ *
* @param value the timestamp value
* @param scale the timestamp scale
* @param precision the timestamp precision
/**
* Copy constructor
- *
+ *
* @param timestamp the timestamp to copy
*/
public TmfTimestamp(final ITmfTimestamp timestamp) {
fPrecision = timestamp.getPrecision();
}
- // ------------------------------------------------------------------------
- // Setters
- // ------------------------------------------------------------------------
-
- protected void setValue(long value, int scale, int precision) {
- fValue = value;
- fScale = scale;
- fPrecision = precision;
- }
-
// ------------------------------------------------------------------------
// ITmfTimestamp
// ------------------------------------------------------------------------
// 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
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());
}
// ------------------------------------------------------------------------
* @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);
+ }
}
}