/*******************************************************************************
* 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 {
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)
*/
- 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
* 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) {
- if (timestamp == null)
+ if (timestamp == null) {
throw new IllegalArgumentException();
+ }
fValue = timestamp.getValue();
fScale = timestamp.getScale();
fPrecision = timestamp.getPrecision();
* @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) {
}
// 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);
}
@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) {
// 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();
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());
}
// ------------------------------------------------------------------------
*/
@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;
}
* @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);
+ }
}
}