Commit | Line | Data |
---|---|---|
0a772436 GAPG |
1 | /******************************************************************************* |
2 | * Copyright (c) 2016 EfficiOS Inc., Jonathan Rajotte-Julien | |
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 | ||
10 | package org.eclipse.tracecompass.internal.tmf.chart.ui.data; | |
11 | ||
12 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; | |
13 | ||
14 | import java.math.BigDecimal; | |
15 | ||
16 | /** | |
4f5813eb | 17 | * BigDecimal based range representation. The chart range cannot be 0. |
0a772436 | 18 | * |
0c927289 GB |
19 | * TODO: See if this chart range is specific to the swtchart charts that are |
20 | * implemented in this plugin or if they can be re-used in another other | |
21 | * charting scheme. We'll probably know when we actually have another | |
22 | * implementation. If swtchart specific, the name of the class and package | |
23 | * should make it clear. | |
24 | * | |
0a772436 GAPG |
25 | * @author Jonathan Rajotte-Julien |
26 | */ | |
27 | public class ChartRange { | |
28 | ||
29 | // ------------------------------------------------------------------------ | |
30 | // Members | |
31 | // ------------------------------------------------------------------------ | |
32 | ||
33 | private BigDecimal fMinimum; | |
34 | private BigDecimal fMaximum; | |
35 | private BigDecimal fRange; | |
36 | ||
37 | // ------------------------------------------------------------------------ | |
38 | // Constructors | |
39 | // ------------------------------------------------------------------------ | |
40 | ||
41 | /** | |
42 | * Constructor. | |
43 | */ | |
44 | public ChartRange() { | |
45 | fMinimum = checkNotNull(BigDecimal.ZERO); | |
46 | fMaximum = checkNotNull(BigDecimal.ONE); | |
47 | fRange = checkNotNull(getMaximum().subtract(getMinimum())); | |
48 | } | |
49 | ||
50 | /** | |
51 | * Constructor with minimum and maximum values supplied. | |
52 | * | |
53 | * @param minimum | |
54 | * The minimum value of the range | |
55 | * @param maximum | |
56 | * The maximum value of the range | |
57 | */ | |
58 | public ChartRange(BigDecimal minimum, BigDecimal maximum) { | |
4f5813eb GB |
59 | BigDecimal subtract = maximum.subtract(minimum); |
60 | if (minimum.compareTo(maximum) > 0) { | |
61 | throw new IllegalArgumentException("ChartRange: minimum should be lower than or equal to the maximum (min: " + minimum + ", max: " + maximum + ')'); //$NON-NLS-1$ //$NON-NLS-2$ | |
62 | } | |
63 | if (BigDecimal.ZERO.equals(subtract)) { | |
0c927289 GB |
64 | /* |
65 | * Minimum and maximum values are all the same, so add 1 to the | |
66 | * minimum | |
67 | */ | |
4f5813eb GB |
68 | fMinimum = minimum; |
69 | fMaximum = minimum.add(BigDecimal.ONE); | |
70 | fRange = checkNotNull(BigDecimal.ONE); | |
71 | } else { | |
72 | fMinimum = minimum; | |
73 | fMaximum = maximum; | |
74 | fRange = checkNotNull(subtract); | |
75 | } | |
0a772436 GAPG |
76 | } |
77 | ||
78 | // ------------------------------------------------------------------------ | |
79 | // Accessors | |
80 | // ------------------------------------------------------------------------ | |
81 | ||
82 | /** | |
83 | * Accessor that returns the lower bound of the range. | |
84 | * | |
85 | * @return The minimum value of the range | |
86 | */ | |
87 | public BigDecimal getMinimum() { | |
88 | return fMinimum; | |
89 | } | |
90 | ||
91 | /** | |
92 | * Accessor that returns the upper bound of the range. | |
93 | * | |
94 | * @return The maximum value of the range | |
95 | */ | |
96 | public BigDecimal getMaximum() { | |
97 | return fMaximum; | |
98 | } | |
99 | ||
100 | /** | |
101 | * Accessor that returns the difference between the lower and the upper | |
102 | * bounds of the range. | |
103 | * | |
104 | * @return The range delta | |
105 | */ | |
106 | public BigDecimal getDelta() { | |
107 | return fRange; | |
108 | } | |
109 | ||
0a772436 GAPG |
110 | // ------------------------------------------------------------------------ |
111 | // Operations | |
112 | // ------------------------------------------------------------------------ | |
113 | ||
114 | /** | |
115 | * This method clamps the positive minimum value of this range down to zero. | |
116 | * It returns the current object back with the minimum value modified. | |
117 | * | |
118 | * @return The current range map | |
119 | */ | |
120 | public ChartRange clamp() { | |
4f5813eb GB |
121 | if (fMinimum.compareTo(BigDecimal.ZERO) > 0) { |
122 | fMinimum = checkNotNull(BigDecimal.ZERO); | |
123 | fRange = fMaximum; | |
124 | } | |
0a772436 GAPG |
125 | |
126 | return this; | |
127 | } | |
128 | ||
4f5813eb GB |
129 | @Override |
130 | public String toString() { | |
131 | return "ChartRange: [" + fMinimum + ", " + fMaximum + "]"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ | |
132 | } | |
133 | ||
0a772436 | 134 | } |