From 011ac1fa22b2831d33c1ce1b78355f1f8fa88cdd Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Fri, 21 Oct 2016 19:17:50 -0700 Subject: [PATCH] Java: Allow any Number type (not just Double) to be passed to putValue(). (#129) Also improve exception message. Fixes #72. --- .../first/wpilibj/networktables/NetworkTable.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/java/src/edu/wpi/first/wpilibj/networktables/NetworkTable.java b/java/src/edu/wpi/first/wpilibj/networktables/NetworkTable.java index 40e5c4f6c0..0c4d2169f7 100644 --- a/java/src/edu/wpi/first/wpilibj/networktables/NetworkTable.java +++ b/java/src/edu/wpi/first/wpilibj/networktables/NetworkTable.java @@ -164,10 +164,10 @@ public class NetworkTable implements ITable, IRemote { return out; } - public static double[] toNative(Double[] arr) { + public static double[] toNative(Number[] arr) { double[] out = new double[arr.length]; for (int i = 0; i < arr.length; i++) - out[i] = arr[i]; + out[i] = arr[i].doubleValue(); return out; } @@ -852,8 +852,8 @@ public class NetworkTable implements ITable, IRemote { public boolean putValue(String key, Object value) throws IllegalArgumentException { if (value instanceof Boolean) return NetworkTablesJNI.putBoolean(pathWithSep + key, ((Boolean)value).booleanValue()); - else if (value instanceof Double) - return NetworkTablesJNI.putDouble(pathWithSep + key, ((Double)value).doubleValue()); + else if (value instanceof Number) + return NetworkTablesJNI.putDouble(pathWithSep + key, ((Number)value).doubleValue()); else if (value instanceof String) return NetworkTablesJNI.putString(pathWithSep + key, (String)value); else if (value instanceof byte[]) @@ -864,8 +864,8 @@ public class NetworkTable implements ITable, IRemote { return NetworkTablesJNI.putDoubleArray(pathWithSep + key, (double[])value); else if (value instanceof Boolean[]) return NetworkTablesJNI.putBooleanArray(pathWithSep + key, toNative((Boolean[])value)); - else if (value instanceof Double[]) - return NetworkTablesJNI.putDoubleArray(pathWithSep + key, toNative((Double[])value)); + else if (value instanceof Number[]) + return NetworkTablesJNI.putDoubleArray(pathWithSep + key, toNative((Number[])value)); else if (value instanceof String[]) return NetworkTablesJNI.putStringArray(pathWithSep + key, (String[])value); else if (value instanceof BooleanArray) @@ -875,7 +875,7 @@ public class NetworkTable implements ITable, IRemote { else if (value instanceof StringArray) return NetworkTablesJNI.putStringArray(pathWithSep + key, (String[])((ArrayData)value).getDataArray()); else - throw new IllegalArgumentException(key); + throw new IllegalArgumentException("Value of type " + value.getClass().getName() + " cannot be put into a table"); } /**