Class CombinedMemoryModel.AbstractByteBufferCombiner<E>
- All Implemented Interfaces:
CombinedMemoryModel.Combiner<E>
- Direct Known Subclasses:
CombinedMemoryModel.AbstractByteBufferCombinerInPlace
- Enclosing class:
CombinedMemoryModel<E>
A skeleton class allowing to simplify implementation of
CombinedMemoryModel.Combiner
interface.
To create a combiner, based in this class, it's enough
to override only 2 very simple methods
loadElement()
and storeElement(Object)
.
These methods operate with a little ByteBuffer
workStorage
, and this class automatically
copy the content of this buffer from / to the storage array.
This combiner always use a single ByteArray
as a storage.
If you need to store different types of data (for example, int
and double fields of the stored objects),
you may use corresponding views of workStorage
buffer
(asIntBuffer(), asDoubleBuffer, etc.).
Unfortunately, for simple structure of element types,
this combiner usually work essentially slower
than the direct implementation of CombinedMemoryModel.Combiner
interface.
-
Field Summary
Modifier and TypeFieldDescriptionprotected final ByteBuffer
A little ByteBuffer for storing one element of the combined array. -
Constructor Summary
ModifierConstructorDescriptionprotected
AbstractByteBufferCombiner
(Class<?> elementType, ByteBuffer workStorageForOneElement, MemoryModel memoryModel) Creates a new instance of this combiner. -
Method Summary
Modifier and TypeMethodDescriptionfinal UpdatableArray[]
allocateStorage
(long length, boolean unresizable) Should create a storage (several AlgART arrays), allowing to store elements of the combined arrays.final E
Returns an element #index of the combined array from the given set of AlgART arrays.protected abstract E
Should create one element of the combined array and fill it fromworkStorage
.final int
numbersOfElementsPerOneCombinedElement
(int indexOfArrayInStorage) Should return the number of sequential elements of the array #indexOfArrayInStorage in the storage, used for storing one element of the combined array.final void
set
(long index, E value, UpdatableArray[] storage) Stores the element value at position #index of the combined array inside the given set of AlgART arrays.protected abstract void
storeElement
(E element) Should store all information about the passed element of the combiner array inworkStorage
.
-
Field Details
-
workStorage
A little ByteBuffer for storing one element of the combined array. This reference is copied from the corresponding constructor argument.
-
-
Constructor Details
-
AbstractByteBufferCombiner
protected AbstractByteBufferCombiner(Class<?> elementType, ByteBuffer workStorageForOneElement, MemoryModel memoryModel) Creates a new instance of this combiner.- Parameters:
elementType
- the type of elements of the combined array.workStorageForOneElement
- a little ByteBuffer enough to store one element of the combined array. May be direct ByteBuffer, but the heap one usually provides better performance.memoryModel
- thememory model
which will be used for creating combined arrays.- Throws:
NullPointerException
- if one of the arguments is null.IllegalArgumentException
- if the passed ByteBuffer is read-only.
-
-
Method Details
-
loadElement
Should create one element of the combined array and fill it fromworkStorage
.- Returns:
- newly created object.
-
storeElement
Should store all information about the passed element of the combiner array inworkStorage
.- Parameters:
element
- the stored element.
-
get
Description copied from interface:CombinedMemoryModel.Combiner
Returns an element #index of the combined array from the given set of AlgART arrays. UnlikeCombinedMemoryModel.CombinerInPlace.getInPlace(long, Object, Array[])
, this method always creates a new object and should work always. This method is called byArray.getElement(long)
.- Specified by:
get
in interfaceCombinedMemoryModel.Combiner<E>
- Parameters:
index
- an index in the combined array.storage
- a set of arrays where the retrieved content is stored now.- Returns:
- new created object containing an element #index of combined array.
-
set
Description copied from interface:CombinedMemoryModel.Combiner
Stores the element value at position #index of the combined array inside the given set of AlgART arrays. This method is called byUpdatableArray.setElement(long, Object)
.Important: this method must not throw NullPointerException if the value argument is null. Instead, it should store some "signal" value in the storage, that cannot be stored for any possible non-null elements, or just some default ("empty") value. In the first case, further
get(index, storage)
should return null; in the second case, it should return an instance in the default state.- Specified by:
set
in interfaceCombinedMemoryModel.Combiner<E>
- Parameters:
index
- an index in the combined array.value
- the stored elementstorage
- a set of arrays where the content will be stored.
-
allocateStorage
Description copied from interface:CombinedMemoryModel.Combiner
Should create a storage (several AlgART arrays), allowing to store elements of the combined arrays. Called while creating new combined arrays.The initial lengths of created arrays should be calculated on the base of passed length argument, that means the length of necessary combined array. Namely, the length of returned array #k should be equal to length*
numbersOfElementsPerOneCombinedElement
(k). This condition is automatically verified while creating combined arrays.If unresizable argument is true, it means that this method is called for creating
unresizable
combined array. In this case we recommend to useMemoryModel.newUnresizableArray
method for creating storage arrays. If unresizable argument is false, every element of returned Java array must implementMutableArray
interface.- Specified by:
allocateStorage
in interfaceCombinedMemoryModel.Combiner<E>
- Parameters:
length
- initial length of corresponding combined arrays.unresizable
- if true, the created arrays should be unresizable, in other case they must be mutable and implementMutableArray
interface.- Returns:
- created storage.
-
numbersOfElementsPerOneCombinedElement
public final int numbersOfElementsPerOneCombinedElement(int indexOfArrayInStorage) Description copied from interface:CombinedMemoryModel.Combiner
Should return the number of sequential elements of the array #indexOfArrayInStorage in the storage, used for storing one element of the combined array. Called while creating new combined arrays.- Specified by:
numbersOfElementsPerOneCombinedElement
in interfaceCombinedMemoryModel.Combiner<E>
- Parameters:
indexOfArrayInStorage
- index of the storage array.- Returns:
- the number of sequential elements used for storing one combined element.
-