/*******************************************************************************
- * Copyright (c) 2012 Ericsson
+ * Copyright (c) 2012, 2013 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: Matthew Khouzam - Initial API and implementation
+ * Contributors:
+ * Matthew Khouzam - Initial API and implementation
*******************************************************************************/
package org.eclipse.linuxtools.tmf.core.ctfadaptor;
-import java.text.DateFormat;
-import java.text.SimpleDateFormat;
-import java.util.Date;
+import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
+import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
-import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
-import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
-
-public class CtfTmfTimestamp extends TmfTimestamp {
-
- public enum TimestampType {
- FULL_DATE, DAY, NANOS, SECONDS
- }
-
- private TimestampType type;
-
- public CtfTmfTimestamp(long timestamp) {
- setValue(timestamp, -9, 0);
- type = TimestampType.DAY;
- }
-
- public void setType(TimestampType value) {
- type = value;
- }
-
- public TimestampType getType() {
- return type;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * org.eclipse.linuxtools.tmf.core.event.TmfTimestamp#getDelta(org.eclipse
- * .linuxtools.tmf.core.event.ITmfTimestamp)
- */
- @Override
- public ITmfTimestamp getDelta(ITmfTimestamp ts) {
- TmfTimestamp parent = (TmfTimestamp) super.getDelta(ts);
- long value = parent.getValue();
- long exp = parent.getScale();
- long diff = exp + 9;
- for (int i = 0; i < diff; i++) {
- value *= 10;
- }
- CtfTmfTimestamp retVal = new CtfTmfTimestamp(value);
- if (value > 100000000) {
- retVal.type = TimestampType.SECONDS;
- } else {
- retVal.type = TimestampType.NANOS;
- }
- return retVal;
- }
+/**
+ * The CTF adapter for the TMF timestamp. It's basically the same as a
+ * TmfTimestamp, but the scale is always nanoseconds, and the precision is 0.
+ *
+ * @version 1.2
+ * @author Matthew khouzam
+ */
+public final class CtfTmfTimestamp extends TmfTimestamp {
- /*
- * (non-Javadoc)
+ /**
+ * Constructor for CtfTmfTimestamp.
*
- * @see java.lang.Object#toString()
+ * @param timestamp
+ * The timestamp value (in nanoseconds)
*/
- @Override
- public String toString() {
- switch (type) {
- case DAY: {
- return dateToString();
- }
- case FULL_DATE: {
- return toFullDateString();
- }
- case NANOS: {
- return nanoToString();
- }
- case SECONDS:{
- return secondsToString();
- }
- }
- return super.toString();
- }
-
- private String secondsToString() {
- double timestamp = getValue();
- timestamp /= 1000000000;
- StringBuilder retVal = new StringBuilder();
- retVal.append(timestamp);
- retVal.append(" s"); //$NON-NLS-1$
- return retVal.toString();
- }
-
- private String nanoToString() {
- final long timestamp = getValue();
- StringBuilder retVal = new StringBuilder();
- retVal.append(timestamp);
- retVal.append(" ns"); //$NON-NLS-1$
- return retVal.toString();
- }
-
- private String dateToString() {
- final long timestamp = getValue();
- final Date d = new Date(timestamp / 1000000);
- final DateFormat df = new SimpleDateFormat("HH:mm:ss."); //$NON-NLS-1$
- final long nanos = (timestamp % 1000000000);
- StringBuilder output = new StringBuilder();
- output.append(df.format(d));
- output.append(String.format("%09d", nanos)); //$NON-NLS-1$
- return output.toString();
- }
-
- private String toFullDateString() {
- final long timestamp = getValue();
- final Date d = new Date(timestamp / 1000000);
- final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss."); //$NON-NLS-1$
- final long nanos = (timestamp % 1000000000);
- StringBuilder output = new StringBuilder();
- output.append(df.format(d));
- output.append(String.format("%09d", nanos)); //$NON-NLS-1$
- return output.toString();
+ public CtfTmfTimestamp(long timestamp) {
+ super(timestamp, ITmfTimestamp.NANOSECOND_SCALE, 0);
}
}