Class ArrayAccess
- java.lang.Object
-
- org.apfloat.spi.ArrayAccess
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.AutoCloseable
- Direct Known Subclasses:
DoubleMemoryArrayAccess
,FloatMemoryArrayAccess
,IntMemoryArrayAccess
,LongMemoryArrayAccess
public abstract class ArrayAccess extends java.lang.Object implements java.io.Serializable, java.lang.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
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private int
length
private int
offset
private static long
serialVersionUID
-
Constructor Summary
Constructors Modifier Constructor Description protected
ArrayAccess(int offset, int length)
Create an array access.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract void
close()
Close this array access and commit any changes to the underlying data storage if applicable.abstract java.lang.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.
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
offset
private int offset
-
length
private int length
-
-
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 java.lang.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 java.lang.UnsupportedOperationException, ApfloatRuntimeException
Returns the array of this array access as anint[]
.- Returns:
- The backing array of this array access.
- Throws:
java.lang.UnsupportedOperationException
- In case the backing array can't be presented asint[]
.ApfloatRuntimeException
-
getLongData
public long[] getLongData() throws java.lang.UnsupportedOperationException, ApfloatRuntimeException
Returns the array of this array access as along[]
.- Returns:
- The backing array of this array access.
- Throws:
java.lang.UnsupportedOperationException
- In case the backing array can't be presented aslong[]
.ApfloatRuntimeException
-
getFloatData
public float[] getFloatData() throws java.lang.UnsupportedOperationException, ApfloatRuntimeException
Returns the array of this array access as afloat[]
.- Returns:
- The backing array of this array access.
- Throws:
java.lang.UnsupportedOperationException
- In case the backing array can't be presented asfloat[]
.ApfloatRuntimeException
-
getDoubleData
public double[] getDoubleData() throws java.lang.UnsupportedOperationException, ApfloatRuntimeException
Returns the array of this array access as adouble[]
.- Returns:
- The backing array of this array access.
- Throws:
java.lang.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 interfacejava.lang.AutoCloseable
- Throws:
ApfloatRuntimeException
-
-