a277bf2e8632c481d0658df3cbadc141a7f90537
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / timestamp / TmfNanoTimestamp.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 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 * Patrick Tasse - Modified from TmfSimpleTimestamp to use nanosecond scale
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.tmf.core.timestamp;
15
16 /**
17 * A simplified timestamp where scale is nanoseconds and precision is set to 0.
18 */
19 public class TmfNanoTimestamp extends TmfTimestamp {
20
21 // ------------------------------------------------------------------------
22 // Constructors
23 // ------------------------------------------------------------------------
24
25 /**
26 * Default constructor (value = 0)
27 */
28 public TmfNanoTimestamp() {
29 this(0);
30 }
31
32 /**
33 * Full constructor
34 *
35 * @param value
36 * the timestamp value
37 */
38 public TmfNanoTimestamp(final long value) {
39 super(value, ITmfTimestamp.NANOSECOND_SCALE);
40 }
41
42 /**
43 * Copy constructor.
44 *
45 * If the parameter is not a TmfNanoTimestamp, the timestamp will be scaled
46 * to nanoseconds, and the precision will be discarded.
47 *
48 * @param timestamp
49 * The timestamp to copy
50 */
51 public TmfNanoTimestamp(final ITmfTimestamp timestamp) {
52 super(timestamp.normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue(), ITmfTimestamp.NANOSECOND_SCALE);
53 }
54
55 // ------------------------------------------------------------------------
56 // ITmfTimestamp
57 // ------------------------------------------------------------------------
58
59 @Override
60 public ITmfTimestamp normalize(final long offset, final int scale) {
61 if (scale == ITmfTimestamp.NANOSECOND_SCALE) {
62 return new TmfNanoTimestamp(getValue() + offset);
63 }
64 return super.normalize(offset, scale);
65 }
66
67 @Override
68 public int compareTo(final ITmfTimestamp ts) {
69 if (ts instanceof TmfNanoTimestamp) {
70 final long delta = getValue() - ts.getValue();
71 return (delta == 0) ? 0 : (delta > 0) ? 1 : -1;
72 }
73 return super.compareTo(ts);
74 }
75
76 @Override
77 public ITmfTimestamp getDelta(final ITmfTimestamp ts) {
78 if (ts instanceof TmfNanoTimestamp) {
79 return new TmfTimestampDelta(getValue() - ts.getValue(), ITmfTimestamp.NANOSECOND_SCALE);
80 }
81 return super.getDelta(ts);
82 }
83
84 /**
85 * @since 2.0
86 */
87 @Override
88 public long toNanos(){
89 return getValue();
90 }
91
92 // ------------------------------------------------------------------------
93 // Object
94 // ------------------------------------------------------------------------
95
96 @Override
97 public int hashCode() {
98 return super.hashCode();
99 }
100
101 @Override
102 public boolean equals(final Object other) {
103 if (this == other) {
104 return true;
105 }
106 if (other == null) {
107 return false;
108 }
109 if (!(other instanceof TmfNanoTimestamp)) {
110 return super.equals(other);
111 }
112 final TmfNanoTimestamp ts = (TmfNanoTimestamp) other;
113
114 return compareTo(ts) == 0;
115 }
116
117 }
This page took 0.034158 seconds and 4 git commands to generate.