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