From dbe4168d8d5393a0017d7bd917e35e2ef72accda Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Fri, 28 Aug 2015 20:37:25 -0700 Subject: [PATCH] Java: allow both object arrays and native arrays. This allows easier use of things like ArrayList. --- .../wpilibj/networktables/NetworkTable.java | 96 +++++++++++++++++++ .../edu/wpi/first/wpilibj/tables/ITable.java | 26 +++++ 2 files changed, 122 insertions(+) diff --git a/java/src/edu/wpi/first/wpilibj/networktables/NetworkTable.java b/java/src/edu/wpi/first/wpilibj/networktables/NetworkTable.java index 5a06ebed66..468460c433 100644 --- a/java/src/edu/wpi/first/wpilibj/networktables/NetworkTable.java +++ b/java/src/edu/wpi/first/wpilibj/networktables/NetworkTable.java @@ -89,6 +89,34 @@ public class NetworkTable implements ITable, IRemote { port = aport; } + public static boolean[] toNative(Boolean[] arr) { + boolean[] out = new boolean[arr.length]; + for (int i = 0; i < arr.length; i++) + out[i] = arr[i]; + return out; + } + + public static double[] toNative(Double[] arr) { + double[] out = new double[arr.length]; + for (int i = 0; i < arr.length; i++) + out[i] = arr[i]; + return out; + } + + public static Boolean[] fromNative(boolean[] arr) { + Boolean[] out = new Boolean[arr.length]; + for (int i = 0; i < arr.length; i++) + out[i] = arr[i]; + return out; + } + + public static Double[] fromNative(double[] arr) { + Double[] out = new Double[arr.length]; + for (int i = 0; i < arr.length; i++) + out[i] = arr[i]; + return out; + } + /** * Gets the table with the specified key. If the table does not exist, a new *table will be created.
@@ -449,6 +477,20 @@ public class NetworkTable implements ITable, IRemote { NetworkTablesJNI.putBooleanArray(path + PATH_SEPARATOR + key, value); } + /** + * Maps the specified key to the specified value in this table. The key can + * not be null. The value can be retrieved by calling the get method with a + * key that is equal to the original key. + * + * @param key + * the key + * @param value + * the value + */ + public void putBooleanArray(String key, Boolean[] value) { + putBooleanArray(key, toNative(value)); + } + /** * Returns the key that the name maps to. * @@ -476,6 +518,24 @@ public class NetworkTable implements ITable, IRemote { return NetworkTablesJNI.getBooleanArray(path + PATH_SEPARATOR + key, defaultValue); } + /** + * Returns the key that the name maps to. If the key is null, it will return + * the default value + * + * @param key + * the key name + * @param defaultValue + * the default value if the key is null + * @return the key + */ + public Boolean[] getBooleanArray(String key, Boolean[] defaultValue) { + try { + return fromNative(getBooleanArray(key)); + } catch (TableKeyNotDefinedException e) { + return defaultValue; + } + } + /** * Maps the specified key to the specified value in this table. The key can * not be null. The value can be retrieved by calling the get method with a @@ -490,6 +550,20 @@ public class NetworkTable implements ITable, IRemote { NetworkTablesJNI.putDoubleArray(path + PATH_SEPARATOR + key, value); } + /** + * Maps the specified key to the specified value in this table. The key can + * not be null. The value can be retrieved by calling the get method with a + * key that is equal to the original key. + * + * @param key + * the key + * @param value + * the value + */ + public void putNumberArray(String key, Double[] value) { + putNumberArray(key, toNative(value)); + } + /** * Returns the key that the name maps to. * @@ -517,6 +591,24 @@ public class NetworkTable implements ITable, IRemote { return NetworkTablesJNI.getDoubleArray(path + PATH_SEPARATOR + key, defaultValue); } + /** + * Returns the key that the name maps to. If the key is null, it will return + * the default value + * + * @param key + * the key name + * @param defaultValue + * the default value if the key is null + * @return the key + */ + public Double[] getNumberArray(String key, Double[] defaultValue) { + try { + return fromNative(getNumberArray(key)); + } catch (TableKeyNotDefinedException e) { + return defaultValue; + } + } + /** * Maps the specified key to the specified value in this table. The key can * not be null. The value can be retrieved by calling the get method with a @@ -579,6 +671,10 @@ public class NetworkTable implements ITable, IRemote { NetworkTablesJNI.putBooleanArray(path + PATH_SEPARATOR + key, (boolean[])value); else if (value instanceof double[]) NetworkTablesJNI.putDoubleArray(path + PATH_SEPARATOR + key, (double[])value); + else if (value instanceof Boolean[]) + NetworkTablesJNI.putBooleanArray(path + PATH_SEPARATOR + key, toNative((Boolean[])value)); + else if (value instanceof Double[]) + NetworkTablesJNI.putDoubleArray(path + PATH_SEPARATOR + key, toNative((Double[])value)); else if (value instanceof String[]) NetworkTablesJNI.putStringArray(path + PATH_SEPARATOR + key, (String[])value); } diff --git a/java/src/edu/wpi/first/wpilibj/tables/ITable.java b/java/src/edu/wpi/first/wpilibj/tables/ITable.java index 122ddcc8e9..f3e6902910 100644 --- a/java/src/edu/wpi/first/wpilibj/tables/ITable.java +++ b/java/src/edu/wpi/first/wpilibj/tables/ITable.java @@ -118,6 +118,12 @@ public interface ITable { * @param value the value that will be assigned */ public void putBooleanArray(String key, boolean[] value); + /** + * Put a boolean array in the table + * @param key the key to be assigned to + * @param value the value that will be assigned + */ + public void putBooleanArray(String key, Boolean[] value); /** * @param key the key to look up * @return the value associated with the given key @@ -132,6 +138,13 @@ public interface ITable { * if there is no value associated with the key */ public boolean[] getBooleanArray(String key, boolean[] defaultValue); + /** + * @param key the key to look up + * @param defaultValue the value to be returned if no value is found + * @return the value associated with the given key or the given default value + * if there is no value associated with the key + */ + public Boolean[] getBooleanArray(String key, Boolean[] defaultValue); /** * Put a number array in the table @@ -139,6 +152,12 @@ public interface ITable { * @param value the value that will be assigned */ public void putNumberArray(String key, double[] value); + /** + * Put a number array in the table + * @param key the key to be assigned to + * @param value the value that will be assigned + */ + public void putNumberArray(String key, Double[] value); /** * @param key the key to look up * @return the value associated with the given key @@ -153,6 +172,13 @@ public interface ITable { * if there is no value associated with the key */ public double[] getNumberArray(String key, double[] defaultValue); + /** + * @param key the key to look up + * @param defaultValue the value to be returned if no value is found + * @return the value associated with the given key or the given default value + * if there is no value associated with the key + */ + public Double[] getNumberArray(String key, Double[] defaultValue); /** * Put a string array in the table