Commit | Line | Data |
---|---|---|
16035098 | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2013, 2014 Ericsson |
16035098 PT |
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 | ||
2bdf0193 | 14 | package org.eclipse.tracecompass.tmf.core.timestamp; |
16035098 PT |
15 | |
16 | /** | |
17 | * A simplified timestamp where scale is nanoseconds and precision is set to 0. | |
16035098 | 18 | */ |
b2c971ec MK |
19 | public final class TmfNanoTimestamp extends TmfTimestamp { |
20 | ||
21 | private final long fValue; | |
16035098 PT |
22 | |
23 | // ------------------------------------------------------------------------ | |
24 | // Constructors | |
25 | // ------------------------------------------------------------------------ | |
26 | ||
27 | /** | |
28 | * Default constructor (value = 0) | |
29 | */ | |
30 | public TmfNanoTimestamp() { | |
31 | this(0); | |
32 | } | |
33 | ||
34 | /** | |
35 | * Full constructor | |
36 | * | |
42e85940 MK |
37 | * @param value |
38 | * the timestamp value | |
16035098 PT |
39 | */ |
40 | public TmfNanoTimestamp(final long value) { | |
b2c971ec | 41 | fValue = value; |
16035098 PT |
42 | } |
43 | ||
b2c971ec MK |
44 | @Override |
45 | public long getValue() { | |
46 | return fValue; | |
47 | } | |
48 | ||
49 | ||
50 | @Override | |
51 | public int getScale() { | |
52 | return ITmfTimestamp.NANOSECOND_SCALE; | |
16035098 PT |
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) { | |
9e925522 | 62 | return new TmfNanoTimestamp(saturatedAdd(getValue(), offset)); |
16035098 PT |
63 | } |
64 | return super.normalize(offset, scale); | |
65 | } | |
66 | ||
67 | @Override | |
065cc19b | 68 | public int compareTo(final ITmfTimestamp ts) { |
16035098 PT |
69 | if (ts instanceof TmfNanoTimestamp) { |
70 | final long delta = getValue() - ts.getValue(); | |
71 | return (delta == 0) ? 0 : (delta > 0) ? 1 : -1; | |
72 | } | |
065cc19b | 73 | return super.compareTo(ts); |
16035098 PT |
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 | ||
42e85940 MK |
84 | /** |
85 | * @since 2.0 | |
86 | */ | |
87 | @Override | |
9e925522 | 88 | public long toNanos() { |
42e85940 MK |
89 | return getValue(); |
90 | } | |
91 | ||
16035098 PT |
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 | ||
065cc19b | 114 | return compareTo(ts) == 0; |
16035098 PT |
115 | } |
116 | ||
117 | } |