9e1deddc46d4cd29dbfa30159b129d11266e1a69
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfTimestamp.java
1 /*******************************************************************************
2 * Copyright (c) 2012 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * 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: Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11
12 package org.eclipse.linuxtools.tmf.core.ctfadaptor;
13
14 import java.text.DateFormat;
15 import java.text.SimpleDateFormat;
16 import java.util.Date;
17
18 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
19 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
20
21 /**
22 */
23 public class CtfTmfTimestamp extends TmfTimestamp {
24
25 /**
26 */
27 public enum TimestampType {
28 FULL_DATE, DAY, NANOS, SECONDS
29 }
30
31 private TimestampType type;
32
33 /**
34 * Constructor for CtfTmfTimestamp.
35 * @param timestamp long
36 */
37 public CtfTmfTimestamp(long timestamp) {
38 setValue(timestamp, -9, 0);
39 type = TimestampType.DAY;
40 }
41
42 /**
43 * Method setType.
44 * @param value TimestampType
45 */
46 public void setType(TimestampType value) {
47 type = value;
48 }
49
50 /**
51 * Method getType.
52 * @return TimestampType
53 */
54 public TimestampType getType() {
55 return type;
56 }
57
58 /*
59 * (non-Javadoc)
60 *
61 * @see
62 * org.eclipse.linuxtools.tmf.core.event.TmfTimestamp#getDelta(org.eclipse
63 * .linuxtools.tmf.core.event.ITmfTimestamp)
64 */
65 /**
66 * Method getDelta.
67 * @param ts ITmfTimestamp
68 * @return ITmfTimestamp
69 * @see org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp#getDelta(ITmfTimestamp)
70 */
71 @Override
72 public ITmfTimestamp getDelta(ITmfTimestamp ts) {
73 TmfTimestamp parent = (TmfTimestamp) super.getDelta(ts);
74 long value = parent.getValue();
75 long exp = parent.getScale();
76 long diff = exp + 9;
77 for (int i = 0; i < diff; i++) {
78 value *= 10;
79 }
80 CtfTmfTimestamp retVal = new CtfTmfTimestamp(value);
81 if (value > 100000000) {
82 retVal.type = TimestampType.SECONDS;
83 } else {
84 retVal.type = TimestampType.NANOS;
85 }
86 return retVal;
87 }
88
89 /*
90 * (non-Javadoc)
91 *
92 * @see java.lang.Object#toString()
93 */
94 @Override
95 public String toString() {
96 switch (type) {
97 case DAY: {
98 return dateToString();
99 }
100 case FULL_DATE: {
101 return toFullDateString();
102 }
103 case NANOS: {
104 return nanoToString();
105 }
106 case SECONDS:{
107 return secondsToString();
108 }
109 }
110 return super.toString();
111 }
112
113 /**
114 * Method secondsToString.
115 * @return String
116 */
117 private String secondsToString() {
118 double timestamp = getValue();
119 timestamp /= 1000000000;
120 StringBuilder retVal = new StringBuilder();
121 retVal.append(timestamp);
122 retVal.append(" s"); //$NON-NLS-1$
123 return retVal.toString();
124 }
125
126 /**
127 * Method nanoToString.
128 * @return String
129 */
130 private String nanoToString() {
131 final long timestamp = getValue();
132 StringBuilder retVal = new StringBuilder();
133 retVal.append(timestamp);
134 retVal.append(" ns"); //$NON-NLS-1$
135 return retVal.toString();
136 }
137
138 /**
139 * Method dateToString.
140 * @return String
141 */
142 private String dateToString() {
143 final long timestamp = getValue();
144 final Date d = new Date(timestamp / 1000000);
145 final DateFormat df = new SimpleDateFormat("HH:mm:ss."); //$NON-NLS-1$
146 final long nanos = (timestamp % 1000000000);
147 StringBuilder output = new StringBuilder();
148 output.append(df.format(d));
149 output.append(String.format("%09d", nanos)); //$NON-NLS-1$
150 return output.toString();
151 }
152
153 /**
154 * Method toFullDateString.
155 * @return String
156 */
157 private String toFullDateString() {
158 final long timestamp = getValue();
159 final Date d = new Date(timestamp / 1000000);
160 final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss."); //$NON-NLS-1$
161 final long nanos = (timestamp % 1000000000);
162 StringBuilder output = new StringBuilder();
163 output.append(df.format(d));
164 output.append(String.format("%09d", nanos)); //$NON-NLS-1$
165 return output.toString();
166 }
167
168 /* (non-Javadoc)
169 * @see java.lang.Object#hashCode()
170 */
171 @Override
172 public int hashCode() {
173 final int prime = 31;
174 int result = super.hashCode() * prime;
175 result += ((type == null) ? 0 : type.toString().hashCode());
176 return result;
177 }
178
179 /* (non-Javadoc)
180 * @see java.lang.Object#equals(java.lang.Object)
181 */
182 @Override
183 public boolean equals(Object obj) {
184 if (this == obj) {
185 return true;
186 }
187 if (!super.equals(obj)) {
188 return false;
189 }
190 if (!(obj instanceof CtfTmfTimestamp)) {
191 return false;
192 }
193 CtfTmfTimestamp other = (CtfTmfTimestamp) obj;
194 if (type != other.type) {
195 return false;
196 }
197 return true;
198 }
199
200 }
This page took 0.035119 seconds and 5 git commands to generate.