tmf.core: Introduce ITmfTimestamp#toNanos
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / timestamp / TmfSimpleTimestamp.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Standardize on the default toString()
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.tmf.core.timestamp;
15
16 /**
17 * A simplified timestamp where scale and precision are set to 0.
18 *
19 * @author Francois Chouinard
20 */
21 public class TmfSimpleTimestamp extends TmfTimestamp {
22
23 // ------------------------------------------------------------------------
24 // Constructors
25 // ------------------------------------------------------------------------
26
27 /**
28 * Default constructor (value = 0)
29 */
30 public TmfSimpleTimestamp() {
31 this(0);
32 }
33
34 /**
35 * Full constructor
36 *
37 * @param value the timestamp value
38 */
39 public TmfSimpleTimestamp(final long value) {
40 super(value, 0);
41 }
42
43 /**
44 * Copy constructor.
45 *
46 * If the parameter is not a TmfSimpleTimestamp, the timestamp will be
47 * scaled to seconds, and the precision will be discarded.
48 *
49 * @param timestamp
50 * The timestamp to copy
51 */
52 public TmfSimpleTimestamp(final ITmfTimestamp timestamp) {
53 super(timestamp.normalize(0, ITmfTimestamp.SECOND_SCALE).getValue(), 0);
54 }
55
56 // ------------------------------------------------------------------------
57 // ITmfTimestamp
58 // ------------------------------------------------------------------------
59
60 @Override
61 public ITmfTimestamp normalize(final long offset, final int scale) {
62 if (scale == 0) {
63 return new TmfSimpleTimestamp(getValue() + offset);
64 }
65 return super.normalize(offset, scale);
66 }
67
68 @Override
69 public int compareTo(final ITmfTimestamp ts) {
70 if (ts instanceof TmfSimpleTimestamp) {
71 final long delta = getValue() - ts.getValue();
72 return (delta == 0) ? 0 : (delta > 0) ? 1 : -1;
73 }
74 return super.compareTo(ts);
75 }
76
77 @Override
78 public ITmfTimestamp getDelta(final ITmfTimestamp ts) {
79 if (ts instanceof TmfSimpleTimestamp) {
80 return new TmfTimestampDelta(getValue() - ts.getValue());
81 }
82 return super.getDelta(ts);
83 }
84
85 // ------------------------------------------------------------------------
86 // Object
87 // ------------------------------------------------------------------------
88
89 @Override
90 public int hashCode() {
91 return super.hashCode();
92 }
93
94 @Override
95 public boolean equals(final Object other) {
96 if (this == other) {
97 return true;
98 }
99 if (other == null) {
100 return false;
101 }
102 if (!(other instanceof TmfSimpleTimestamp)) {
103 return super.equals(other);
104 }
105 final TmfSimpleTimestamp ts = (TmfSimpleTimestamp) other;
106
107 return compareTo(ts) == 0;
108 }
109
110 }
This page took 0.031755 seconds and 5 git commands to generate.