charts: Better protect ChartRange to avoid null ranges
authorGeneviève Bastien <gbastien+lttng@versatic.net>
Tue, 24 Jan 2017 18:37:42 +0000 (13:37 -0500)
committerGenevieve Bastien <gbastien+lttng@versatic.net>
Thu, 2 Feb 2017 14:52:46 +0000 (09:52 -0500)
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 <gbastien+lttng@versatic.net>
Reviewed-on: https://git.eclipse.org/r/89468
Reviewed-by: Hudson CI
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Tested-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
tmf/org.eclipse.tracecompass.tmf.chart.ui/src/org/eclipse/tracecompass/internal/tmf/chart/ui/data/ChartRange.java
tmf/org.eclipse.tracecompass.tmf.chart.ui/src/org/eclipse/tracecompass/internal/tmf/chart/ui/data/ChartRangeMap.java
tmf/org.eclipse.tracecompass.tmf.chart.ui/src/org/eclipse/tracecompass/internal/tmf/chart/ui/format/ChartDecimalUnitFormat.java
tmf/org.eclipse.tracecompass.tmf.chart.ui/src/org/eclipse/tracecompass/internal/tmf/chart/ui/format/ChartTimeStampFormat.java

index 2c1ac0fd78641cc55bbf0e1de49085ced222e31b..bef9f3b660b51e5f0b487fe20613e3818db4e8c5 100644 (file)
@@ -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
index 0878d6358cba7eac3f487815b037109c3181c34b..0ac956c41d4317efd7b3f7462fa9c0cf7f3ab369 100644 (file)
@@ -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$
+    }
+
 }
index 1f3b8bff7f763d2edb8b6e038e0b3b05608a6be2..6262ee19a574638f54e0b3e03fdd252e72cece0a 100644 (file)
@@ -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);
index 2711286f81ab003b9abea40cad39705aeeb295a4..ff4712cb3c9158101fdebd26093a03a21aaf75ab 100644 (file)
@@ -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())));
This page took 0.027562 seconds and 5 git commands to generate.