Commit | Line | Data |
---|---|---|
a9ee1687 | 1 | /******************************************************************************* |
61759503 | 2 | * Copyright (c) 2012, 2013 Ericsson |
a9ee1687 BH |
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 | *******************************************************************************/ | |
3bd46eef | 12 | package org.eclipse.linuxtools.tmf.core.timestamp; |
a9ee1687 BH |
13 | |
14 | import java.util.TimeZone; | |
15 | ||
16 | /** | |
17 | * A generic timestamp implementation for delta between timestamps. | |
18 | * The toString() method takes negative values into consideration. | |
19 | * | |
20 | * @author Bernd Hufmann | |
21 | * @since 2.0 | |
22 | */ | |
23 | public class TmfTimestampDelta extends TmfTimestamp { | |
24 | ||
25 | // ------------------------------------------------------------------------ | |
26 | // Members | |
27 | // ------------------------------------------------------------------------ | |
28 | ||
29 | // ------------------------------------------------------------------------ | |
30 | // Constructors | |
31 | // ------------------------------------------------------------------------ | |
32 | /** | |
33 | * Default constructor | |
34 | */ | |
35 | public TmfTimestampDelta() { | |
36 | super(); | |
37 | } | |
38 | ||
39 | /** | |
40 | * Simple constructor (scale = precision = 0) | |
41 | * | |
42 | * @param value the timestamp value | |
43 | */ | |
44 | ||
45 | public TmfTimestampDelta(long value) { | |
46 | super(value); | |
47 | } | |
48 | ||
49 | /** | |
50 | * Simple constructor (precision = 0) | |
51 | * | |
52 | * @param value the timestamp value | |
53 | * @param scale the timestamp scale | |
54 | */ | |
55 | public TmfTimestampDelta(long value, int scale) { | |
56 | super(value, scale); | |
57 | } | |
58 | ||
59 | ||
60 | /** | |
61 | * Copy constructor | |
62 | * | |
63 | * @param timestamp the timestamp to copy | |
64 | */ | |
65 | public TmfTimestampDelta(ITmfTimestamp timestamp) { | |
66 | super(timestamp); | |
67 | } | |
68 | ||
69 | /** | |
70 | * Full constructor | |
71 | * | |
72 | * @param value the timestamp value | |
73 | * @param scale the timestamp scale | |
74 | * @param precision the timestamp precision | |
75 | */ | |
76 | public TmfTimestampDelta(long value, int scale, int precision) { | |
77 | super(value, scale, precision); | |
78 | } | |
79 | ||
80 | // ------------------------------------------------------------------------ | |
81 | // Operations | |
82 | // ------------------------------------------------------------------------ | |
83 | ||
a9ee1687 BH |
84 | @Override |
85 | public ITmfTimestamp normalize(final long offset, final int scale) { | |
86 | ITmfTimestamp nts = super.normalize(offset, scale); | |
87 | return new TmfTimestampDelta(nts.getValue(), nts.getScale(), nts.getPrecision()); | |
88 | } | |
89 | ||
a9ee1687 BH |
90 | @Override |
91 | public String toString() { | |
92 | return toString(TmfTimestampFormat.getDefaulIntervalFormat()); | |
93 | } | |
94 | ||
a9ee1687 BH |
95 | @Override |
96 | public String toString(TmfTimestampFormat format) { | |
97 | if (getValue() < 0) { | |
98 | TmfTimestampDelta tmpTs = new TmfTimestampDelta(-getValue(), getScale(), getPrecision()); | |
99 | return "-" + tmpTs.toString(format); //$NON-NLS-1$ | |
100 | } | |
101 | TmfTimestampFormat deltaFormat = new TmfTimestampFormat(format.toPattern()); | |
102 | deltaFormat.setTimeZone(TimeZone.getTimeZone("UTC")); //$NON-NLS-1$ | |
103 | return super.toString(deltaFormat); | |
104 | } | |
105 | } |