Interface DataBufferWindow<B extends DataBuffer<?>>

  • Type Parameters:
    B - the type of buffer being viewed
    All Known Implementing Classes:
    AbstractDataBufferWindow

    public interface DataBufferWindow<B extends DataBuffer<?>>
    A mutable container for viewing part of a DataBuffer.

    Data buffer windows have a fixed size and can "slide" along a buffer to provide different views of the data without allocating a new buffer instance, like DataBuffer.offset(long) does. This improves overall performance when this operation is repeated frequently. For example:

    
     IntDataBuffer bufferA = DataBuffers.ofInts(1024);
     // ... init buffer data
     IntDataBuffer bufferB = DataBuffers.ofInts(1, 2, 3, 4);
    
     // Return the index of the first occurrence of bufferB in bufferA using a sliding window
     DataBufferWindow<IntDataBuffer> windowA = bufferA.window(4);
     for (int i = 0; i < bufferA.size() - bufferB.size(); ++i) {
         if (windowA.slideTo(i).buffer().equals(bufferB)) {
             return i;
         }
     }
     

    DataBufferWindow instances are stateful and not thread-safe.

    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      B buffer()
      Returns the buffer backing this window.
      long offset()
      Returns the current offset of this window in the original buffer.
      long size()
      Returns the size of this buffer window.
      DataBufferWindow<B> slide​(long step)
      Moves the window of step elements in the original buffer.
      DataBufferWindow<B> slideTo​(long index)
      Moves the window at the given position in the original buffer.
    • Method Detail

      • offset

        long offset()
        Returns the current offset of this window in the original buffer.
      • size

        long size()
        Returns the size of this buffer window.
      • slideTo

        DataBufferWindow<B> slideTo​(long index)
        Moves the window at the given position in the original buffer.

        The size of the window remains the same and its offset is set to index, so that accessing the value of buffer() at index x will return the value at index + x in the original buffer.

        Parameters:
        index - new offset for this window
        Returns:
        this instance
        Throws:
        IndexOutOfBoundsException - if the window cannot be slid because it goes beyond the original buffer limits
      • slide

        DataBufferWindow<B> slide​(long step)
        Moves the window of step elements in the original buffer.

        The size of the window remains the same and its offset is set to offset() + step. If step is positive, then the window will slide forward. If it is negative, it will slide backward.

        Parameters:
        step - value to add to the current offset of this window
        Returns:
        this instance
        Throws:
        IndexOutOfBoundsException - if the window cannot be slid because it goes beyond the original buffer limits
      • buffer

        B buffer()
        Returns the buffer backing this window.

        Each window instance has it's own buffer providing a view onto the original DataBuffer. The buffers are mutated when the window slides to different offsets. For example:

        
         IntDataBuffer buffer = DataBuffers.of(0, 1, 2, 3);
         DataBufferWindow<IntDataBuffer> window = buffer.window(0, 2);
        
         IntDataBuffer windowBuffer = window.buffer();
         assertEquals(0, windowBuffer.getInt(0));
         assertEquals(1, windowBuffer.getInt(1));
        
         window.slideTo(2);
         assertEquals(2, windowBuffer.getInt(0));
         assertEquals(3, windowBuffer.getInt(1));
         assertSame(windowBuffer, window.buffer());
         
        Returns:
        this window's buffer