tmf: Fix the actual end time of state system modules
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / timestamp / TmfTimestampDelta.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 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 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.tracecompass.tmf.core.timestamp;
13
14 import java.util.TimeZone;
15
16 /**
17 * A generic timestamp implementation for delta between timestamps. The
18 * toString() method takes negative values into consideration.
19 *
20 * @author Bernd Hufmann
21 */
22 public class TmfTimestampDelta extends TmfTimestamp {
23
24 // ------------------------------------------------------------------------
25 // Members
26 // ------------------------------------------------------------------------
27
28 private final long fValue;
29 private final int fScale;
30
31 // ------------------------------------------------------------------------
32 // Constructors
33 // ------------------------------------------------------------------------
34 /**
35 * Default constructor
36 */
37 public TmfTimestampDelta() {
38 this(0, ITmfTimestamp.SECOND_SCALE);
39 }
40
41 /**
42 * Simple constructor (scale = precision = 0)
43 *
44 * @param value
45 * the timestamp value
46 */
47
48 public TmfTimestampDelta(long value) {
49 this(value, ITmfTimestamp.SECOND_SCALE);
50 }
51
52 /**
53 * Constructor
54 *
55 * @param value
56 * the timestamp value
57 * @param scale
58 * the timestamp scale
59 */
60 public TmfTimestampDelta(long value, int scale) {
61 fValue = value;
62 fScale = scale;
63 }
64
65 /**
66 * Copy constructor
67 *
68 * @param timestamp
69 * the timestamp to copy
70 */
71 public TmfTimestampDelta(ITmfTimestamp timestamp) {
72 this(timestamp.getValue(), timestamp.getScale());
73 }
74
75 // ------------------------------------------------------------------------
76 // Operations
77 // ------------------------------------------------------------------------
78
79 @Override
80 public long getValue() {
81 return fValue;
82 }
83
84 @Override
85 public int getScale() {
86 return fScale;
87 }
88
89 @Override
90 public ITmfTimestamp normalize(final long offset, final int scale) {
91 ITmfTimestamp nts = super.normalize(offset, scale);
92 return new TmfTimestampDelta(nts.getValue(), nts.getScale());
93 }
94
95 @Override
96 public String toString() {
97 return toString(TmfTimestampFormat.getDefaulIntervalFormat());
98 }
99
100 @Override
101 public String toString(TmfTimestampFormat format) {
102 if (getValue() < 0) {
103 TmfTimestampDelta tmpTs = new TmfTimestampDelta(-getValue(), getScale());
104 return "-" + tmpTs.toString(format); //$NON-NLS-1$
105 }
106 TmfTimestampFormat deltaFormat = new TmfTimestampFormat(format.toPattern());
107 deltaFormat.setTimeZone(TimeZone.getTimeZone("UTC")); //$NON-NLS-1$
108 return super.toString(deltaFormat);
109 }
110 }
This page took 0.034146 seconds and 5 git commands to generate.