Skip to content

Commit

Permalink
Improve plotting error messages when the input type is bad. (#4698)
Browse files Browse the repository at this point in the history
  • Loading branch information
chipkent authored Oct 24, 2023
1 parent 79af13b commit 909eff2
Showing 1 changed file with 49 additions and 16 deletions.
65 changes: 49 additions & 16 deletions Plot/src/main/java/io/deephaven/plot/util/ArgumentValidations.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*/
package io.deephaven.plot.util;

import io.deephaven.base.verify.Require;
import io.deephaven.base.verify.RequirementFailure;
import io.deephaven.configuration.Configuration;
import io.deephaven.engine.table.ColumnDefinition;
import io.deephaven.plot.datasets.data.IndexableNumericData;
Expand Down Expand Up @@ -178,7 +180,8 @@ public static void assertIsNumericOrTimeOrCharOrComparableInstance(final Table t
final PlotInfo plotInfo) {
assertNotNull(t, "t", plotInfo);
assertIsNumericOrTimeOrCharOrComparableInstance(t, column,
createWrongColumnTypeErrorMessage(t, column, plotInfo), plotInfo);
createWrongColumnTypeErrorMessage(t, column, plotInfo, "Numeric", "Time", "Character", "Comparable"),
plotInfo);
}


Expand All @@ -196,7 +199,8 @@ public static void assertIsNumericOrTimeOrCharOrComparableInstance(final TableDe
final PlotInfo plotInfo) {
assertNotNull(t, "t", plotInfo);
assertIsNumericOrTimeOrCharOrComparableInstance(t, column,
createWrongColumnTypeErrorMessage(t, column, plotInfo), plotInfo);
createWrongColumnTypeErrorMessage(t, column, plotInfo, "Numeric", "Time", "Character", "Comparable"),
plotInfo);
}

/**
Expand Down Expand Up @@ -528,7 +532,7 @@ public static boolean isNumericOrTime(final SelectableDataSet sds, final String
*/
public static void assertIsTime(final Table t, final String column, final PlotInfo plotInfo) {
assertNotNull(t, "t", plotInfo);
assertIsTime(t, column, createWrongColumnTypeErrorMessage(t, column, plotInfo), plotInfo);
assertIsTime(t, column, createWrongColumnTypeErrorMessage(t, column, plotInfo, "Time"), plotInfo);
}

/**
Expand All @@ -541,7 +545,7 @@ public static void assertIsTime(final Table t, final String column, final PlotIn
*/
public static void assertIsTime(final TableDefinition t, final String column, final PlotInfo plotInfo) {
assertNotNull(t, "t", plotInfo);
assertIsTime(t, column, createWrongColumnTypeErrorMessage(t, column, plotInfo), plotInfo);
assertIsTime(t, column, createWrongColumnTypeErrorMessage(t, column, plotInfo, "Time"), plotInfo);
}

/**
Expand Down Expand Up @@ -588,7 +592,8 @@ public static void assertIsTime(final TableDefinition t, final String column, fi
*/
public static void assertIsPrimitiveNumeric(final Table t, final String column, final PlotInfo plotInfo) {
assertNotNull(t, "t", plotInfo);
assertIsPrimitiveNumeric(t, column, createWrongColumnTypeErrorMessage(t, column, plotInfo), plotInfo);
assertIsPrimitiveNumeric(t, column, createWrongColumnTypeErrorMessage(t, column, plotInfo, "primitive Numeric"),
plotInfo);
}

/**
Expand Down Expand Up @@ -620,7 +625,8 @@ public static void assertIsPrimitiveNumeric(final Table t, final String column,
*/
public static void assertIsBoxedNumeric(final Table t, final String column, final PlotInfo plotInfo) {
assertNotNull(t, "t", plotInfo);
assertIsBoxedNumeric(t, column, createWrongColumnTypeErrorMessage(t, column, plotInfo), plotInfo);
assertIsBoxedNumeric(t, column, createWrongColumnTypeErrorMessage(t, column, plotInfo, "boxed Numeric"),
plotInfo);
}

/**
Expand Down Expand Up @@ -652,7 +658,7 @@ public static void assertIsBoxedNumeric(final Table t, final String column, fina
*/
public static void assertIsNumeric(final Table t, final String column, final PlotInfo plotInfo) {
assertNotNull(t, "t", plotInfo);
assertIsNumeric(t, column, createWrongColumnTypeErrorMessage(t, column, plotInfo), plotInfo);
assertIsNumeric(t, column, createWrongColumnTypeErrorMessage(t, column, plotInfo, "Numeric"), plotInfo);
}


Expand All @@ -666,7 +672,7 @@ public static void assertIsNumeric(final Table t, final String column, final Plo
*/
public static void assertIsNumeric(final TableDefinition t, final String column, final PlotInfo plotInfo) {
assertNotNull(t, "t", plotInfo);
assertIsNumeric(t, column, createWrongColumnTypeErrorMessage(t, column, plotInfo), plotInfo);
assertIsNumeric(t, column, createWrongColumnTypeErrorMessage(t, column, plotInfo, "Numeric"), plotInfo);
}


Expand Down Expand Up @@ -749,7 +755,7 @@ public static void assertIsNumericOrTime(final Table t, final String column, fin
*/
public static void assertIsNumericOrTime(final TableDefinition t, final String column, final PlotInfo plotInfo) {
assertNotNull(t, "t", plotInfo);
assertIsNumericOrTime(t, column, createWrongColumnTypeErrorMessage(t, column, plotInfo, "Numeric, Time"),
assertIsNumericOrTime(t, column, createWrongColumnTypeErrorMessage(t, column, plotInfo, "Numeric", "Time"),
plotInfo);
}

Expand All @@ -765,7 +771,7 @@ public static void assertIsNumericOrTime(final TableDefinition t, final String c
public static void assertIsNumericOrTime(final SelectableDataSet sds, final String column,
final PlotInfo plotInfo) {
assertNotNull(sds, "sds", plotInfo);
assertIsNumericOrTime(sds, column, createWrongColumnTypeErrorMessage(sds, column, plotInfo, "Numeric, Time"),
assertIsNumericOrTime(sds, column, createWrongColumnTypeErrorMessage(sds, column, plotInfo, "Numeric", "Time"),
plotInfo);
}

Expand Down Expand Up @@ -903,24 +909,51 @@ public static boolean nanSafeEquals(double x, double x1) {
return x == x1 || (Double.isNaN(x) && Double.isNaN(x1));
}

private static String wrongColumnTypeErrorMessage(final String column, final Class columnType,
final String... expectedTypes) {
Require.geqZero(expectedTypes.length, "expectedTypes.length");

final StringBuilder sb = new StringBuilder();
sb.append("Invalid data type in column = ").append(column).append(".");

if (expectedTypes.length == 1) {
sb.append(" Expected a ").append(expectedTypes[0]).append(" but was a ").append(columnType.getName());
} else {
sb.append(" Expected the type to be ");

for (int i = 0; i < expectedTypes.length; i++) {
if (i != 0) {
sb.append(", ");
}

if (i == expectedTypes.length) {
sb.append("or ");
}

sb.append(expectedTypes[i]);
}

sb.append(" but was a ").append(columnType.getName());
}

return sb.toString();
}

private static String createWrongColumnTypeErrorMessage(final Table t, final String column, final PlotInfo plotInfo,
final String... types) {
assertNotNull(t, "t", plotInfo);
return "Invalid data type in column = " + column + ". Expected one of " + Arrays.toString(types) + ", was "
+ getColumnType(t, column, plotInfo);
return wrongColumnTypeErrorMessage(column, getColumnType(t, column, plotInfo), types);
}

private static String createWrongColumnTypeErrorMessage(final TableDefinition t, final String column,
final PlotInfo plotInfo, final String... types) {
assertNotNull(t, "t", plotInfo);
return "Invalid data type in column = " + column + ". Expected one of " + Arrays.toString(types) + ", was "
+ getColumnType(t, column, plotInfo);
return wrongColumnTypeErrorMessage(column, getColumnType(t, column, plotInfo), types);
}

private static String createWrongColumnTypeErrorMessage(final SelectableDataSet sds, final String column,
final PlotInfo plotInfo, final String... types) {
assertNotNull(sds, "sds", plotInfo);
return "Invalid data type in column = " + column + ". Expected one of " + Arrays.toString(types) + ", was "
+ getColumnType(sds, column, plotInfo);
return wrongColumnTypeErrorMessage(column, getColumnType(sds, column, plotInfo), types);
}
}

0 comments on commit 909eff2

Please sign in to comment.