mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
Java: Add backwards compat shims for array types.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package edu.wpi.first.wpilibj.networktables;
|
||||
|
||||
import edu.wpi.first.wpilibj.tables.*;
|
||||
import edu.wpi.first.wpilibj.networktables2.type.*;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
@@ -677,6 +678,24 @@ public class NetworkTable implements ITable, IRemote {
|
||||
NetworkTablesJNI.putDoubleArray(path + PATH_SEPARATOR + key, toNative((Double[])value));
|
||||
else if (value instanceof String[])
|
||||
NetworkTablesJNI.putStringArray(path + PATH_SEPARATOR + key, (String[])value);
|
||||
else if (value instanceof BooleanArray)
|
||||
NetworkTablesJNI.putBooleanArray(path + PATH_SEPARATOR + key, toNative((Boolean[])((ArrayData)value).getDataArray()));
|
||||
else if (value instanceof NumberArray)
|
||||
NetworkTablesJNI.putDoubleArray(path + PATH_SEPARATOR + key, toNative((Double[])((ArrayData)value).getDataArray()));
|
||||
else if (value instanceof StringArray)
|
||||
NetworkTablesJNI.putStringArray(path + PATH_SEPARATOR + key, (String[])((ArrayData)value).getDataArray());
|
||||
}
|
||||
|
||||
public void retrieveValue(String key, Object externalData) throws TableKeyNotDefinedException {
|
||||
Object value = getValue(key);
|
||||
if (value instanceof boolean[] && externalData instanceof BooleanArray)
|
||||
((ArrayData)externalData).setDataArray(fromNative((boolean[])value));
|
||||
else if (value instanceof double[] && externalData instanceof NumberArray)
|
||||
((ArrayData)externalData).setDataArray(fromNative((double[])value));
|
||||
else if (value instanceof String[] && externalData instanceof StringArray)
|
||||
((ArrayData)externalData).setDataArray((String[])value);
|
||||
else
|
||||
throw new TableKeyNotDefinedException(key);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package edu.wpi.first.wpilibj.networktables2.type;
|
||||
|
||||
/**
|
||||
* @deprecated Use ArrayList instead.
|
||||
*/
|
||||
public class ArrayData {
|
||||
private Object[] data = new Object[0];
|
||||
|
||||
protected Object getAsObject(int index) {
|
||||
return data[index];
|
||||
}
|
||||
protected void _set(int index, Object value) {
|
||||
data[index] = value;
|
||||
}
|
||||
protected void _add(Object value) {
|
||||
setSize(size() + 1);
|
||||
data[size() - 1] = value;
|
||||
}
|
||||
public void remove(int index) {
|
||||
if (index < 0 || index >= size())
|
||||
throw new IndexOutOfBoundsException();
|
||||
if (index < size() - 1)
|
||||
System.arraycopy(data, index + 1, data, index, size() - index - 1);
|
||||
setSize(size() - 1);
|
||||
}
|
||||
public void setSize(int size) {
|
||||
if (size == data.length)
|
||||
return;
|
||||
Object[] newArray = new Object[size];
|
||||
if (size < data.length)
|
||||
System.arraycopy(data, 0, newArray, 0, size);
|
||||
else {
|
||||
System.arraycopy(data, 0, newArray, 0, data.length);
|
||||
for (int i = data.length; i < newArray.length; ++i)
|
||||
newArray[i] = null;
|
||||
}
|
||||
data = newArray;
|
||||
}
|
||||
public int size() {
|
||||
return data.length;
|
||||
}
|
||||
|
||||
public Object[] getDataArray() {
|
||||
return data;
|
||||
}
|
||||
public void setDataArray(Object[] value) {
|
||||
data = value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package edu.wpi.first.wpilibj.networktables2.type;
|
||||
|
||||
/**
|
||||
* @deprecated Use ArrayList<Boolean> instead.
|
||||
*/
|
||||
public class BooleanArray extends ArrayData {
|
||||
public boolean get(int index) {
|
||||
return ((Boolean)getAsObject(index)).booleanValue();
|
||||
}
|
||||
public void set(int index, boolean value) {
|
||||
_set(index, value?Boolean.TRUE:Boolean.FALSE);
|
||||
}
|
||||
public void add(boolean value) {
|
||||
_add(value?Boolean.TRUE:Boolean.FALSE);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package edu.wpi.first.wpilibj.networktables2.type;
|
||||
|
||||
/**
|
||||
* @deprecated Use ArrayList<Double> instead.
|
||||
*/
|
||||
public class NumberArray extends ArrayData {
|
||||
public double get(int index) {
|
||||
return ((Double)getAsObject(index)).doubleValue();
|
||||
}
|
||||
public void set(int index, double value) {
|
||||
_set(index, new Double(value));
|
||||
}
|
||||
public void add(double value) {
|
||||
_add(new Double(value));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package edu.wpi.first.wpilibj.networktables2.type;
|
||||
|
||||
/**
|
||||
* @deprecated Use ArrayList<String> instead.
|
||||
*/
|
||||
public class StringArray extends ArrayData {
|
||||
public String get(int index) {
|
||||
return ((String)getAsObject(index));
|
||||
}
|
||||
public void set(int index, String value) {
|
||||
_set(index, value);
|
||||
}
|
||||
public void add(String value) {
|
||||
_add(value);
|
||||
}
|
||||
}
|
||||
@@ -49,6 +49,16 @@ public interface ITable {
|
||||
public void putValue(String key, Object value)
|
||||
throws IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Retrieve an array data type from the table.
|
||||
* @param key the key to be assigned to
|
||||
* @param externalValue the array data type to retreive into
|
||||
* @throws TableKeyNotDefinedException if there is no value associated with
|
||||
* the given key
|
||||
* @deprecated Use get*Array functions instead.
|
||||
*/
|
||||
public void retrieveValue(String key, Object externalValue) throws TableKeyNotDefinedException;
|
||||
|
||||
/**
|
||||
* Put a number in the table
|
||||
* @param key the key to be assigned to
|
||||
|
||||
Reference in New Issue
Block a user