Class CombinedMemoryModel.AbstractByteBufferCombiner<E>

java.lang.Object
net.algart.arrays.CombinedMemoryModel.AbstractByteBufferCombiner<E>
All Implemented Interfaces:
CombinedMemoryModel.Combiner<E>
Direct Known Subclasses:
CombinedMemoryModel.AbstractByteBufferCombinerInPlace
Enclosing class:
CombinedMemoryModel<E>

public abstract static class CombinedMemoryModel.AbstractByteBufferCombiner<E> extends Object implements CombinedMemoryModel.Combiner<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

    Fields
    Modifier and Type
    Field
    Description
    protected final ByteBuffer
    A little ByteBuffer for storing one element of the combined array.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    AbstractByteBufferCombiner(Class<?> elementType, ByteBuffer workStorageForOneElement, MemoryModel memoryModel)
    Creates a new instance of this combiner.
  • Method Summary

    Modifier and Type
    Method
    Description
    allocateStorage(long length, boolean unresizable)
    Should create a storage (several AlgART arrays), allowing to store elements of the combined arrays.
    final E
    get(long index, Array[] storage)
    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 from workStorage.
    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 in workStorage.

    Methods inherited from class java.lang.Object

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

    • workStorage

      protected final ByteBuffer 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 - the memory 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

      protected abstract E loadElement()
      Should create one element of the combined array and fill it from workStorage.
      Returns:
      newly created object.
    • storeElement

      protected abstract void storeElement(E element)
      Should store all information about the passed element of the combiner array in workStorage.
      Parameters:
      element - the stored element.
    • get

      public final E get(long index, Array[] storage)
      Description copied from interface: CombinedMemoryModel.Combiner
      Returns an element #index of the combined array from the given set of AlgART arrays. Unlike CombinedMemoryModel.CombinerInPlace.getInPlace(long, Object, Array[]), this method always creates a new object and should work always. This method is called by Array.getElement(long).
      Specified by:
      get in interface CombinedMemoryModel.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

      public final void set(long index, E value, UpdatableArray[] storage)
      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 by UpdatableArray.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 interface CombinedMemoryModel.Combiner<E>
      Parameters:
      index - an index in the combined array.
      value - the stored element
      storage - a set of arrays where the content will be stored.
    • allocateStorage

      public final UpdatableArray[] allocateStorage(long length, boolean unresizable)
      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 use MemoryModel.newUnresizableArray method for creating storage arrays. If unresizable argument is false, every element of returned Java array must implement MutableArray interface.

      Specified by:
      allocateStorage in interface CombinedMemoryModel.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 implement MutableArray 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 interface CombinedMemoryModel.Combiner<E>
      Parameters:
      indexOfArrayInStorage - index of the storage array.
      Returns:
      the number of sequential elements used for storing one combined element.