Improve javadoc for ctfAdapter in Tmf.Core
[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 /**
29 * yyyy/mm/dd hh:mm:ss.nnnnnnnnnn
30 */
31 FULL_DATE,
32 /**
33 * hh:mm:ss.nnnnnnnnnn
34 */
35 DAY,
36 /**
37 * nnnnnnnnnnnnnnnnnnnnn ns
38 */
39 NANOS,
40 /**
41 * ssssssssss.nnnnnnnnnn s
42 */
43 SECONDS
44 }
45
46 private TimestampType type;
47
48 /**
49 * Constructor for CtfTmfTimestamp.
50 * @param timestamp long
51 */
52 public CtfTmfTimestamp(long timestamp) {
53 setValue(timestamp, -9, 0);
54 type = TimestampType.DAY;
55 }
56
57 /**
58 * Method setType.
59 * @param value TimestampType
60 */
61 public void setType(TimestampType value) {
62 type = value;
63 }
64
65 /**
66 * Method getType.
67 * @return TimestampType
68 */
69 public TimestampType getType() {
70 return type;
71 }
72
73 /*
74 * (non-Javadoc)
75 *
76 * @see
77 * org.eclipse.linuxtools.tmf.core.event.TmfTimestamp#getDelta(org.eclipse
78 * .linuxtools.tmf.core.event.ITmfTimestamp)
79 */
80 /**
81 * Method getDelta.
82 * @param ts ITmfTimestamp
83 * @return ITmfTimestamp
84 * @see org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp#getDelta(ITmfTimestamp)
85 */
86 @Override
87 public ITmfTimestamp getDelta(ITmfTimestamp ts) {
88 TmfTimestamp parent = (TmfTimestamp) super.getDelta(ts);
89 long value = parent.getValue();
90 long exp = parent.getScale();
91 long diff = exp + 9;
92 for (int i = 0; i < diff; i++) {
93 value *= 10;
94 }
95 CtfTmfTimestamp retVal = new CtfTmfTimestamp(value);
96 if (value > 100000000) {
97 retVal.type = TimestampType.SECONDS;
98 } else {
99 retVal.type = TimestampType.NANOS;
100 }
101 return retVal;
102 }
103
104 /*
105 * (non-Javadoc)
106 *
107 * @see java.lang.Object#toString()
108 */
109 @Override
110 public String toString() {
111 switch (type) {
112 case DAY: {
113 return dateToString();
114 }
115 case FULL_DATE: {
116 return toFullDateString();
117 }
118 case NANOS: {
119 return nanoToString();
120 }
121 case SECONDS:{
122 return secondsToString();
123 }
124 }
125 return super.toString();
126 }
127
128 /**
129 * Method secondsToString.
130 * @return String
131 */
132 private String secondsToString() {
133 double timestamp = getValue();
134 timestamp /= 1000000000;
135 StringBuilder retVal = new StringBuilder();
136 retVal.append(timestamp);
137 retVal.append(" s"); //$NON-NLS-1$
138 return retVal.toString();
139 }
140
141 /**
142 * Method nanoToString.
143 * @return String
144 */
145 private String nanoToString() {
146 final long timestamp = getValue();
147 StringBuilder retVal = new StringBuilder();
148 retVal.append(timestamp);
149 retVal.append(" ns"); //$NON-NLS-1$
150 return retVal.toString();
151 }
152
153 /**
154 * Method dateToString.
155 * @return String
156 */
157 private String dateToString() {
158 final long timestamp = getValue();
159 final Date d = new Date(timestamp / 1000000);
160 final DateFormat df = new SimpleDateFormat("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 /**
169 * Method toFullDateString.
170 * @return String
171 */
172 private String toFullDateString() {
173 final long timestamp = getValue();
174 final Date d = new Date(timestamp / 1000000);
175 final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss."); //$NON-NLS-1$
176 final long nanos = (timestamp % 1000000000);
177 StringBuilder output = new StringBuilder();
178 output.append(df.format(d));
179 output.append(String.format("%09d", nanos)); //$NON-NLS-1$
180 return output.toString();
181 }
182
183 /* (non-Javadoc)
184 * @see java.lang.Object#hashCode()
185 */
186 @Override
187 public int hashCode() {
188 final int prime = 31;
189 int result = super.hashCode() * prime;
190 result += ((type == null) ? 0 : type.toString().hashCode());
191 return result;
192 }
193
194 /* (non-Javadoc)
195 * @see java.lang.Object#equals(java.lang.Object)
196 */
197 @Override
198 public boolean equals(Object obj) {
199 if (this == obj) {
200 return true;
201 }
202 if (!super.equals(obj)) {
203 return false;
204 }
205 if (!(obj instanceof CtfTmfTimestamp)) {
206 return false;
207 }
208 CtfTmfTimestamp other = (CtfTmfTimestamp) obj;
209 if (type != other.type) {
210 return false;
211 }
212 return true;
213 }
214
215 }
This page took 0.035706 seconds and 5 git commands to generate.