Class ArrayAccess
- java.lang.Object
-
- org.apfloat.spi.ArrayAccess
-
- All Implemented Interfaces:
Serializable
,AutoCloseable
- Direct Known Subclasses:
DoubleMemoryArrayAccess
,FloatMemoryArrayAccess
,IntMemoryArrayAccess
,LongMemoryArrayAccess
public abstract class ArrayAccess extends Object implements Serializable, AutoCloseable
TheArrayAccess
class simulates aC
language pointer. With oneArrayAccess
object you can point to a location within an array. You can easily add or subtract a value to this "pointer", thus essentially emulatingC
pointer arithmetic. AnArrayAccess
provides an array, the starting index within that array and the length of accessible data within the array, all in one convenient package.Just like pointers in the
C
language,ArrayAccess
objects are inherently unsafe and must be used cautiously. It is the responsibility of the user of anArrayAccess
object to make sure that he doesn't access the provided array outside the allowed range. TheArrayAccess
object itself does nothing to enforce this, except of course the mandatory bounds check of Java, which can throw anArrayIndexOutOfBoundsException
.- Version:
- 1.9.0
- Author:
- Mikko Tommila
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
ArrayAccess(int offset, int length)
Create an array access.
-
Method Summary
Modifier and Type Method Description abstract void
close()
Close this array access and commit any changes to the underlying data storage if applicable.abstract Object
getData()
Returns the array of this array access.double[]
getDoubleData()
Returns the array of this array access as adouble[]
.float[]
getFloatData()
Returns the array of this array access as afloat[]
.int[]
getIntData()
Returns the array of this array access as anint[]
.int
getLength()
Returns the length of the access segment within the backing array.long[]
getLongData()
Returns the array of this array access as along[]
.int
getOffset()
Returns the offset of the access segment within the backing array.abstract ArrayAccess
subsequence(int offset, int length)
Create a sub-sequence view of this array access.
-
-
-
Method Detail
-
subsequence
public abstract ArrayAccess subsequence(int offset, int length)
Create a sub-sequence view of this array access.Note that the changes done to the sub-sequence array are not necessarily committed to the underlying data storage when the sub-sequence is closed (with
close()
), but only when the "base"ArrayAccess
is closed.- Parameters:
offset
- The sub-sequence starting offset within this ArrayAccess.length
- The sub-sequence length.- Returns:
- The sub-sequence array access.
-
getData
public abstract Object getData() throws ApfloatRuntimeException
Returns the array of this array access. This is an array of a primitive type, depending on the implementation class.- Returns:
- The backing array of this array access.
- Throws:
ApfloatRuntimeException
-
getIntData
public int[] getIntData() throws UnsupportedOperationException, ApfloatRuntimeException
Returns the array of this array access as anint[]
.- Returns:
- The backing array of this array access.
- Throws:
UnsupportedOperationException
- In case the backing array can't be presented asint[]
.ApfloatRuntimeException
-
getLongData
public long[] getLongData() throws UnsupportedOperationException, ApfloatRuntimeException
Returns the array of this array access as along[]
.- Returns:
- The backing array of this array access.
- Throws:
UnsupportedOperationException
- In case the backing array can't be presented aslong[]
.ApfloatRuntimeException
-
getFloatData
public float[] getFloatData() throws UnsupportedOperationException, ApfloatRuntimeException
Returns the array of this array access as afloat[]
.- Returns:
- The backing array of this array access.
- Throws:
UnsupportedOperationException
- In case the backing array can't be presented asfloat[]
.ApfloatRuntimeException
-
getDoubleData
public double[] getDoubleData() throws UnsupportedOperationException, ApfloatRuntimeException
Returns the array of this array access as adouble[]
.- Returns:
- The backing array of this array access.
- Throws:
UnsupportedOperationException
- In case the backing array can't be presented asdouble[]
.ApfloatRuntimeException
-
getOffset
public int getOffset()
Returns the offset of the access segment within the backing array.- Returns:
- The starting index within the backing array that is allowed to be accessed.
-
getLength
public int getLength()
Returns the length of the access segment within the backing array.- Returns:
- The number of elements within the backing array that is allowed to be accessed.
-
close
public abstract void close() throws ApfloatRuntimeException
Close this array access and commit any changes to the underlying data storage if applicable.If the
ArrayAccess
was obtained in write mode, the changes are saved to the data storage. Note that even if theArrayAccess
was obtained for reading only, any changes made to the array data may still be committed to the data storage.Note that changes done to a sub-sequence array are not necessarily committed to the underlying data storage when the sub-sequence is closed, but only when the "base"
ArrayAccess
is closed.- Specified by:
close
in interfaceAutoCloseable
- Throws:
ApfloatRuntimeException
-
-