Package org.apfloat.spi
Class DataStorage
- java.lang.Object
-
- org.apfloat.spi.DataStorage
-
- All Implemented Interfaces:
java.io.Serializable
- Direct Known Subclasses:
DiskDataStorage,DoubleMemoryDataStorage,FloatMemoryDataStorage,IntMemoryDataStorage,LongMemoryDataStorage
public abstract class DataStorage extends java.lang.Object implements java.io.SerializableGeneric data storage class.Initially when a data storage is created, it is mutable (it can be modified). After the contents have been properly set, the user should call
setReadOnly()to set the storage to be immutable. After this the data storage can be safely shared between different users.Access to
DataStorageobjects is generally not internally synchronized. Accessing multiple non-overlapping parts of the storage concurrently with thegetArray(int,long,int),getArray(int,int,int,int)orgetTransposedArray(int,int,int,int)method and with iterators over non-overlapping parts is permitted. Invoking other methods must generally be externally synchronized.- Version:
- 1.8.1
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classDataStorage.AbstractIteratorAbstract base class for iterators iterating through thisDataStorage.static classDataStorage.IteratorIterator for iterating through elements of the data storage.
-
Field Summary
Fields Modifier and Type Field Description private booleanisReadOnlyprivate booleanisSubsequencedprivate longlengthprivate longoffsetprivate DataStorageoriginalDataStoragestatic intREADRead access mode specifier.static intREAD_WRITERead-write access mode specifier.private static longserialVersionUIDstatic intWRITEWrite access mode specifier.
-
Constructor Summary
Constructors Modifier Constructor Description protectedDataStorage()Default constructor.protectedDataStorage(DataStorage dataStorage, long offset, long length)Subsequence constructor.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidcopyFrom(DataStorage dataStorage)Copies all data from another data storage to this data storage.voidcopyFrom(DataStorage dataStorage, long size)Copies the specified number of elements from another data storage to this data storage.ArrayAccessgetArray(int mode, int startColumn, int columns, int rows)Maps a block of data to a memory array when the data is treated as a matrix.ArrayAccessgetArray(int mode, long offset, int length)Gets an array access to the data of this data storage when the data is treated as a linear block.protected longgetOffset()Return the sub-sequence offset.longgetSize()Return the size of the data storage, or the length of this sub-sequence if this data storage is a sub-sequence.ArrayAccessgetTransposedArray(int mode, int startColumn, int columns, int rows)Maps a transposed block of data to a memory array when the data is treated as a matrix.protected abstract voidimplCopyFrom(DataStorage dataStorage, long size)Copies the specified number of elements from another data storage to this data storage.protected abstract ArrayAccessimplGetArray(int mode, int startColumn, int columns, int rows)Maps a block of data to a memory array when the data is treated as a matrix.protected abstract ArrayAccessimplGetArray(int mode, long offset, int length)Gets an array access to the data of this data storage when it is treated as a linear block.protected abstract longimplGetSize()Return the size of the whole data storage, not including sub-sequence settings.protected abstract ArrayAccessimplGetTransposedArray(int mode, int startColumn, int columns, int rows)Maps a transposed block of data to a memory array when the data is treated as a matrix.protected abstract voidimplSetSize(long size)Sets the size of the data storage.protected abstract DataStorageimplSubsequence(long offset, long length)Implementation of getting a subsequence of this data storage.abstract booleanisCached()Is this object cached in memory.booleanisReadOnly()Returns the read-only state of this data storage.booleanisSubsequenced()Is this object a subsequence of some other object, or do subsequences of this object exist.abstract DataStorage.Iteratoriterator(int mode, long startPosition, long endPosition)Constructs a new iterator.voidsetReadOnly()Sets this data storage as read-only.voidsetSize(long size)Sets the size of the data storage.private voidsetSubsequenced()DataStoragesubsequence(long offset, long length)Get a subsequence of this data storage.
-
-
-
Field Detail
-
READ
public static final int READ
Read access mode specifier.- See Also:
- Constant Field Values
-
WRITE
public static final int WRITE
Write access mode specifier.- See Also:
- Constant Field Values
-
READ_WRITE
public static final int READ_WRITE
Read-write access mode specifier. For convenience, equivalent toREAD | WRITE.- See Also:
- Constant Field Values
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
offset
private long offset
-
length
private long length
-
originalDataStorage
private DataStorage originalDataStorage
-
isReadOnly
private boolean isReadOnly
-
isSubsequenced
private boolean isSubsequenced
-
-
Constructor Detail
-
DataStorage
protected DataStorage()
Default constructor. To be called by subclasses when creating a new emptyDataStorage.
-
DataStorage
protected DataStorage(DataStorage dataStorage, long offset, long length)
Subsequence constructor. To be called by subclasses when creating a subsequence of an existing DataStorage.- Parameters:
dataStorage- The originating data storage.offset- The subsequence starting position.length- The subsequence length.
-
-
Method Detail
-
subsequence
public final DataStorage subsequence(long offset, long length) throws java.lang.IllegalArgumentException, ApfloatRuntimeException
Get a subsequence of this data storage.- Parameters:
offset- The subsequence starting position.length- The subsequence length.- Returns:
- Data storage that represents the specified part of this data storage.
- Throws:
java.lang.IllegalArgumentException- If the requested subsequence is out of range.ApfloatRuntimeException
-
implSubsequence
protected abstract DataStorage implSubsequence(long offset, long length) throws ApfloatRuntimeException
Implementation of getting a subsequence of this data storage. The validity of the arguments of this method do not need to be checked.- Parameters:
offset- The subsequence starting position.length- The subsequence length.- Returns:
- Data storage that represents the specified part of this data storage.
- Throws:
ApfloatRuntimeException
-
copyFrom
public final void copyFrom(DataStorage dataStorage) throws java.lang.IllegalArgumentException, java.lang.IllegalStateException, ApfloatRuntimeException
Copies all data from another data storage to this data storage.- Parameters:
dataStorage- The data storage where the data should be copied from.- Throws:
java.lang.IllegalArgumentException- If the origin data source has a size of zero.java.lang.IllegalStateException- If this data storage is read-only or has subsequences.ApfloatRuntimeException
-
copyFrom
public final void copyFrom(DataStorage dataStorage, long size) throws java.lang.IllegalArgumentException, java.lang.IllegalStateException, ApfloatRuntimeException
Copies the specified number of elements from another data storage to this data storage.- Parameters:
dataStorage- The data storage where the data should be copied from.size- The number of elements to be copied.- Throws:
java.lang.IllegalArgumentException- If the size is invalid or zero.java.lang.IllegalStateException- If this data storage is read-only or has subsequences.ApfloatRuntimeException
-
implCopyFrom
protected abstract void implCopyFrom(DataStorage dataStorage, long size) throws ApfloatRuntimeException
Copies the specified number of elements from another data storage to this data storage. The validity of the arguments of this method do not need to be checked.- Parameters:
dataStorage- The data storage where the data should be copied from.size- The number of elements to be copied.- Throws:
ApfloatRuntimeException
-
getSize
public final long getSize() throws ApfloatRuntimeExceptionReturn the size of the data storage, or the length of this sub-sequence if this data storage is a sub-sequence.- Returns:
- The size of the data storage.
- Throws:
ApfloatRuntimeException
-
implGetSize
protected abstract long implGetSize() throws ApfloatRuntimeExceptionReturn the size of the whole data storage, not including sub-sequence settings.- Returns:
- The size of the whole data storage, not including sub-sequence settings.
- Throws:
ApfloatRuntimeException
-
setSize
public final void setSize(long size) throws java.lang.IllegalArgumentException, java.lang.IllegalStateException, ApfloatRuntimeExceptionSets the size of the data storage.- Parameters:
size- The size of the data storage.- Throws:
java.lang.IllegalArgumentException- If the size is invalid or zero.java.lang.IllegalStateException- If this data storage is read-only or has subsequences.ApfloatRuntimeException
-
implSetSize
protected abstract void implSetSize(long size) throws ApfloatRuntimeExceptionSets the size of the data storage. The validity of the arguments of this method do not need to be checked.- Parameters:
size- The size of the data storage.- Throws:
ApfloatRuntimeException
-
isReadOnly
public final boolean isReadOnly()
Returns the read-only state of this data storage.- Returns:
trueif this data storage is read-only, otherwisefalse.
-
setReadOnly
public final void setReadOnly() throws ApfloatRuntimeExceptionSets this data storage as read-only. All existing sub-sequences (recursively) of this data storage are set to read-only as well.- Throws:
ApfloatRuntimeException
-
getArray
public final ArrayAccess getArray(int mode, long offset, int length) throws java.lang.IllegalArgumentException, java.lang.IllegalStateException, ApfloatRuntimeException
Gets an array access to the data of this data storage when the data is treated as a linear block.- Parameters:
mode- Access mode for the array access:READ,WRITEor both.offset- Starting position of the array access in the data storage.length- Number of accessible elements in the array access.- Returns:
- The array access.
- Throws:
java.lang.IllegalArgumentException- If the offset or length are out of bounds of the data storage.java.lang.IllegalStateException- If write access is requested for a read-only data storage.ApfloatRuntimeException
-
implGetArray
protected abstract ArrayAccess implGetArray(int mode, long offset, int length) throws ApfloatRuntimeException
Gets an array access to the data of this data storage when it is treated as a linear block. The validity of the arguments of this method do not need to be checked.- Parameters:
mode- Access mode for the array access:READ,WRITEor both.offset- Starting position of the array access in the data storage.length- Number of accessible elements in the array access.- Returns:
- The array access.
- Throws:
ApfloatRuntimeException
-
getArray
public final ArrayAccess getArray(int mode, int startColumn, int columns, int rows) throws java.lang.IllegalArgumentException, java.lang.IllegalStateException, ApfloatRuntimeException
Maps a block of data to a memory array when the data is treated as a matrix. The matrix size is n1 x n2. The following picture illustrates the block being accessed (in gray):Matrix ← startColumn →← columns →↑
n1
↓← n2 →- Parameters:
mode- Whether the array is prepared for reading, writing or both. The value should beREAD,WRITEor a combination of these.startColumn- The starting column where data is read.columns- The number of columns of data to read.rows- The number of rows of data to read. This should be equivalent to n1, number of rows in the matrix.- Returns:
- Access to an array of size
columnsxrowscontaining the data. - Throws:
java.lang.IllegalArgumentException- If the requested area is out of bounds of the data storage.java.lang.IllegalStateException- If write access is requested for a read-only data storage.ApfloatRuntimeException- Since:
- 1.7.0
-
implGetArray
protected abstract ArrayAccess implGetArray(int mode, int startColumn, int columns, int rows) throws ApfloatRuntimeException
Maps a block of data to a memory array when the data is treated as a matrix. The validity of the arguments of this method do not need to be checked.- Parameters:
mode- Whether the array is prepared for reading, writing or both. The value should beREAD,WRITEor a combination of these.startColumn- The starting column where data is read.columns- The number of columns of data to read.rows- The number of rows of data to read. This should be equivalent to n1, number of rows in the matrix.- Returns:
- Access to an array of size
columnsxrowscontaining the data. - Throws:
ApfloatRuntimeException- Since:
- 1.7.0
-
getTransposedArray
public final ArrayAccess getTransposedArray(int mode, int startColumn, int columns, int rows) throws java.lang.IllegalArgumentException, java.lang.IllegalStateException, ApfloatRuntimeException
Maps a transposed block of data to a memory array when the data is treated as a matrix. The matrix size is n1 x n2. The accessed block is illustrated in gray in the following picture. The argumentcolumnsis the valueb:
The data is read from an n1 x b area of the matrix, in blocks of b elements, to a b x n1 memory array as follows:Matrix ← startColumn →A↑
n1
↓BCD← b →← n2 →
Each b x b block is transposed, to form the final b x n1 array in memory, where the columns are located linearly:Read matrix section A↑
b
↓BCD← n1 →Transposed matrix section ABCD↑
b
↓← n1 →- Parameters:
mode- Whether the array is prepared for reading, writing or both. The value should beREAD,WRITEor a combination of these.startColumn- The starting column where data is read.columns- The number of columns of data to read.rows- The number of rows of data to read. This should be equivalent to n1, number of rows in the matrix.- Returns:
- Access to an array of size
columnsxrowscontaining the transposed data. - Throws:
java.lang.IllegalArgumentException- If the requested area is out of bounds of the data storage.java.lang.IllegalStateException- If write access is requested for a read-only data storage.ApfloatRuntimeException
-
implGetTransposedArray
protected abstract ArrayAccess implGetTransposedArray(int mode, int startColumn, int columns, int rows) throws ApfloatRuntimeException
Maps a transposed block of data to a memory array when the data is treated as a matrix. The validity of the arguments of this method do not need to be checked.- Parameters:
mode- Whether the array is prepared for reading, writing or both. The value should beREAD,WRITEor a combination of these.startColumn- The starting column where data is read.columns- The number of columns of data to read.rows- The number of rows of data to read. This should be equivalent to n1, number of rows in the matrix.- Returns:
- Access to an array of size
columnsxrowscontaining the transposed data. - Throws:
ApfloatRuntimeException
-
iterator
public abstract DataStorage.Iterator iterator(int mode, long startPosition, long endPosition) throws java.lang.IllegalArgumentException, java.lang.IllegalStateException, ApfloatRuntimeException
Constructs a new iterator. Elements can be iterated either in forward or in reverse order, depending on ifstartPositionis less than or greater thanendPosition, correspondingly.- Parameters:
mode- Access mode for iterator:READ,WRITEor both.startPosition- Starting position of iterator in the data set. For reverse access, the first element in the iterator isstartPosition - 1.endPosition- End position of iterator in the data set. For forward access, the last accessible element in the iterator isendPosition - 1.- Returns:
- An iterator.
- Throws:
java.lang.IllegalArgumentException- If the requested area is out of bounds of the data storage.java.lang.IllegalStateException- If write access is requested for a read-only data storage.ApfloatRuntimeException
-
isSubsequenced
public final boolean isSubsequenced()
Is this object a subsequence of some other object, or do subsequences of this object exist.- Returns:
trueif this object is a subsequence of some other object, or if subsequences of this object exist,falseotherwise.
-
isCached
public abstract boolean isCached()
Is this object cached in memory.- Returns:
trueif this object is cached in memory,falseif not.- Since:
- 1.7.0
-
getOffset
protected final long getOffset()
Return the sub-sequence offset.- Returns:
- Absolute offset of the sub-sequence within the (top-level) base data storage.
-
setSubsequenced
private void setSubsequenced() throws ApfloatRuntimeException- Throws:
ApfloatRuntimeException
-
-