diff --git a/pom.xml b/pom.xml index 8b40bdc9..4f0f2118 100644 --- a/pom.xml +++ b/pom.xml @@ -42,4 +42,17 @@ https://github.com/epics-base/pvDataJava https://github.com/epics-base/pvDataJava + + + ${project.groupId} + epics-util + 1.0.0-SNAPSHOT + + + org.hamcrest + hamcrest-all + 1.3 + test + + diff --git a/src/org/epics/pvdata/factory/AbstractPVArray.java b/src/org/epics/pvdata/factory/AbstractPVArray.java index af0ddd31..210a9c4b 100644 --- a/src/org/epics/pvdata/factory/AbstractPVArray.java +++ b/src/org/epics/pvdata/factory/AbstractPVArray.java @@ -10,6 +10,8 @@ import org.epics.pvdata.pv.ArrayData; import org.epics.pvdata.pv.PVArray; import org.epics.pvdata.pv.SerializableControl; +import org.epics.util.array.CollectionNumbers; +import org.epics.util.array.ListNumber; /** * Abstract base class for any PVArray field. @@ -116,6 +118,22 @@ protected int internalPut(int offset, int len, Object from, int fromOffset) { return len; } + public void put(int offset, ListNumber list) { + if (super.isImmutable()) + throw new IllegalStateException("field is immutable"); + + int newLength = offset + list.size(); + if (newLength > length) + { + checkLength(newLength); + setCapacity(newLength); + length = newLength; + } + + CollectionNumbers.toList(getValue()).setAll(offset, list); + super.postPut(); + } + private void checkLength(int len) { Array.ArraySizeType type = getArray().getArraySizeType(); diff --git a/src/org/epics/pvdata/factory/BasePVByteArray.java b/src/org/epics/pvdata/factory/BasePVByteArray.java index d2c62bac..a3d0f527 100644 --- a/src/org/epics/pvdata/factory/BasePVByteArray.java +++ b/src/org/epics/pvdata/factory/BasePVByteArray.java @@ -12,6 +12,8 @@ import org.epics.pvdata.pv.PVByteArray; import org.epics.pvdata.pv.ScalarArray; import org.epics.pvdata.pv.SerializableControl; +import org.epics.util.array.ArrayByte; +import org.epics.util.array.CollectionNumbers; /** @@ -71,6 +73,11 @@ protected int getFromBuffer(ByteBuffer buffer, DeserializableControl control, in public int get(int offset, int len, ByteArrayData data) { return internalGet(offset, len, data); } + + @Override + public ArrayByte get() { + return CollectionNumbers.unmodifiableListByte(value); + } /* (non-Javadoc) * @see org.epics.pvdata.pv.PVByteArray#put(int, int, byte[], int) diff --git a/src/org/epics/pvdata/factory/BasePVDoubleArray.java b/src/org/epics/pvdata/factory/BasePVDoubleArray.java index e5788e6e..b40626a3 100644 --- a/src/org/epics/pvdata/factory/BasePVDoubleArray.java +++ b/src/org/epics/pvdata/factory/BasePVDoubleArray.java @@ -12,6 +12,9 @@ import org.epics.pvdata.pv.PVDoubleArray; import org.epics.pvdata.pv.ScalarArray; import org.epics.pvdata.pv.SerializableControl; +import org.epics.util.array.ArrayDouble; +import org.epics.util.array.CollectionNumbers; +import org.epics.util.array.ListNumber; /** @@ -73,6 +76,11 @@ protected int getFromBuffer(ByteBuffer buffer, DeserializableControl control, in public int get(int offset, int len, DoubleArrayData data) { return internalGet(offset, len, data); } + + @Override + public ArrayDouble get() { + return CollectionNumbers.unmodifiableListDouble(value); + } /* (non-Javadoc) * @see org.epics.pvdata.pv.PVDoubleArray#put(int, int, double[], int) @@ -82,7 +90,6 @@ public int put(int offset, int len, double[] from, int fromOffset) { return internalPut(offset, len, from, fromOffset); } - /* (non-Javadoc) * @see org.epics.pvdata.pv.PVDoubleArray#shareData(double[]) */ diff --git a/src/org/epics/pvdata/factory/BasePVFloatArray.java b/src/org/epics/pvdata/factory/BasePVFloatArray.java index 8102c3d9..3e61b49f 100644 --- a/src/org/epics/pvdata/factory/BasePVFloatArray.java +++ b/src/org/epics/pvdata/factory/BasePVFloatArray.java @@ -12,6 +12,8 @@ import org.epics.pvdata.pv.PVFloatArray; import org.epics.pvdata.pv.ScalarArray; import org.epics.pvdata.pv.SerializableControl; +import org.epics.util.array.ArrayFloat; +import org.epics.util.array.CollectionNumbers; /** @@ -73,6 +75,11 @@ protected int getFromBuffer(ByteBuffer buffer, DeserializableControl control, in public int get(int offset, int len, FloatArrayData data) { return internalGet(offset, len, data); } + + @Override + public ArrayFloat get() { + return CollectionNumbers.unmodifiableListFloat(value); + } /* (non-Javadoc) * @see org.epics.pvdata.pv.PVFloatArray#put(int, int, float[], int) diff --git a/src/org/epics/pvdata/factory/BasePVIntArray.java b/src/org/epics/pvdata/factory/BasePVIntArray.java index 23ae3d69..af0d2cf5 100644 --- a/src/org/epics/pvdata/factory/BasePVIntArray.java +++ b/src/org/epics/pvdata/factory/BasePVIntArray.java @@ -1,111 +1,118 @@ -/* - * Copyright information and license terms for this software can be - * found in the file LICENSE that is included with the distribution - */ -package org.epics.pvdata.factory; - -import java.nio.ByteBuffer; -import java.util.Arrays; - -import org.epics.pvdata.pv.DeserializableControl; -import org.epics.pvdata.pv.IntArrayData; -import org.epics.pvdata.pv.PVIntArray; -import org.epics.pvdata.pv.ScalarArray; -import org.epics.pvdata.pv.SerializableControl; - - -/** - * Base class for implementing PVIntArray. - * @author mrk - * - */ -public class BasePVIntArray extends AbstractPVScalarArray implements PVIntArray -{ - protected int[] value; - - /** - * Constructor. - * @param array The introspection interface. - */ - public BasePVIntArray(ScalarArray array) - { - super(array); - } - - @Override - protected void allocate(int newCapacity) { - value = new int[newCapacity]; - capacity = newCapacity; - } - - @Override - protected Object getValue() - { - return value; - } - - @Override - protected void setValue(Object array) - { - value = (int[])array; - } - - @Override - protected int putToBuffer(ByteBuffer buffer, SerializableControl control, int offset, int length) - { - buffer.asIntBuffer().put(value, offset, length); - buffer.position(buffer.position() + length*4); - return length; - } - - @Override - protected int getFromBuffer(ByteBuffer buffer, DeserializableControl control, int offset, int length) - { - buffer.asIntBuffer().get(value, offset, length); - buffer.position(buffer.position() + length*4); - return length; - } - - /* (non-Javadoc) - * @see org.epics.pvdata.pv.PVIntArray#get(int, int, org.epics.pvdata.pv.IntArrayData) - */ - @Override - public int get(int offset, int len, IntArrayData data) { - return internalGet(offset, len, data); - } - - /* (non-Javadoc) - * @see org.epics.pvdata.pv.PVIntArray#put(int, int, int[], int) - */ - @Override - public int put(int offset, int len, int[] from, int fromOffset) { - return internalPut(offset, len, from, fromOffset); - } - - - /* (non-Javadoc) - * @see org.epics.pvdata.pv.PVIntArray#shareData(int[]) - */ - @Override - public void shareData(int[] from) { - internalShareData(from); - } - - @Override - protected boolean valueEquals(Object obj) - { - PVIntArray b = (PVIntArray)obj; - IntArrayData arrayData = new IntArrayData(); - // NOTE: this assumes entire array set to arrayData - b.get(0, b.getLength(), arrayData); - return Arrays.equals(arrayData.data, value); - } - - /* (non-Javadoc) - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() { - return Arrays.hashCode(value); - } -} +/* + * Copyright information and license terms for this software can be + * found in the file LICENSE that is included with the distribution + */ +package org.epics.pvdata.factory; + +import java.nio.ByteBuffer; +import java.util.Arrays; + +import org.epics.pvdata.pv.DeserializableControl; +import org.epics.pvdata.pv.IntArrayData; +import org.epics.pvdata.pv.PVIntArray; +import org.epics.pvdata.pv.ScalarArray; +import org.epics.pvdata.pv.SerializableControl; +import org.epics.util.array.ArrayInteger; +import org.epics.util.array.CollectionNumbers; + + +/** + * Base class for implementing PVIntArray. + * @author mrk + * + */ +public class BasePVIntArray extends AbstractPVScalarArray implements PVIntArray +{ + protected int[] value; + + /** + * Constructor. + * @param array The introspection interface. + */ + public BasePVIntArray(ScalarArray array) + { + super(array); + } + + @Override + protected void allocate(int newCapacity) { + value = new int[newCapacity]; + capacity = newCapacity; + } + + @Override + protected Object getValue() + { + return value; + } + + @Override + protected void setValue(Object array) + { + value = (int[])array; + } + + @Override + protected int putToBuffer(ByteBuffer buffer, SerializableControl control, int offset, int length) + { + buffer.asIntBuffer().put(value, offset, length); + buffer.position(buffer.position() + length*4); + return length; + } + + @Override + protected int getFromBuffer(ByteBuffer buffer, DeserializableControl control, int offset, int length) + { + buffer.asIntBuffer().get(value, offset, length); + buffer.position(buffer.position() + length*4); + return length; + } + + /* (non-Javadoc) + * @see org.epics.pvdata.pv.PVIntArray#get(int, int, org.epics.pvdata.pv.IntArrayData) + */ + @Override + public int get(int offset, int len, IntArrayData data) { + return internalGet(offset, len, data); + } + + @Override + public ArrayInteger get() { + return CollectionNumbers.unmodifiableListInt(value); + } + + /* (non-Javadoc) + * @see org.epics.pvdata.pv.PVIntArray#put(int, int, int[], int) + */ + @Override + public int put(int offset, int len, int[] from, int fromOffset) { + return internalPut(offset, len, from, fromOffset); + } + + + /* (non-Javadoc) + * @see org.epics.pvdata.pv.PVIntArray#shareData(int[]) + */ + @Override + public void shareData(int[] from) { + internalShareData(from); + } + + @Override + protected boolean valueEquals(Object obj) + { + PVIntArray b = (PVIntArray)obj; + IntArrayData arrayData = new IntArrayData(); + // NOTE: this assumes entire array set to arrayData + b.get(0, b.getLength(), arrayData); + return Arrays.equals(arrayData.data, value); + } + + /* (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + return Arrays.hashCode(value); + } +} diff --git a/src/org/epics/pvdata/factory/BasePVLongArray.java b/src/org/epics/pvdata/factory/BasePVLongArray.java index 039e6169..79087464 100644 --- a/src/org/epics/pvdata/factory/BasePVLongArray.java +++ b/src/org/epics/pvdata/factory/BasePVLongArray.java @@ -12,6 +12,8 @@ import org.epics.pvdata.pv.PVLongArray; import org.epics.pvdata.pv.ScalarArray; import org.epics.pvdata.pv.SerializableControl; +import org.epics.util.array.ArrayLong; +import org.epics.util.array.CollectionNumbers; /** @@ -73,6 +75,11 @@ protected int getFromBuffer(ByteBuffer buffer, DeserializableControl control, in public int get(int offset, int len, LongArrayData data) { return internalGet(offset, len, data); } + + @Override + public ArrayLong get() { + return CollectionNumbers.unmodifiableListLong(value); + } /* (non-Javadoc) * @see org.epics.pvdata.pv.PVLongArray#put(int, int, long[], int) diff --git a/src/org/epics/pvdata/factory/BasePVShortArray.java b/src/org/epics/pvdata/factory/BasePVShortArray.java index 3a4c983f..65acbe71 100644 --- a/src/org/epics/pvdata/factory/BasePVShortArray.java +++ b/src/org/epics/pvdata/factory/BasePVShortArray.java @@ -12,6 +12,8 @@ import org.epics.pvdata.pv.ScalarArray; import org.epics.pvdata.pv.SerializableControl; import org.epics.pvdata.pv.ShortArrayData; +import org.epics.util.array.ArrayShort; +import org.epics.util.array.CollectionNumbers; /** @@ -73,6 +75,11 @@ protected int getFromBuffer(ByteBuffer buffer, DeserializableControl control, in public int get(int offset, int len, ShortArrayData data) { return internalGet(offset, len, data); } + + @Override + public ArrayShort get() { + return CollectionNumbers.unmodifiableListShort(value); + } /* (non-Javadoc) * @see org.epics.pvdata.pv.PVShortArray#put(int, int, short[], int) diff --git a/src/org/epics/pvdata/factory/BasePVUByteArray.java b/src/org/epics/pvdata/factory/BasePVUByteArray.java index 81f3df76..9e83c7b4 100644 --- a/src/org/epics/pvdata/factory/BasePVUByteArray.java +++ b/src/org/epics/pvdata/factory/BasePVUByteArray.java @@ -12,6 +12,8 @@ import org.epics.pvdata.pv.PVUByteArray; import org.epics.pvdata.pv.ScalarArray; import org.epics.pvdata.pv.SerializableControl; +import org.epics.util.array.ArrayUByte; +import org.epics.util.array.CollectionNumbers; /** @@ -71,6 +73,11 @@ protected int getFromBuffer(ByteBuffer buffer, DeserializableControl control, in public int get(int offset, int len, ByteArrayData data) { return internalGet(offset, len, data); } + + @Override + public ArrayUByte get() { + return CollectionNumbers.unmodifiableListUByte(value); + } /* (non-Javadoc) * @see org.epics.pvdata.pv.PVUByteArray#put(int, int, byte[], int) diff --git a/src/org/epics/pvdata/factory/BasePVUIntArray.java b/src/org/epics/pvdata/factory/BasePVUIntArray.java index 02a90a8c..4bb1cf6c 100644 --- a/src/org/epics/pvdata/factory/BasePVUIntArray.java +++ b/src/org/epics/pvdata/factory/BasePVUIntArray.java @@ -1,111 +1,118 @@ -/* - * Copyright information and license terms for this software can be - * found in the file LICENSE that is included with the distribution - */ -package org.epics.pvdata.factory; - -import java.nio.ByteBuffer; -import java.util.Arrays; - -import org.epics.pvdata.pv.DeserializableControl; -import org.epics.pvdata.pv.IntArrayData; -import org.epics.pvdata.pv.PVUIntArray; -import org.epics.pvdata.pv.ScalarArray; -import org.epics.pvdata.pv.SerializableControl; - - -/** - * Base class for implementing PVUIntArray. - * @author mrk - * - */ -public class BasePVUIntArray extends AbstractPVScalarArray implements PVUIntArray -{ - protected int[] value; - - /** - * Constructor. - * @param array The introspection interface. - */ - public BasePVUIntArray(ScalarArray array) - { - super(array); - } - - @Override - protected void allocate(int newCapacity) { - value = new int[newCapacity]; - capacity = newCapacity; - } - - @Override - protected Object getValue() - { - return value; - } - - @Override - protected void setValue(Object array) - { - value = (int[])array; - } - - @Override - protected int putToBuffer(ByteBuffer buffer, SerializableControl control, int offset, int length) - { - buffer.asIntBuffer().put(value, offset, length); - buffer.position(buffer.position() + length*4); - return length; - } - - @Override - protected int getFromBuffer(ByteBuffer buffer, DeserializableControl control, int offset, int length) - { - buffer.asIntBuffer().get(value, offset, length); - buffer.position(buffer.position() + length*4); - return length; - } - - /* (non-Javadoc) - * @see org.epics.pvdata.pv.PVUIntArray#get(int, int, org.epics.pvdata.pv.IntArrayData) - */ - @Override - public int get(int offset, int len, IntArrayData data) { - return internalGet(offset, len, data); - } - - /* (non-Javadoc) - * @see org.epics.pvdata.pv.PVUIntArray#put(int, int, int[], int) - */ - @Override - public int put(int offset, int len, int[] from, int fromOffset) { - return internalPut(offset, len, from, fromOffset); - } - - - /* (non-Javadoc) - * @see org.epics.pvdata.pv.PVUIntArray#shareData(int[]) - */ - @Override - public void shareData(int[] from) { - internalShareData(from); - } - - @Override - protected boolean valueEquals(Object obj) - { - PVUIntArray b = (PVUIntArray)obj; - IntArrayData arrayData = new IntArrayData(); - // NOTE: this assumes entire array set to arrayData - b.get(0, b.getLength(), arrayData); - return Arrays.equals(arrayData.data, value); - } - - /* (non-Javadoc) - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() { - return Arrays.hashCode(value); - } -} +/* + * Copyright information and license terms for this software can be + * found in the file LICENSE that is included with the distribution + */ +package org.epics.pvdata.factory; + +import java.nio.ByteBuffer; +import java.util.Arrays; + +import org.epics.pvdata.pv.DeserializableControl; +import org.epics.pvdata.pv.IntArrayData; +import org.epics.pvdata.pv.PVUIntArray; +import org.epics.pvdata.pv.ScalarArray; +import org.epics.pvdata.pv.SerializableControl; +import org.epics.util.array.ArrayUInteger; +import org.epics.util.array.CollectionNumbers; + + +/** + * Base class for implementing PVUIntArray. + * @author mrk + * + */ +public class BasePVUIntArray extends AbstractPVScalarArray implements PVUIntArray +{ + protected int[] value; + + /** + * Constructor. + * @param array The introspection interface. + */ + public BasePVUIntArray(ScalarArray array) + { + super(array); + } + + @Override + protected void allocate(int newCapacity) { + value = new int[newCapacity]; + capacity = newCapacity; + } + + @Override + protected Object getValue() + { + return value; + } + + @Override + protected void setValue(Object array) + { + value = (int[])array; + } + + @Override + protected int putToBuffer(ByteBuffer buffer, SerializableControl control, int offset, int length) + { + buffer.asIntBuffer().put(value, offset, length); + buffer.position(buffer.position() + length*4); + return length; + } + + @Override + protected int getFromBuffer(ByteBuffer buffer, DeserializableControl control, int offset, int length) + { + buffer.asIntBuffer().get(value, offset, length); + buffer.position(buffer.position() + length*4); + return length; + } + + /* (non-Javadoc) + * @see org.epics.pvdata.pv.PVUIntArray#get(int, int, org.epics.pvdata.pv.IntArrayData) + */ + @Override + public int get(int offset, int len, IntArrayData data) { + return internalGet(offset, len, data); + } + + @Override + public ArrayUInteger get() { + return CollectionNumbers.unmodifiableListUInt(value); + } + + /* (non-Javadoc) + * @see org.epics.pvdata.pv.PVUIntArray#put(int, int, int[], int) + */ + @Override + public int put(int offset, int len, int[] from, int fromOffset) { + return internalPut(offset, len, from, fromOffset); + } + + + /* (non-Javadoc) + * @see org.epics.pvdata.pv.PVUIntArray#shareData(int[]) + */ + @Override + public void shareData(int[] from) { + internalShareData(from); + } + + @Override + protected boolean valueEquals(Object obj) + { + PVUIntArray b = (PVUIntArray)obj; + IntArrayData arrayData = new IntArrayData(); + // NOTE: this assumes entire array set to arrayData + b.get(0, b.getLength(), arrayData); + return Arrays.equals(arrayData.data, value); + } + + /* (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + return Arrays.hashCode(value); + } +} diff --git a/src/org/epics/pvdata/factory/BasePVULongArray.java b/src/org/epics/pvdata/factory/BasePVULongArray.java index 257b898c..82678dff 100644 --- a/src/org/epics/pvdata/factory/BasePVULongArray.java +++ b/src/org/epics/pvdata/factory/BasePVULongArray.java @@ -12,6 +12,8 @@ import org.epics.pvdata.pv.PVULongArray; import org.epics.pvdata.pv.ScalarArray; import org.epics.pvdata.pv.SerializableControl; +import org.epics.util.array.ArrayULong; +import org.epics.util.array.CollectionNumbers; /** @@ -73,6 +75,11 @@ protected int getFromBuffer(ByteBuffer buffer, DeserializableControl control, in public int get(int offset, int len, LongArrayData data) { return internalGet(offset, len, data); } + + @Override + public ArrayULong get() { + return CollectionNumbers.unmodifiableListULong(value); + } /* (non-Javadoc) * @see org.epics.pvdata.pv.PVULongArray#put(int, int, long[], int) diff --git a/src/org/epics/pvdata/factory/BasePVUShortArray.java b/src/org/epics/pvdata/factory/BasePVUShortArray.java index 755a8ef9..ce089299 100644 --- a/src/org/epics/pvdata/factory/BasePVUShortArray.java +++ b/src/org/epics/pvdata/factory/BasePVUShortArray.java @@ -12,6 +12,8 @@ import org.epics.pvdata.pv.ScalarArray; import org.epics.pvdata.pv.SerializableControl; import org.epics.pvdata.pv.ShortArrayData; +import org.epics.util.array.ArrayUShort; +import org.epics.util.array.CollectionNumbers; /** @@ -73,6 +75,11 @@ protected int getFromBuffer(ByteBuffer buffer, DeserializableControl control, in public int get(int offset, int len, ShortArrayData data) { return internalGet(offset, len, data); } + + @Override + public ArrayUShort get() { + return CollectionNumbers.unmodifiableListUShort(value); + } /* (non-Javadoc) * @see org.epics.pvdata.pv.PVUShortArray#put(int, int, short[], int) diff --git a/src/org/epics/pvdata/pv/PVByteArray.java b/src/org/epics/pvdata/pv/PVByteArray.java index 4f6e9ff5..decc6893 100644 --- a/src/org/epics/pvdata/pv/PVByteArray.java +++ b/src/org/epics/pvdata/pv/PVByteArray.java @@ -4,6 +4,8 @@ */ package org.epics.pvdata.pv; +import org.epics.util.array.ArrayByte; + /** * Get/put a byte array. * The caller must be prepared to get/put the array in chunks. @@ -12,7 +14,7 @@ * @author mrk * */ -public interface PVByteArray extends PVScalarArray { +public interface PVByteArray extends PVNumberArray { /** * Get values from a PVByteArray and put them into byte[]from. * @@ -28,6 +30,14 @@ public interface PVByteArray extends PVScalarArray { * array. */ int get(int offset, int length, ByteArrayData data); + + /** + * Returns an unmodifiable view of the data. + * + * @return an unmodifiable view of the data + */ + @Override + ArrayByte get(); /** * Put values into a PVByteArray from byte[]to. diff --git a/src/org/epics/pvdata/pv/PVDoubleArray.java b/src/org/epics/pvdata/pv/PVDoubleArray.java index d1edabf8..96b10a61 100644 --- a/src/org/epics/pvdata/pv/PVDoubleArray.java +++ b/src/org/epics/pvdata/pv/PVDoubleArray.java @@ -4,6 +4,9 @@ */ package org.epics.pvdata.pv; +import org.epics.util.array.ArrayDouble; +import org.epics.util.array.ListNumber; + /** * Get/put a double array. * The caller must be prepared to get/put the array in chunks. @@ -12,7 +15,7 @@ * @author mrk * */ -public interface PVDoubleArray extends PVScalarArray{ +public interface PVDoubleArray extends PVNumberArray{ /** * Get values from a PVDoubleArray * and put them into double[]to @@ -29,6 +32,14 @@ public interface PVDoubleArray extends PVScalarArray{ * array. */ int get(int offset, int length, DoubleArrayData data); + + /** + * Returns an unmodifiable view of the data. + * + * @return an unmodifiable view of the data + */ + @Override + ArrayDouble get(); /** * Put values into a PVDoubleArray from double[]from diff --git a/src/org/epics/pvdata/pv/PVFloatArray.java b/src/org/epics/pvdata/pv/PVFloatArray.java index 5da12c57..cb66fde2 100644 --- a/src/org/epics/pvdata/pv/PVFloatArray.java +++ b/src/org/epics/pvdata/pv/PVFloatArray.java @@ -4,6 +4,8 @@ */ package org.epics.pvdata.pv; +import org.epics.util.array.ArrayFloat; + /** * Get/put a float array. * The caller must be prepared to get/put the array in chunks. @@ -12,7 +14,7 @@ * @author mrk * */ -public interface PVFloatArray extends PVScalarArray{ +public interface PVFloatArray extends PVNumberArray{ /** * Get values from a PVFloatArray * and put them into float[]to. @@ -29,6 +31,14 @@ public interface PVFloatArray extends PVScalarArray{ * array. */ int get(int offset, int length, FloatArrayData data); + + /** + * Returns an unmodifiable view of the data. + * + * @return an unmodifiable view of the data + */ + @Override + ArrayFloat get(); /** * Put values into a PVFloatArray from float[]from. diff --git a/src/org/epics/pvdata/pv/PVIntArray.java b/src/org/epics/pvdata/pv/PVIntArray.java index d5a5ac5b..455a52f8 100644 --- a/src/org/epics/pvdata/pv/PVIntArray.java +++ b/src/org/epics/pvdata/pv/PVIntArray.java @@ -1,54 +1,65 @@ -/* - * Copyright information and license terms for this software can be - * found in the file LICENSE that is included with the distribution - */ -package org.epics.pvdata.pv; - -/** - * Get/put a int array. - * The caller must be prepared to get/put the array in chunks. - * The return argument is always the number of elements that were transfered. - * It may be less than the number requested. - * @author mrk - * - */ -public interface PVIntArray extends PVScalarArray{ - /** - * Get values from a PVIntArray and put them into int[]to. - * - * @param offset the offset to the first element to get - * @param length the maximum number of elements to transfer - * @param data the class containing the data and an offset into the data. - * Get sets these values. The caller must do the actual data transfer. - * @return the number of elements that can be transfered. - * This is always less than or equal to length. - * If the value is less then length then get should be called again. - * If the return value is greater than 0 then data.data is - * a reference to the array and data.offset is the offset into the - * array. - */ - int get(int offset, int length, IntArrayData data); - - /** - * Put values into a PVIntArray from int[]from. - * - * @param offset the offset to the first element to put - * @param length the maximum number of elements to transfer - * @param from the array from which to get the data - * @param fromOffset the offset into from - * @return the number of elements transfered. - * This is always less than or equal to length. - * If the value is less than the length then put should be called again. - * @throws IllegalStateException if the field is not mutable - */ - int put(int offset, int length, int[] from, int fromOffset); - - /** - * Share the data from caller. - * The capacity and length are taken from the array and this array is made immutable. - * This should only be used to share immutable data. - * - * @param from the data to share - */ - void shareData(int[] from); -} +/* + * Copyright information and license terms for this software can be + * found in the file LICENSE that is included with the distribution + */ +package org.epics.pvdata.pv; + +import org.epics.util.array.ArrayInteger; +import org.epics.util.array.ListNumber; + +/** + * Get/put a int array. + * The caller must be prepared to get/put the array in chunks. + * The return argument is always the number of elements that were transfered. + * It may be less than the number requested. + * @author mrk + * + */ +public interface PVIntArray extends PVNumberArray{ + /** + * Get values from a PVIntArray and put them into int[]to. + * + * @param offset the offset to the first element to get + * @param length the maximum number of elements to transfer + * @param data the class containing the data and an offset into the data. + * Get sets these values. The caller must do the actual data transfer. + * @return the number of elements that can be transfered. + * This is always less than or equal to length. + * If the value is less then length then get should be called again. + * If the return value is greater than 0 then data.data is + * a reference to the array and data.offset is the offset into the + * array. + */ + int get(int offset, int length, IntArrayData data); + + /** + * Returns an unmodifiable view of the data. + * + * @return an unmodifiable view of the data + */ + @Override + ArrayInteger get(); + + /** + * Put values into a PVIntArray from int[]from. + * + * @param offset the offset to the first element to put + * @param length the maximum number of elements to transfer + * @param from the array from which to get the data + * @param fromOffset the offset into from + * @return the number of elements transfered. + * This is always less than or equal to length. + * If the value is less than the length then put should be called again. + * @throws IllegalStateException if the field is not mutable + */ + int put(int offset, int length, int[] from, int fromOffset); + + /** + * Share the data from caller. + * The capacity and length are taken from the array and this array is made immutable. + * This should only be used to share immutable data. + * + * @param from the data to share + */ + void shareData(int[] from); +} diff --git a/src/org/epics/pvdata/pv/PVLongArray.java b/src/org/epics/pvdata/pv/PVLongArray.java index b35e3099..40f1803a 100644 --- a/src/org/epics/pvdata/pv/PVLongArray.java +++ b/src/org/epics/pvdata/pv/PVLongArray.java @@ -4,6 +4,8 @@ */ package org.epics.pvdata.pv; +import org.epics.util.array.ArrayLong; + /** * Get/put a long array. * The caller must be prepared to get/put the array in chunks. @@ -12,7 +14,7 @@ * @author mrk * */ -public interface PVLongArray extends PVScalarArray{ +public interface PVLongArray extends PVNumberArray{ /** * Get values from a PVLongArray and put them into long[]to. * @@ -28,6 +30,14 @@ public interface PVLongArray extends PVScalarArray{ * array. */ int get(int offset, int length, LongArrayData data); + + /** + * Returns an unmodifiable view of the data. + * + * @return an unmodifiable view of the data + */ + @Override + ArrayLong get(); /** * Put values into a PVLongArray from long[]from. diff --git a/src/org/epics/pvdata/pv/PVNumberArray.java b/src/org/epics/pvdata/pv/PVNumberArray.java new file mode 100644 index 00000000..723d8372 --- /dev/null +++ b/src/org/epics/pvdata/pv/PVNumberArray.java @@ -0,0 +1,32 @@ +/* + * Copyright information and license terms for this software can be + * found in the file LICENSE that is included with the distribution + */ +package org.epics.pvdata.pv; + +import org.epics.util.array.ListNumber; + +/** + * Get/put a numeric array array. + * The caller must be prepared to get/put the array in chunks. + * The return argument is always the number of elements that were transfered. + * It may be less than the number requested. + * + */ +public interface PVNumberArray extends PVScalarArray { + + /** + * Returns an unmodifiable view of the data. + * + * @return an unmodifiable view of the data + */ + ListNumber get(); + + /** + * Puts the new value contained in the list starting from the offset. + * + * @param offset the first element to be changed + * @param list the values to be copied + */ + void put(int offset, ListNumber list); +} diff --git a/src/org/epics/pvdata/pv/PVShortArray.java b/src/org/epics/pvdata/pv/PVShortArray.java index 52f765f4..2753d581 100644 --- a/src/org/epics/pvdata/pv/PVShortArray.java +++ b/src/org/epics/pvdata/pv/PVShortArray.java @@ -4,6 +4,8 @@ */ package org.epics.pvdata.pv; +import org.epics.util.array.ArrayShort; + /** * Get/put a short array. * The caller must be prepared to get/put the array in chunks. @@ -12,7 +14,7 @@ * @author mrk * */ -public interface PVShortArray extends PVScalarArray{ +public interface PVShortArray extends PVNumberArray{ /** * Get values from a PVShortArray and put them into short[]to. * @@ -28,6 +30,14 @@ public interface PVShortArray extends PVScalarArray{ * array. */ int get(int offset, int length, ShortArrayData data); + + /** + * Returns an unmodifiable view of the data. + * + * @return an unmodifiable view of the data + */ + @Override + ArrayShort get(); /** * Put values into a PVShortArray from short[]from. diff --git a/src/org/epics/pvdata/pv/PVUByteArray.java b/src/org/epics/pvdata/pv/PVUByteArray.java index 459b3352..9efd5b79 100644 --- a/src/org/epics/pvdata/pv/PVUByteArray.java +++ b/src/org/epics/pvdata/pv/PVUByteArray.java @@ -4,6 +4,8 @@ */ package org.epics.pvdata.pv; +import org.epics.util.array.ArrayUByte; + /** * Get/put a byte array. * Since Java does not support unsigned the actual arguments are signed. @@ -14,7 +16,7 @@ * @author mrk * */ -public interface PVUByteArray extends PVScalarArray{ +public interface PVUByteArray extends PVNumberArray{ /** * Get values from a PVByteArray and put them into byte[]from. * @@ -30,6 +32,14 @@ public interface PVUByteArray extends PVScalarArray{ * array. */ int get(int offset, int length, ByteArrayData data); + + /** + * Returns an unmodifiable view of the data. + * + * @return an unmodifiable view of the data + */ + @Override + ArrayUByte get(); /** * Put values into a PVByteArray from byte[]to. diff --git a/src/org/epics/pvdata/pv/PVUIntArray.java b/src/org/epics/pvdata/pv/PVUIntArray.java index fb42d848..3b8c0b85 100644 --- a/src/org/epics/pvdata/pv/PVUIntArray.java +++ b/src/org/epics/pvdata/pv/PVUIntArray.java @@ -1,56 +1,66 @@ -/* - * Copyright information and license terms for this software can be - * found in the file LICENSE that is included with the distribution - */ -package org.epics.pvdata.pv; - -/** - * Get/put a int array. - * Since Java does not support unsigned the actual arguments are signed. - * Code that calls methods of the class is responsible for integer overflow problems. - * The caller must be prepared to get/put the array in chunks. - * The return argument is always the number of elements that were transfered. - * It may be less than the number requested. - * @author mrk - * - */ -public interface PVUIntArray extends PVScalarArray{ - /** - * Get values from a PVIntArray and put them into int[]to. - * - * @param offset the offset to the first element to get - * @param length the maximum number of elements to transfer - * @param data the class containing the data and an offset into the data. - * Get sets these values. The caller must do the actual data transfer. - * @return the number of elements that can be transfered. - * This is always less than or equal to length. - * If the value is less then length then get should be called again. - * If the return value is greater than 0 then data.data is - * a reference to the array and data.offset is the offset into the - * array. - */ - int get(int offset, int length, IntArrayData data); - - /** - * Put values into a PVIntArray from int[]from. - * - * @param offset the offset to the first element to put - * @param length the maximum number of elements to transfer - * @param from the array from which to get the data - * @param fromOffset The offset into from - * @return the number of elements transfered. - * This is always less than or equal to length. - * If the value is less than the length then put should be called again. - * @throws IllegalStateException if the field is not mutable. - */ - int put(int offset,int length, int[] from, int fromOffset); - - /** - * Share the data from caller. - * The capacity and length are taken from the array and this array is made immutable. - * This should only be used to share immutable data. - * - * @param from the data to share - */ - void shareData(int[] from); -} +/* + * Copyright information and license terms for this software can be + * found in the file LICENSE that is included with the distribution + */ +package org.epics.pvdata.pv; + +import org.epics.util.array.ArrayUInteger; + +/** + * Get/put a int array. + * Since Java does not support unsigned the actual arguments are signed. + * Code that calls methods of the class is responsible for integer overflow problems. + * The caller must be prepared to get/put the array in chunks. + * The return argument is always the number of elements that were transfered. + * It may be less than the number requested. + * @author mrk + * + */ +public interface PVUIntArray extends PVNumberArray{ + /** + * Get values from a PVIntArray and put them into int[]to. + * + * @param offset the offset to the first element to get + * @param length the maximum number of elements to transfer + * @param data the class containing the data and an offset into the data. + * Get sets these values. The caller must do the actual data transfer. + * @return the number of elements that can be transfered. + * This is always less than or equal to length. + * If the value is less then length then get should be called again. + * If the return value is greater than 0 then data.data is + * a reference to the array and data.offset is the offset into the + * array. + */ + int get(int offset, int length, IntArrayData data); + + /** + * Returns an unmodifiable view of the data. + * + * @return an unmodifiable view of the data + */ + @Override + ArrayUInteger get(); + + /** + * Put values into a PVIntArray from int[]from. + * + * @param offset the offset to the first element to put + * @param length the maximum number of elements to transfer + * @param from the array from which to get the data + * @param fromOffset The offset into from + * @return the number of elements transfered. + * This is always less than or equal to length. + * If the value is less than the length then put should be called again. + * @throws IllegalStateException if the field is not mutable. + */ + int put(int offset,int length, int[] from, int fromOffset); + + /** + * Share the data from caller. + * The capacity and length are taken from the array and this array is made immutable. + * This should only be used to share immutable data. + * + * @param from the data to share + */ + void shareData(int[] from); +} diff --git a/src/org/epics/pvdata/pv/PVULongArray.java b/src/org/epics/pvdata/pv/PVULongArray.java index 3a1b09b3..8ee57a49 100644 --- a/src/org/epics/pvdata/pv/PVULongArray.java +++ b/src/org/epics/pvdata/pv/PVULongArray.java @@ -4,6 +4,8 @@ */ package org.epics.pvdata.pv; +import org.epics.util.array.ArrayULong; + /** * Get/put a long array. * Since Java does not support unsigned the actual arguments are signed. @@ -14,7 +16,7 @@ * @author mrk * */ -public interface PVULongArray extends PVScalarArray{ +public interface PVULongArray extends PVNumberArray{ /** * Get values from a PVLongArray and put them into long[]to. * @@ -30,6 +32,14 @@ public interface PVULongArray extends PVScalarArray{ * array. */ int get(int offset, int length, LongArrayData data); + + /** + * Returns an unmodifiable view of the data. + * + * @return an unmodifiable view of the data + */ + @Override + ArrayULong get(); /** * Put values into a PVLongArray from long[]from. diff --git a/src/org/epics/pvdata/pv/PVUShortArray.java b/src/org/epics/pvdata/pv/PVUShortArray.java index f1fbddbb..59bf2323 100644 --- a/src/org/epics/pvdata/pv/PVUShortArray.java +++ b/src/org/epics/pvdata/pv/PVUShortArray.java @@ -4,6 +4,8 @@ */ package org.epics.pvdata.pv; +import org.epics.util.array.ArrayUShort; + /** * Get/put a short array. * Since Java does not support unsigned the actual arguments are signed. @@ -14,7 +16,7 @@ * @author mrk * */ -public interface PVUShortArray extends PVScalarArray{ +public interface PVUShortArray extends PVNumberArray{ /** * Get values from a PVShortArray and put them into short[]to. * @@ -30,6 +32,14 @@ public interface PVUShortArray extends PVScalarArray{ * array. */ int get(int offset, int length, ShortArrayData data); + + /** + * Returns an unmodifiable view of the data. + * + * @return an unmodifiable view of the data + */ + @Override + ArrayUShort get(); /** * Put values into a PVShortArray from short[]from. diff --git a/test/org/epics/pvdata/NumericArrayTest.java b/test/org/epics/pvdata/NumericArrayTest.java new file mode 100644 index 00000000..dd503648 --- /dev/null +++ b/test/org/epics/pvdata/NumericArrayTest.java @@ -0,0 +1,150 @@ +/* + * Copyright information and license terms for this software can be + * found in the file LICENSE that is included with the distribution + */ +package org.epics.pvdata; + + +import junit.framework.TestCase; + +import org.epics.pvdata.factory.PVDataFactory; +import org.epics.pvdata.pv.PVByteArray; +import org.epics.pvdata.pv.PVDataCreate; +import org.epics.pvdata.pv.PVDoubleArray; +import org.epics.pvdata.pv.PVFloatArray; +import org.epics.pvdata.pv.PVIntArray; +import org.epics.pvdata.pv.PVLongArray; +import org.epics.pvdata.pv.PVNumberArray; +import org.epics.pvdata.pv.PVShortArray; +import org.epics.pvdata.pv.PVUByteArray; +import org.epics.pvdata.pv.PVUIntArray; +import org.epics.pvdata.pv.PVULongArray; +import org.epics.pvdata.pv.PVUShortArray; +import org.epics.pvdata.pv.ScalarType; +import org.epics.util.array.ArrayByte; +import org.epics.util.array.ArrayDouble; +import org.epics.util.array.ArrayFloat; +import org.epics.util.array.ArrayInteger; +import org.epics.util.array.ArrayLong; +import org.epics.util.array.ArrayShort; +import org.epics.util.array.ArrayUByte; +import org.epics.util.array.ArrayUInteger; +import org.epics.util.array.ArrayULong; +import org.epics.util.array.ArrayUShort; +import org.epics.util.array.CollectionNumbers; +import org.epics.util.array.ListNumber; +import static org.junit.Assert.*; +import static org.hamcrest.Matchers.*; + +public class NumericArrayTest extends TestCase { + + public void testPutDoubleArray1() { + PVDataCreate factory = PVDataFactory.getPVDataCreate(); + PVDoubleArray pvArray = (PVDoubleArray) factory.createPVScalarArray(ScalarType.pvDouble); + assertThat(pvArray.get(), instanceOf(ArrayDouble.class)); + pvArray.put(0, CollectionNumbers.toListDouble(0,1,2,3,4,5,6,7,8,9)); + assertThat(pvArray.get(), equalTo(CollectionNumbers.toListDouble(0,1,2,3,4,5,6,7,8,9))); + pvArray.put(2, CollectionNumbers.toListFloat(3,2)); + assertThat(pvArray.get(), equalTo(CollectionNumbers.toListDouble(0,1,3,2,4,5,6,7,8,9))); + } + + public void testPutFloatArray1() { + PVDataCreate factory = PVDataFactory.getPVDataCreate(); + PVFloatArray pvArray = (PVFloatArray) factory.createPVScalarArray(ScalarType.pvFloat); + assertThat(pvArray.get(), instanceOf(ArrayFloat.class)); + pvArray.put(0, CollectionNumbers.toListFloat(0,1,2,3,4,5,6,7,8,9)); + assertThat(pvArray.get(), equalTo(CollectionNumbers.toListFloat(0,1,2,3,4,5,6,7,8,9))); + pvArray.put(2, CollectionNumbers.toListDouble(3,2)); + assertThat(pvArray.get(), equalTo(CollectionNumbers.toListFloat(0,1,3,2,4,5,6,7,8,9))); + } + + public void testPutLongArray1() { + PVDataCreate factory = PVDataFactory.getPVDataCreate(); + PVLongArray pvArray = (PVLongArray) factory.createPVScalarArray(ScalarType.pvLong); + assertThat(pvArray.get(), instanceOf(ArrayLong.class)); + pvArray.put(0, CollectionNumbers.toListLong(0,1,2,3,4,5,6,7,8,9)); + assertThat(pvArray.get(), equalTo(CollectionNumbers.toListLong(0,1,2,3,4,5,6,7,8,9))); + pvArray.put(2, CollectionNumbers.toListInt(3,2)); + assertThat(pvArray.get(), equalTo(CollectionNumbers.toListLong(0,1,3,2,4,5,6,7,8,9))); + } + + public void testPutULongArray1() { + PVDataCreate factory = PVDataFactory.getPVDataCreate(); + PVULongArray pvArray = (PVULongArray) factory.createPVScalarArray(ScalarType.pvULong); + assertThat(pvArray.get(), instanceOf(ArrayULong.class)); + pvArray.put(0, CollectionNumbers.toListULong(0,1,2,3,4,5,6,7,8,9)); + assertThat(pvArray.get(), equalTo(CollectionNumbers.toListULong(0,1,2,3,4,5,6,7,8,9))); + pvArray.put(2, CollectionNumbers.toListInt(3,2)); + assertThat(pvArray.get(), equalTo(CollectionNumbers.toListULong(0,1,3,2,4,5,6,7,8,9))); + } + + public void testPutIntArray1() { + PVDataCreate factory = PVDataFactory.getPVDataCreate(); + PVIntArray pvArray = (PVIntArray) factory.createPVScalarArray(ScalarType.pvInt); + assertThat(pvArray.get(), instanceOf(ArrayInteger.class)); + pvArray.put(0, CollectionNumbers.toListInt(0,1,2,3,4,5,6,7,8,9)); + assertThat(pvArray.get(), equalTo(CollectionNumbers.toListInt(0,1,2,3,4,5,6,7,8,9))); + pvArray.put(2, CollectionNumbers.toListLong(3,2)); + assertThat(pvArray.get(), equalTo(CollectionNumbers.toListInt(0,1,3,2,4,5,6,7,8,9))); + } + + public void testPutUIntArray1() { + PVDataCreate factory = PVDataFactory.getPVDataCreate(); + PVUIntArray pvArray = (PVUIntArray) factory.createPVScalarArray(ScalarType.pvUInt); + assertThat(pvArray.get(), instanceOf(ArrayUInteger.class)); + pvArray.put(0, CollectionNumbers.toListUInt(0,1,2,3,4,5,6,7,8,9)); + assertThat(pvArray.get(), equalTo(CollectionNumbers.toListUInt(0,1,2,3,4,5,6,7,8,9))); + pvArray.put(2, CollectionNumbers.toListLong(3,2)); + assertThat(pvArray.get(), equalTo(CollectionNumbers.toListUInt(0,1,3,2,4,5,6,7,8,9))); + } + + public void testPutShortArray1() { + PVDataCreate factory = PVDataFactory.getPVDataCreate(); + PVShortArray pvArray = (PVShortArray) factory.createPVScalarArray(ScalarType.pvShort); + assertThat(pvArray.get(), instanceOf(ArrayShort.class)); + pvArray.put(0, CollectionNumbers.toListShort(new short[] {0,1,2,3,4,5,6,7,8,9})); + assertThat(pvArray.get(), equalTo(CollectionNumbers.toListShort(new short[] {0,1,2,3,4,5,6,7,8,9}))); + pvArray.put(2, CollectionNumbers.toListInt(3,2)); + assertThat(pvArray.get(), equalTo(CollectionNumbers.toListShort(new short[] {0,1,3,2,4,5,6,7,8,9}))); + } + + public void testPutUShortArray1() { + PVDataCreate factory = PVDataFactory.getPVDataCreate(); + PVUShortArray pvArray = (PVUShortArray) factory.createPVScalarArray(ScalarType.pvUShort); + assertThat(pvArray.get(), instanceOf(ArrayUShort.class)); + pvArray.put(0, CollectionNumbers.toListUShort(new short[] {0,1,2,3,4,5,6,7,8,9})); + assertThat(pvArray.get(), equalTo(CollectionNumbers.toListUShort(new short[] {0,1,2,3,4,5,6,7,8,9}))); + pvArray.put(2, CollectionNumbers.toListInt(3,2)); + assertThat(pvArray.get(), equalTo(CollectionNumbers.toListUShort(new short[] {0,1,3,2,4,5,6,7,8,9}))); + } + + public void testPutByteArray1() { + PVDataCreate factory = PVDataFactory.getPVDataCreate(); + PVByteArray pvArray = (PVByteArray) factory.createPVScalarArray(ScalarType.pvByte); + assertThat(pvArray.get(), instanceOf(ArrayByte.class)); + pvArray.put(0, CollectionNumbers.toListByte(new byte[] {0,1,2,3,4,5,6,7,8,9})); + assertThat(pvArray.get(), equalTo(CollectionNumbers.toListByte(new byte[] {0,1,2,3,4,5,6,7,8,9}))); + pvArray.put(2, CollectionNumbers.toListInt(3,2)); + assertThat(pvArray.get(), equalTo(CollectionNumbers.toListByte(new byte[] {0,1,3,2,4,5,6,7,8,9}))); + } + + public void testPutUByteArray1() { + PVDataCreate factory = PVDataFactory.getPVDataCreate(); + PVUByteArray pvArray = (PVUByteArray) factory.createPVScalarArray(ScalarType.pvUByte); + assertThat(pvArray.get(), instanceOf(ArrayUByte.class)); + pvArray.put(0, CollectionNumbers.toListUByte(new byte[] {0,1,2,3,4,5,6,7,8,9})); + assertThat(pvArray.get(), equalTo(CollectionNumbers.toListUByte(new byte[] {0,1,2,3,4,5,6,7,8,9}))); + pvArray.put(2, CollectionNumbers.toListInt(3,2)); + assertThat(pvArray.get(), equalTo(CollectionNumbers.toListUByte(new byte[] {0,1,3,2,4,5,6,7,8,9}))); + } + + public void testPutNumericArray1() { + PVDataCreate factory = PVDataFactory.getPVDataCreate(); + PVNumberArray pvArray = (PVNumberArray) factory.createPVScalarArray(ScalarType.pvInt); + assertThat(pvArray.get(), instanceOf(ArrayInteger.class)); + pvArray.put(0, CollectionNumbers.toListInt(0,1,2,3,4,5,6,7,8,9)); + assertThat(pvArray.get(), equalTo((ListNumber) CollectionNumbers.toListInt(0,1,2,3,4,5,6,7,8,9))); + pvArray.put(2, CollectionNumbers.toListInt(3,2)); + assertThat(pvArray.get(), equalTo((ListNumber) CollectionNumbers.toListInt(0,1,3,2,4,5,6,7,8,9))); + } +}