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
The ArrayAccess class simulates a C language pointer. With one ArrayAccess object you can point to a location within an array. You can easily add or subtract a value to this "pointer", thus essentially emulating C pointer arithmetic. An ArrayAccess 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 an ArrayAccess object to make sure that he doesn't access the provided array outside the allowed range. The ArrayAccess object itself does nothing to enforce this, except of course the mandatory bounds check of Java, which can throw an ArrayIndexOutOfBoundsException.

Version:
1.9.0
Author:
Mikko Tommila
See Also:
  • 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 this array access and commit any changes to the underlying data storage if applicable.
    abstract Object
    Returns the array of this array access.
    double[]
    Returns the array of this array access as a double[].
    float[]
    Returns the array of this array access as a float[].
    int[]
    Returns the array of this array access as an int[].
    int
    Returns the length of the access segment within the backing array.
    long[]
    Returns the array of this array access as a long[].
    int
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ArrayAccess

      protected ArrayAccess(int offset, int length)
      Create an array access.

      Parameters:
      offset - The offset of the access segment within the array.
      length - The access segment.
  • Method Details

    • 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 an int[].
      Returns:
      The backing array of this array access.
      Throws:
      UnsupportedOperationException - In case the backing array can't be presented as int[].
      ApfloatRuntimeException
    • getLongData

      public long[] getLongData() throws UnsupportedOperationException, ApfloatRuntimeException
      Returns the array of this array access as a long[].
      Returns:
      The backing array of this array access.
      Throws:
      UnsupportedOperationException - In case the backing array can't be presented as long[].
      ApfloatRuntimeException
    • getFloatData

      public float[] getFloatData() throws UnsupportedOperationException, ApfloatRuntimeException
      Returns the array of this array access as a float[].
      Returns:
      The backing array of this array access.
      Throws:
      UnsupportedOperationException - In case the backing array can't be presented as float[].
      ApfloatRuntimeException
    • getDoubleData

      public double[] getDoubleData() throws UnsupportedOperationException, ApfloatRuntimeException
      Returns the array of this array access as a double[].
      Returns:
      The backing array of this array access.
      Throws:
      UnsupportedOperationException - In case the backing array can't be presented as double[].
      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 the ArrayAccess 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 interface AutoCloseable
      Throws:
      ApfloatRuntimeException