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 PT |
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 | * | |
42e85940 MK |
35 | * @param value |
36 | * the timestamp value | |
16035098 PT |
37 | */ |
38 | public TmfNanoTimestamp(final long value) { | |
065cc19b | 39 | super(value, ITmfTimestamp.NANOSECOND_SCALE); |
16035098 PT |
40 | } |
41 | ||
42 | /** | |
43 | * Copy constructor. | |
44 | * | |
42e85940 MK |
45 | * If the parameter is not a TmfNanoTimestamp, the timestamp will be scaled |
46 | * to nanoseconds, and the precision will be discarded. | |
16035098 PT |
47 | * |
48 | * @param timestamp | |
49 | * The timestamp to copy | |
50 | */ | |
51 | public TmfNanoTimestamp(final ITmfTimestamp timestamp) { | |
065cc19b | 52 | super(timestamp.normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue(), 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) { | |
62 | return new TmfNanoTimestamp(getValue() + offset); | |
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 | |
88 | public long toNanos(){ | |
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 | } |