NASA World Wind

gov.nasa.worldwind.util
Class CompoundBuffer<T,V>

java.lang.Object
  extended by gov.nasa.worldwind.util.CompoundBuffer<T,V>
Type Parameters:
T - The backing buffer type of this CompoundBuffer.
V - The sub-buffer type accepted and returned by this CompoundBuffer.
Direct Known Subclasses:
CompoundStringBuffer, CompoundVecBuffer

public abstract class CompoundBuffer<T,V>
extends java.lang.Object

CompoundBuffer provides a mechanism for constructing a single buffer from a collection of buffers with potentially different length, then indexing that 'compound' buffer to reference the individual buffer content within. Compounding separate buffers in this way is useful for grouping related data into a single block of memory, which may then be managed easlily, and easily tranferred from one system to another.


Field Summary
protected  T buffer
           
protected static int DEFAULT_INITIAL_CAPACITY
           
protected  int numSubBuffers
           
protected  int size
           
protected  java.nio.IntBuffer subLengthBuffer
           
protected  java.nio.IntBuffer subPositionBuffer
           
 
Constructor Summary
CompoundBuffer(int initialCapacity, T buffer)
          Constructs a CompoundBuffer with the specified initial capacity, and the specified backing buffer.
CompoundBuffer(int size, T buffer, java.nio.IntBuffer subPositionBuffer, java.nio.IntBuffer subLengthBuffer, int numSubBuffers)
          Creates a CompoundBuffer with the specified backing buffer size, backing buffer, sub-buffer starting positions, sub-buffer lengths, and number of sub-buffers.
CompoundBuffer(T buffer)
          Constructs a CompoundBuffer with the specified backing buffer, and a default initial capacity.
 
Method Summary
protected abstract  void append(V buffer)
           
 int appendEmptyBuffer()
          Appends an empty sub-buffer to the end of this compound buffer.
 int appendSubBuffer(V subBuffer)
          Appends the contents of the specified sub-buffer to the end of this compound buffer, incrementing the number of sub-buffers by one.
 void clear()
           
protected  int computeNewSize(int size, int minSize)
           
 T getBackingBuffer()
          Returns the backing buffer.
 int getNumSubBuffers()
          Returns the number of sub-buffers contained in the compound buffer.
protected abstract  int getSize(V buffer)
           
 V getSubBuffer(int index)
          Returns the sub-buffer identified by the specified index.
protected abstract  V getSubBuffer(int position, int length)
           
 java.nio.IntBuffer getSubLengthBuffer()
          Returns the buffer which contains the logical size of each sub-buffer in the backing buffer.
 java.nio.IntBuffer getSubPositionBuffer()
          Returns the buffer which contains the logical starting position of each sub-buffer in the backing buffer.
 int getTotalBufferSize()
          Returns the collective size of all sub buffers.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

buffer

protected T buffer

DEFAULT_INITIAL_CAPACITY

protected static final int DEFAULT_INITIAL_CAPACITY
See Also:
Constant Field Values

numSubBuffers

protected int numSubBuffers

size

protected int size

subLengthBuffer

protected java.nio.IntBuffer subLengthBuffer

subPositionBuffer

protected java.nio.IntBuffer subPositionBuffer
Constructor Detail

CompoundBuffer

public CompoundBuffer(int initialCapacity,
                      T buffer)
Constructs a CompoundBuffer with the specified initial capacity, and the specified backing buffer.

Parameters:
initialCapacity - the initial capacity to allocate, in number of sub-buffers.
buffer - the backing buffer.
Throws:
java.lang.IllegalArgumentException - if the initialCapacity is negative, or if the buffer is null.

CompoundBuffer

public CompoundBuffer(int size,
                      T buffer,
                      java.nio.IntBuffer subPositionBuffer,
                      java.nio.IntBuffer subLengthBuffer,
                      int numSubBuffers)
Creates a CompoundBuffer with the specified backing buffer size, backing buffer, sub-buffer starting positions, sub-buffer lengths, and number of sub-buffers.

Parameters:
size - the backing buffer size.
buffer - the backing buffer,
subPositionBuffer - the sub-buffer logical starting positions.
subLengthBuffer - the sub-buffer logical sizes.
numSubBuffers - the number of sub-buffers.
Throws:
java.lang.IllegalArgumentException - if size is negative, if any of the buffer, subPositionBuffer, subLengthBuffer are null, or if numSubBuffers is negative.

CompoundBuffer

public CompoundBuffer(T buffer)
Constructs a CompoundBuffer with the specified backing buffer, and a default initial capacity.

Parameters:
buffer - the compound buffer's backing buffer.
Throws:
java.lang.IllegalArgumentException - if the buffer is null.
Method Detail

append

protected abstract void append(V buffer)

appendEmptyBuffer

public int appendEmptyBuffer()
Appends an empty sub-buffer to the end of this compound buffer.

Returns:
the new sub-buffer's index.

appendSubBuffer

public int appendSubBuffer(V subBuffer)
Appends the contents of the specified sub-buffer to the end of this compound buffer, incrementing the number of sub-buffers by one. The backing buffer grows to accomodate the sub-buffer if it does not already have enough capacity to hold it. The relative logical starting position and length of the new sub-buffer are stored in this buffer, and may be recalled by either manually retrieving them from the buffers returned by #getSubPositionBuffer and #getSubLengthBuffer, or by calling #getSubBuffer.

Parameters:
subBuffer - the sub-buffer to append.
Returns:
the new sub-buffer's index.
Throws:
java.lang.IllegalArgumentException - if the subBuffer is null.

clear

public void clear()

computeNewSize

protected int computeNewSize(int size,
                             int minSize)

getBackingBuffer

public T getBackingBuffer()
Returns the backing buffer.

Returns:
the backing buffer.

getNumSubBuffers

public int getNumSubBuffers()
Returns the number of sub-buffers contained in the compound buffer.

Returns:
the number of sub-buffers.

getSize

protected abstract int getSize(V buffer)

getSubBuffer

public V getSubBuffer(int index)
Returns the sub-buffer identified by the specified index. Index i corresponds to the ith sub-buffer.

Parameters:
index - the index of the sub-buffer to get.
Returns:
the sub-buffer at the specified index.
Throws:
java.lang.IllegalArgumentException - if the index is out of range.

getSubBuffer

protected abstract V getSubBuffer(int position,
                                  int length)

getSubLengthBuffer

public java.nio.IntBuffer getSubLengthBuffer()
Returns the buffer which contains the logical size of each sub-buffer in the backing buffer.

Returns:
the buffer of sub-buffer logical sizes.

getSubPositionBuffer

public java.nio.IntBuffer getSubPositionBuffer()
Returns the buffer which contains the logical starting position of each sub-buffer in the backing buffer.

Returns:
the buffer of sub-buffer logical starting positions.

getTotalBufferSize

public int getTotalBufferSize()
Returns the collective size of all sub buffers.

Returns:
the size of all sub buffers.

NASA World Wind