Java: allow both object arrays and native arrays.

This allows easier use of things like ArrayList<Double>.
This commit is contained in:
Peter Johnson
2015-08-28 20:37:25 -07:00
parent 969916851c
commit dbe4168d8d
2 changed files with 122 additions and 0 deletions

View File

@@ -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.<br>
@@ -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);
}

View File

@@ -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