From 4f5813ebdf5eddbeeba500865cc76bf19e26b1cc Mon Sep 17 00:00:00 2001 From: =?utf8?q?Genevi=C3=A8ve=20Bastien?= Date: Tue, 24 Jan 2017 13:37:42 -0500 Subject: [PATCH] charts: Better protect ChartRange to avoid null ranges MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Also: * Add toString methods to chart range and range map * Remove setMinimum and setMaximum mutators (they are not used) * Add methods to get the pattern of the timestamp formatter Change-Id: I4ea0fd34c06af8e09194cd6bfb5d256b334d0993 Signed-off-by: Geneviève Bastien Reviewed-on: https://git.eclipse.org/r/89468 Reviewed-by: Hudson CI Reviewed-by: Matthew Khouzam Tested-by: Matthew Khouzam --- .../tmf/chart/ui/data/ChartRange.java | 64 +++++++------------ .../tmf/chart/ui/data/ChartRangeMap.java | 5 ++ .../ui/format/ChartDecimalUnitFormat.java | 10 --- .../chart/ui/format/ChartTimeStampFormat.java | 19 +++--- 4 files changed, 39 insertions(+), 59 deletions(-) diff --git a/tmf/org.eclipse.tracecompass.tmf.chart.ui/src/org/eclipse/tracecompass/internal/tmf/chart/ui/data/ChartRange.java b/tmf/org.eclipse.tracecompass.tmf.chart.ui/src/org/eclipse/tracecompass/internal/tmf/chart/ui/data/ChartRange.java index 2c1ac0fd78..bef9f3b660 100644 --- a/tmf/org.eclipse.tracecompass.tmf.chart.ui/src/org/eclipse/tracecompass/internal/tmf/chart/ui/data/ChartRange.java +++ b/tmf/org.eclipse.tracecompass.tmf.chart.ui/src/org/eclipse/tracecompass/internal/tmf/chart/ui/data/ChartRange.java @@ -14,7 +14,7 @@ import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; import java.math.BigDecimal; /** - * BigDecimal based range representation + * BigDecimal based range representation. The chart range cannot be 0. * * @author Jonathan Rajotte-Julien */ @@ -50,9 +50,20 @@ public class ChartRange { * The maximum value of the range */ public ChartRange(BigDecimal minimum, BigDecimal maximum) { - fMinimum = minimum; - fMaximum = maximum; - fRange = checkNotNull(maximum.subtract(minimum)); + BigDecimal subtract = maximum.subtract(minimum); + if (minimum.compareTo(maximum) > 0) { + throw new IllegalArgumentException("ChartRange: minimum should be lower than or equal to the maximum (min: " + minimum + ", max: " + maximum + ')'); //$NON-NLS-1$ //$NON-NLS-2$ + } + if (BigDecimal.ZERO.equals(subtract)) { + // Minimum and maximum values are all the same, so add 1 to the minimum + fMinimum = minimum; + fMaximum = minimum.add(BigDecimal.ONE); + fRange = checkNotNull(BigDecimal.ONE); + } else { + fMinimum = minimum; + fMaximum = maximum; + fRange = checkNotNull(subtract); + } } // ------------------------------------------------------------------------ @@ -87,41 +98,6 @@ public class ChartRange { return fRange; } - /** - * Method that checks if the delta is equal to zero. - * - * @return {@code true} if the delta is null, else {@code false} - */ - public boolean isDeltaNull() { - return getDelta().compareTo(BigDecimal.ZERO) == 0; - } - - // ------------------------------------------------------------------------ - // Mutators - // ------------------------------------------------------------------------ - - /** - * Mutator that sets the minimum value of the range. - * - * @param minimum - * The new minimum value - */ - public void setMinimum(BigDecimal minimum) { - fMinimum = minimum; - fRange = checkNotNull(getMaximum().subtract(getMinimum())); - } - - /** - * Mutator that sets the maximum value of the range. - * - * @param maximum - * The new maximum value - */ - public void setMaximum(BigDecimal maximum) { - fMaximum = maximum; - fRange = checkNotNull(getMaximum().subtract(getMinimum())); - } - // ------------------------------------------------------------------------ // Operations // ------------------------------------------------------------------------ @@ -133,9 +109,17 @@ public class ChartRange { * @return The current range map */ public ChartRange clamp() { - fMinimum = fMinimum.min(BigDecimal.ZERO); + if (fMinimum.compareTo(BigDecimal.ZERO) > 0) { + fMinimum = checkNotNull(BigDecimal.ZERO); + fRange = fMaximum; + } return this; } + @Override + public String toString() { + return "ChartRange: [" + fMinimum + ", " + fMaximum + "]"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ + } + } \ No newline at end of file diff --git a/tmf/org.eclipse.tracecompass.tmf.chart.ui/src/org/eclipse/tracecompass/internal/tmf/chart/ui/data/ChartRangeMap.java b/tmf/org.eclipse.tracecompass.tmf.chart.ui/src/org/eclipse/tracecompass/internal/tmf/chart/ui/data/ChartRangeMap.java index 0878d6358c..0ac956c41d 100644 --- a/tmf/org.eclipse.tracecompass.tmf.chart.ui/src/org/eclipse/tracecompass/internal/tmf/chart/ui/data/ChartRangeMap.java +++ b/tmf/org.eclipse.tracecompass.tmf.chart.ui/src/org/eclipse/tracecompass/internal/tmf/chart/ui/data/ChartRangeMap.java @@ -187,4 +187,9 @@ public class ChartRangeMap { return checkNotNull(externalValue); } + @Override + public String toString() { + return "ChartRangeMap: Input Data -> " + fInputDataRange + ", Plotted -> " + fPlottedRange + "]"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ + } + } diff --git a/tmf/org.eclipse.tracecompass.tmf.chart.ui/src/org/eclipse/tracecompass/internal/tmf/chart/ui/format/ChartDecimalUnitFormat.java b/tmf/org.eclipse.tracecompass.tmf.chart.ui/src/org/eclipse/tracecompass/internal/tmf/chart/ui/format/ChartDecimalUnitFormat.java index 1f3b8bff7f..6262ee19a5 100644 --- a/tmf/org.eclipse.tracecompass.tmf.chart.ui/src/org/eclipse/tracecompass/internal/tmf/chart/ui/format/ChartDecimalUnitFormat.java +++ b/tmf/org.eclipse.tracecompass.tmf.chart.ui/src/org/eclipse/tracecompass/internal/tmf/chart/ui/format/ChartDecimalUnitFormat.java @@ -15,7 +15,6 @@ import java.text.FieldPosition; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.tracecompass.common.core.format.DecimalUnitFormat; -import org.eclipse.tracecompass.internal.tmf.chart.ui.data.ChartRange; import org.eclipse.tracecompass.internal.tmf.chart.ui.data.ChartRangeMap; /** @@ -105,15 +104,6 @@ public class ChartDecimalUnitFormat extends DecimalUnitFormat { return (buffer == null ? new StringBuffer() : buffer); } - ChartRange internalRange = rangeMap.getPlottedRange(); - ChartRange externalRange = rangeMap.getInputDataRange(); - - /* If any range's delta is null, format with the external bounds */ - if (internalRange.isDeltaNull() || externalRange.isDeltaNull()) { - StringBuffer buffer = super.format(externalRange.getMinimum().doubleValue(), toAppendTo, pos); - return (buffer == null ? new StringBuffer() : buffer); - } - /* Find external value before formatting */ Double externalValue = checkNotNull(fRangeMap).getExternalValue(number).doubleValue(); StringBuffer buffer = super.format(externalValue, toAppendTo, pos); diff --git a/tmf/org.eclipse.tracecompass.tmf.chart.ui/src/org/eclipse/tracecompass/internal/tmf/chart/ui/format/ChartTimeStampFormat.java b/tmf/org.eclipse.tracecompass.tmf.chart.ui/src/org/eclipse/tracecompass/internal/tmf/chart/ui/format/ChartTimeStampFormat.java index 2711286f81..ff4712cb3c 100644 --- a/tmf/org.eclipse.tracecompass.tmf.chart.ui/src/org/eclipse/tracecompass/internal/tmf/chart/ui/format/ChartTimeStampFormat.java +++ b/tmf/org.eclipse.tracecompass.tmf.chart.ui/src/org/eclipse/tracecompass/internal/tmf/chart/ui/format/ChartTimeStampFormat.java @@ -17,7 +17,7 @@ import java.text.Format; import java.text.ParsePosition; import org.eclipse.jdt.annotation.Nullable; -import org.eclipse.tracecompass.internal.tmf.chart.ui.data.ChartRange; +import org.eclipse.tracecompass.common.core.NonNullUtils; import org.eclipse.tracecompass.internal.tmf.chart.ui.data.ChartRangeMap; import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestampFormat; @@ -84,6 +84,15 @@ public class ChartTimeStampFormat extends Format { fRangeMap = map; } + /** + * Get the pattern string of the format. + * + * @return the pattern string. + */ + public String getPattern() { + return NonNullUtils.checkNotNull(fFormat.toPattern()); + } + // ------------------------------------------------------------------------ // Operations // ------------------------------------------------------------------------ @@ -103,14 +112,6 @@ public class ChartTimeStampFormat extends Format { return checkNotNull(toAppendTo.append(fFormat.format(time))); } - ChartRange internalRange = rangeMap.getPlottedRange(); - ChartRange externalRange = rangeMap.getInputDataRange(); - - /* If any range's delta is null, format with the external bounds */ - if (internalRange.isDeltaNull() || externalRange.isDeltaNull()) { - return checkNotNull(toAppendTo.append(fFormat.format(externalRange.getMinimum().doubleValue()))); - } - /* Find external value before formatting */ BigDecimal externalValue = checkNotNull(fRangeMap).getExternalValue(number); return checkNotNull(toAppendTo.append(fFormat.format(externalValue.longValue()))); -- 2.34.1