Commit | Line | Data |
---|---|---|
16035098 PT |
1 | /******************************************************************************* |
2 | * Copyright (c) 2013 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 | ||
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. | |
18 | * | |
4b121c48 | 19 | * @since 2.1 |
16035098 PT |
20 | */ |
21 | public class TmfNanoTimestamp extends TmfTimestamp { | |
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 | * | |
37 | * @param value the timestamp value | |
38 | */ | |
39 | public TmfNanoTimestamp(final long value) { | |
40 | super(value, ITmfTimestamp.NANOSECOND_SCALE, 0); | |
41 | } | |
42 | ||
43 | /** | |
44 | * Copy constructor. | |
45 | * | |
46 | * If the parameter is not a TmfNanoTimestamp, the timestamp will be | |
47 | * scaled to nanoseconds, and the precision will be discarded. | |
48 | * | |
49 | * @param timestamp | |
50 | * The timestamp to copy | |
51 | */ | |
52 | public TmfNanoTimestamp(final ITmfTimestamp timestamp) { | |
53 | super(timestamp.normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue(), ITmfTimestamp.NANOSECOND_SCALE, 0); | |
54 | } | |
55 | ||
56 | // ------------------------------------------------------------------------ | |
57 | // ITmfTimestamp | |
58 | // ------------------------------------------------------------------------ | |
59 | ||
60 | @Override | |
61 | public ITmfTimestamp normalize(final long offset, final int scale) { | |
62 | if (scale == ITmfTimestamp.NANOSECOND_SCALE) { | |
63 | return new TmfNanoTimestamp(getValue() + offset); | |
64 | } | |
65 | return super.normalize(offset, scale); | |
66 | } | |
67 | ||
68 | @Override | |
69 | public int compareTo(final ITmfTimestamp ts, final boolean withinPrecision) { | |
70 | if (ts instanceof TmfNanoTimestamp) { | |
71 | final long delta = getValue() - ts.getValue(); | |
72 | return (delta == 0) ? 0 : (delta > 0) ? 1 : -1; | |
73 | } | |
74 | return super.compareTo(ts, withinPrecision); | |
75 | } | |
76 | ||
77 | @Override | |
78 | public ITmfTimestamp getDelta(final ITmfTimestamp ts) { | |
79 | if (ts instanceof TmfNanoTimestamp) { | |
80 | return new TmfTimestampDelta(getValue() - ts.getValue(), ITmfTimestamp.NANOSECOND_SCALE); | |
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 TmfNanoTimestamp)) { | |
103 | return super.equals(other); | |
104 | } | |
105 | final TmfNanoTimestamp ts = (TmfNanoTimestamp) other; | |
106 | ||
107 | return compareTo(ts, false) == 0; | |
108 | } | |
109 | ||
110 | } |