Commit | Line | Data |
---|---|---|
a9ee1687 | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2012, 2014 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 | *******************************************************************************/ | |
2bdf0193 | 12 | package org.eclipse.tracecompass.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 | |
a9ee1687 BH |
21 | */ |
22 | public class TmfTimestampDelta extends TmfTimestamp { | |
23 | ||
24 | // ------------------------------------------------------------------------ | |
25 | // Members | |
26 | // ------------------------------------------------------------------------ | |
27 | ||
28 | // ------------------------------------------------------------------------ | |
29 | // Constructors | |
30 | // ------------------------------------------------------------------------ | |
31 | /** | |
32 | * Default constructor | |
33 | */ | |
34 | public TmfTimestampDelta() { | |
35 | super(); | |
36 | } | |
37 | ||
38 | /** | |
39 | * Simple constructor (scale = precision = 0) | |
40 | * | |
41 | * @param value the timestamp value | |
42 | */ | |
43 | ||
44 | public TmfTimestampDelta(long value) { | |
45 | super(value); | |
46 | } | |
47 | ||
48 | /** | |
49 | * Simple constructor (precision = 0) | |
50 | * | |
51 | * @param value the timestamp value | |
52 | * @param scale the timestamp scale | |
53 | */ | |
54 | public TmfTimestampDelta(long value, int scale) { | |
55 | super(value, scale); | |
56 | } | |
57 | ||
58 | ||
59 | /** | |
60 | * Copy constructor | |
61 | * | |
62 | * @param timestamp the timestamp to copy | |
63 | */ | |
64 | public TmfTimestampDelta(ITmfTimestamp timestamp) { | |
65 | super(timestamp); | |
66 | } | |
67 | ||
a9ee1687 BH |
68 | // ------------------------------------------------------------------------ |
69 | // Operations | |
70 | // ------------------------------------------------------------------------ | |
71 | ||
a9ee1687 BH |
72 | @Override |
73 | public ITmfTimestamp normalize(final long offset, final int scale) { | |
74 | ITmfTimestamp nts = super.normalize(offset, scale); | |
065cc19b | 75 | return new TmfTimestampDelta(nts.getValue(), nts.getScale()); |
a9ee1687 BH |
76 | } |
77 | ||
a9ee1687 BH |
78 | @Override |
79 | public String toString() { | |
80 | return toString(TmfTimestampFormat.getDefaulIntervalFormat()); | |
81 | } | |
82 | ||
a9ee1687 BH |
83 | @Override |
84 | public String toString(TmfTimestampFormat format) { | |
85 | if (getValue() < 0) { | |
065cc19b | 86 | TmfTimestampDelta tmpTs = new TmfTimestampDelta(-getValue(), getScale()); |
a9ee1687 BH |
87 | return "-" + tmpTs.toString(format); //$NON-NLS-1$ |
88 | } | |
89 | TmfTimestampFormat deltaFormat = new TmfTimestampFormat(format.toPattern()); | |
90 | deltaFormat.setTimeZone(TimeZone.getTimeZone("UTC")); //$NON-NLS-1$ | |
91 | return super.toString(deltaFormat); | |
92 | } | |
93 | } |