tmf: Move timestamps to their own package
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / timestamp / TmfSimpleTimestamp.java
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 * Francois Chouinard - Standardize on the default toString()
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.core.timestamp;
15
16 /**
17 * A simplified timestamp where scale and precision are set to 0.
18 *
19 * @author Francois Chouinard
20 * @version 1.1
21 * @since 2.0
22 */
23 public class TmfSimpleTimestamp extends TmfTimestamp {
24
25 // ------------------------------------------------------------------------
26 // Constructors
27 // ------------------------------------------------------------------------
28
29 /**
30 * Default constructor (value = 0)
31 */
32 public TmfSimpleTimestamp() {
33 this(0);
34 }
35
36 /**
37 * Full constructor
38 *
39 * @param value the timestamp value
40 */
41 public TmfSimpleTimestamp(final long value) {
42 super(value, 0, 0);
43 }
44
45 /**
46 * Copy constructor.
47 *
48 * If the parameter is not a TmfSimpleTimestamp, the timestamp will be
49 * scaled to seconds, and the precision will be discarded.
50 *
51 * @param timestamp
52 * The timestamp to copy
53 */
54 public TmfSimpleTimestamp(final ITmfTimestamp timestamp) {
55 super(timestamp.normalize(0, ITmfTimestamp.SECOND_SCALE).getValue(), 0, 0);
56 }
57
58 // ------------------------------------------------------------------------
59 // ITmfTimestamp
60 // ------------------------------------------------------------------------
61
62 /* (non-Javadoc)
63 * @see org.eclipse.linuxtools.tmf.core.event.TmfTimestamp#normalize(long, int)
64 */
65 @Override
66 public ITmfTimestamp normalize(final long offset, final int scale) {
67 if (scale == 0) {
68 return new TmfSimpleTimestamp(getValue() + offset);
69 }
70 return super.normalize(offset, scale);
71 }
72
73 /* (non-Javadoc)
74 * @see org.eclipse.linuxtools.tmf.core.event.TmfTimestamp#compareTo(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp, boolean)
75 */
76 @Override
77 public int compareTo(final ITmfTimestamp ts, final boolean withinPrecision) {
78 if (ts instanceof TmfSimpleTimestamp) {
79 final long delta = getValue() - ts.getValue();
80 return (delta == 0) ? 0 : (delta > 0) ? 1 : -1;
81 }
82 return super.compareTo(ts, withinPrecision);
83 }
84
85 /* (non-Javadoc)
86 * @see org.eclipse.linuxtools.tmf.core.event.TmfTimestamp#getDelta(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
87 */
88 @Override
89 public ITmfTimestamp getDelta(final ITmfTimestamp ts) {
90 if (ts instanceof TmfSimpleTimestamp) {
91 return new TmfTimestampDelta(getValue() - ts.getValue());
92 }
93 return super.getDelta(ts);
94 }
95
96 // ------------------------------------------------------------------------
97 // Object
98 // ------------------------------------------------------------------------
99
100 /* (non-Javadoc)
101 * @see org.eclipse.linuxtools.tmf.core.event.TmfTimestamp#hashCode()
102 */
103 @Override
104 public int hashCode() {
105 return super.hashCode();
106 }
107
108 /* (non-Javadoc)
109 * @see org.eclipse.linuxtools.tmf.core.event.TmfTimestamp#equals(java.lang.Object)
110 */
111 @Override
112 public boolean equals(final Object other) {
113 if (this == other) {
114 return true;
115 }
116 if (other == null) {
117 return false;
118 }
119 if (!(other instanceof TmfSimpleTimestamp)) {
120 return super.equals(other);
121 }
122 final TmfSimpleTimestamp ts = (TmfSimpleTimestamp) other;
123
124 return compareTo(ts, false) == 0;
125 }
126
127 }
This page took 0.032773 seconds and 5 git commands to generate.