Commit | Line | Data |
---|---|---|
7636a88e | 1 | /******************************************************************************* |
2 | * Copyright (c) 2012 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 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.core.event; | |
14 | ||
15 | /** | |
b9e37ffd FC |
16 | * A simplified timestamp where scale and precision are set to 0 i.e. timestamps |
17 | * are represented by the tuple { value, 0, 0 }. | |
18 | * | |
19 | * @since 1.0 | |
20 | * @version 1.0 | |
21 | * @author Francois Chouinard | |
22 | * @see ITmfTimestamp | |
23 | * @see TmfTimestamp | |
7636a88e | 24 | */ |
25 | public class TmfSimpleTimestamp extends TmfTimestamp { | |
26 | ||
27 | // ------------------------------------------------------------------------ | |
28 | // Constructors | |
29 | // ------------------------------------------------------------------------ | |
30 | ||
31 | /** | |
32 | * Default constructor | |
33 | */ | |
34 | public TmfSimpleTimestamp() { | |
35 | this(0); | |
36 | } | |
37 | ||
38 | /** | |
39 | * Full constructor | |
40 | * | |
41 | * @param value the timestamp value | |
42 | */ | |
085d898f | 43 | public TmfSimpleTimestamp(final long value) { |
7636a88e | 44 | super(value, 0, 0); |
45 | } | |
46 | ||
47 | /** | |
48 | * Copy constructor | |
49 | * | |
50 | * @param timestamp the timestamp to copy | |
51 | */ | |
085d898f | 52 | public TmfSimpleTimestamp(final ITmfTimestamp timestamp) { |
b9e37ffd | 53 | if (timestamp == null || timestamp.getScale() != 0 || timestamp.getPrecision() != 0) { |
7636a88e | 54 | throw new IllegalArgumentException(); |
b9e37ffd FC |
55 | } |
56 | setValue(timestamp.getValue(), 0, 0); | |
7636a88e | 57 | } |
58 | ||
59 | // ------------------------------------------------------------------------ | |
60 | // ITmfTimestamp | |
61 | // ------------------------------------------------------------------------ | |
62 | ||
d7dbf09a FC |
63 | /* (non-Javadoc) |
64 | * @see org.eclipse.linuxtools.tmf.core.event.TmfTimestamp#normalize(long, int) | |
65 | */ | |
7636a88e | 66 | @Override |
085d898f | 67 | public ITmfTimestamp normalize(final long offset, final int scale) throws ArithmeticException { |
b9e37ffd FC |
68 | if (scale == 0) { |
69 | return new TmfSimpleTimestamp(getValue() + offset); | |
70 | } | |
7636a88e | 71 | return super.normalize(offset, scale); |
72 | } | |
73 | ||
d7dbf09a FC |
74 | /* (non-Javadoc) |
75 | * @see org.eclipse.linuxtools.tmf.core.event.TmfTimestamp#compareTo(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp, boolean) | |
76 | */ | |
7636a88e | 77 | @Override |
085d898f | 78 | public int compareTo(final ITmfTimestamp ts, final boolean withinPrecision) { |
7636a88e | 79 | if (ts instanceof TmfSimpleTimestamp) { |
b9e37ffd | 80 | final long delta = getValue() - ts.getValue(); |
7636a88e | 81 | return (delta == 0) ? 0 : (delta > 0) ? 1 : -1; |
82 | } | |
83 | return super.compareTo(ts, withinPrecision); | |
84 | } | |
85 | ||
d7dbf09a FC |
86 | /* (non-Javadoc) |
87 | * @see org.eclipse.linuxtools.tmf.core.event.TmfTimestamp#getDelta(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp) | |
88 | */ | |
7636a88e | 89 | @Override |
085d898f | 90 | public ITmfTimestamp getDelta(final ITmfTimestamp ts) { |
b9e37ffd FC |
91 | if (ts instanceof TmfSimpleTimestamp) { |
92 | return new TmfSimpleTimestamp(getValue() - ts.getValue()); | |
93 | } | |
7636a88e | 94 | return super.getDelta(ts); |
95 | } | |
96 | ||
8c149234 FC |
97 | // ------------------------------------------------------------------------ |
98 | // Cloneable | |
99 | // ------------------------------------------------------------------------ | |
100 | ||
101 | /* (non-Javadoc) | |
b9e37ffd | 102 | * @see org.eclipse.linuxtools.tmf.core.event.TmfTimestamp#clone() |
8c149234 FC |
103 | */ |
104 | @Override | |
105 | public TmfSimpleTimestamp clone() { | |
106 | return (TmfSimpleTimestamp) super.clone(); | |
107 | } | |
108 | ||
7636a88e | 109 | // ------------------------------------------------------------------------ |
110 | // Object | |
111 | // ------------------------------------------------------------------------ | |
112 | ||
beae214a FC |
113 | /* (non-Javadoc) |
114 | * @see org.eclipse.linuxtools.tmf.core.event.TmfTimestamp#hashCode() | |
115 | */ | |
116 | @Override | |
117 | public int hashCode() { | |
118 | return super.hashCode(); | |
119 | } | |
120 | ||
d7dbf09a FC |
121 | /* (non-Javadoc) |
122 | * @see org.eclipse.linuxtools.tmf.core.event.TmfTimestamp#equals(java.lang.Object) | |
123 | */ | |
7636a88e | 124 | @Override |
085d898f | 125 | public boolean equals(final Object other) { |
b9e37ffd | 126 | if (this == other) { |
7636a88e | 127 | return true; |
b9e37ffd FC |
128 | } |
129 | if (other == null) { | |
7636a88e | 130 | return false; |
b9e37ffd FC |
131 | } |
132 | if (!(other instanceof TmfSimpleTimestamp)) { | |
7636a88e | 133 | return super.equals(other); |
b9e37ffd | 134 | } |
085d898f | 135 | final TmfSimpleTimestamp ts = (TmfSimpleTimestamp) other; |
7656d70a | 136 | |
7636a88e | 137 | return compareTo(ts, false) == 0; |
138 | } | |
139 | ||
d7dbf09a FC |
140 | /* (non-Javadoc) |
141 | * @see org.eclipse.linuxtools.tmf.core.event.TmfTimestamp#toString() | |
142 | */ | |
7636a88e | 143 | @Override |
144 | @SuppressWarnings("nls") | |
145 | public String toString() { | |
b9e37ffd | 146 | return "TmfSimpleTimestamp [fValue=" + getValue() + "]"; |
7636a88e | 147 | } |
148 | ||
149 | } |