simtools.data.buffer
Class Buffer

java.lang.Object
  extended by simtools.data.ValueProvider
      extended by simtools.data.buffer.Buffer
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable
Direct Known Subclasses:
SimpleBuffer

public abstract class Buffer
extends ValueProvider
implements java.lang.Cloneable, java.io.Serializable

A Buffer provides a buffering service for values, based on an index. To do that, it asks a ValueProvider for new unbuffered values when necessary. It should implement a strategy in order to minimize calls to the ValueProvider getUnbuffered method. The Buffer is also a ValueProvider itself, of course. A simple strategy is proposed with the AgeBufferPolicy, where the oldest entry is removed to make space for a new entry when the buffer is full. More intelligent or faster policies may be implemented by subclasses, such as using the values access frequency, or managing ranges of index.

Version:
1.0 2001
Author:
Nicolas Brodu
See Also:
Serialized Form

Nested Class Summary
 class Buffer.ByteManipulator
          This class handles optimization for byte objects.
 class Buffer.DoubleManipulator
          This class handles optimization for double objects.
 class Buffer.FloatManipulator
          This class handles optimization for float objects.
 class Buffer.IntegerManipulator
          This class handles optimization for int objects.
 class Buffer.LongManipulator
          This class handles optimization for long objects.
static interface Buffer.Manipulator
           
 class Buffer.ObjectManipulator
          This class handles optimization for Object objects.
 class Buffer.ShortManipulator
          This class handles optimization for short objects.
 
Field Summary
protected  Buffer.ByteManipulator byteManipulator
           
protected  Buffer.DoubleManipulator doubleManipulator
           
protected  long endIndex
           
protected  Buffer.FloatManipulator floatManipulator
           
protected  Buffer.IntegerManipulator intManipulator
           
protected  Buffer.LongManipulator longManipulator
           
protected  Buffer.ObjectManipulator objectManipulator
           
protected  ValueProvider provider
           
protected  Buffer.ShortManipulator shortManipulator
           
protected  long startIndex
           
 
Fields inherited from class simtools.data.ValueProvider
ByteProvider, DoubleProvider, FloatProvider, IntegerProvider, kind, LongProvider, ObjectProvider, ShortProvider
 
Constructor Summary
Buffer()
           
Buffer(int type)
           
Buffer(int type, ValueProvider vp)
           
Buffer(ValueProvider vp)
           
 
Method Summary
 void clear()
           
 java.lang.Object clone()
           
protected abstract  Buffer.Manipulator createManipulator(int kind)
          Manipulator factory Subclasses shall implement it to return their specialized versions of Manipulators.
 byte getByteValue(long index)
          Optimized accessor for byte values.
 double getDoubleValue(long index)
          Optimized accessor for double values.
 long getEndIndex()
           
 float getFloatValue(long index)
          Optimized accessor for float values.
 int getIntegerValue(long index)
          Optimized accessor for int values.
 int getKind()
          The kind of this provider may be used fo optimization purpose.
 long getLongValue(long index)
          Optimized accessor for long values.
 ValueProvider getProvider()
           
 short getShortValue(long index)
          Optimized accessor for short values.
 long getStartIndex()
           
 java.lang.Object getValue(long index)
           
 boolean setByteValue(long index, byte value)
           
 boolean setDoubleValue(long index, double value)
           
 boolean setFloatValue(long index, float value)
           
 boolean setIntegerValue(long index, int value)
           
 boolean setLongValue(long index, long value)
           
 void setProvider(ValueProvider vp)
           
 boolean setShortValue(long index, short value)
           
 void setSlice(long min, long max)
          Sets a region of interest in the buffer.
 boolean setValue(long index, java.lang.Object value)
          Sets a new value in the buffer, if possible.
 
Methods inherited from class simtools.data.ValueProvider
getObjectValue, valueClass
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

provider

protected transient ValueProvider provider

objectManipulator

protected transient Buffer.ObjectManipulator objectManipulator

byteManipulator

protected transient Buffer.ByteManipulator byteManipulator

shortManipulator

protected transient Buffer.ShortManipulator shortManipulator

intManipulator

protected transient Buffer.IntegerManipulator intManipulator

longManipulator

protected transient Buffer.LongManipulator longManipulator

floatManipulator

protected transient Buffer.FloatManipulator floatManipulator

doubleManipulator

protected transient Buffer.DoubleManipulator doubleManipulator

startIndex

protected transient long startIndex

endIndex

protected transient long endIndex
Constructor Detail

Buffer

public Buffer()

Buffer

public Buffer(int type)

Buffer

public Buffer(ValueProvider vp)

Buffer

public Buffer(int type,
              ValueProvider vp)
Method Detail

setProvider

public void setProvider(ValueProvider vp)

getProvider

public ValueProvider getProvider()

setValue

public boolean setValue(long index,
                        java.lang.Object value)
                 throws DataException
Sets a new value in the buffer, if possible. With getValue(), the buffer is polling information from ValueProviders With setValue(), an external source may push information to the buffer The buffer implementation may, or may not, take the new value into account. But if it does, this may lead to quite a big optimization. For example, pathIterators in graphical shapes are run one after the other. But a collective data source may read those values in the same file, all the source values for a given index on the same line. So, the pathIterators would run through the file n times, when they could have run through it only once if only they were doing their job in parallel. Since it's not possible to change that, the data source collection pushes the other values read on the same line directly to the data source buffers.

Parameters:
index - The index for which a new value is given
value - The new value
Returns:
true if the new value could be put in the buffer.
Throws:
DataException

setSlice

public void setSlice(long min,
                     long max)
Sets a region of interest in the buffer. This is a purely informative function, but the buffer may use this information to optimize itself on this specific range.


clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object

getEndIndex

public long getEndIndex()
Returns:
long

getStartIndex

public long getStartIndex()
Returns:
long

getValue

public java.lang.Object getValue(long index)
                          throws DataException
Specified by:
getValue in class ValueProvider
Throws:
DataException

getByteValue

public byte getByteValue(long index)
                  throws DataException
Description copied from class: ValueProvider
Optimized accessor for byte values. If the provider can provide bytes directly, then it should override this method. Otherwise, the default implementation will do its best to convert the Object returned by getValue() to a byte. This conversion also parses strings, and returns 0 if it failed. An exception is thrown if, and only if, getValue throws it.

Overrides:
getByteValue in class ValueProvider
Throws:
DataException

setByteValue

public boolean setByteValue(long index,
                            byte value)
                     throws DataException
Throws:
DataException

getShortValue

public short getShortValue(long index)
                    throws DataException
Description copied from class: ValueProvider
Optimized accessor for short values. If the provider can provide shorts directly, then it should override this method. Otherwise, the default implementation will do its best to convert the Object returned by getValue() to a short. This conversion also parses strings, and returns 0 if it failed. An exception is thrown if, and only if, getValue throws it.

Overrides:
getShortValue in class ValueProvider
Throws:
DataException

setShortValue

public boolean setShortValue(long index,
                             short value)
                      throws DataException
Throws:
DataException

getIntegerValue

public int getIntegerValue(long index)
                    throws DataException
Description copied from class: ValueProvider
Optimized accessor for int values. If the provider can provide ints directly, then it should override this method. Otherwise, the default implementation will do its best to convert the Object returned by getValue() to a int. This conversion also parses strings, and returns 0 if it failed. An exception is thrown if, and only if, getValue throws it.

Overrides:
getIntegerValue in class ValueProvider
Throws:
DataException

setIntegerValue

public boolean setIntegerValue(long index,
                               int value)
                        throws DataException
Throws:
DataException

getLongValue

public long getLongValue(long index)
                  throws DataException
Description copied from class: ValueProvider
Optimized accessor for long values. If the provider can provide longs directly, then it should override this method. Otherwise, the default implementation will do its best to convert the Object returned by getValue() to a long. This conversion also parses strings, and returns 0 if it failed. An exception is thrown if, and only if, getValue throws it.

Overrides:
getLongValue in class ValueProvider
Throws:
DataException

setLongValue

public boolean setLongValue(long index,
                            long value)
                     throws DataException
Throws:
DataException

getFloatValue

public float getFloatValue(long index)
                    throws DataException
Description copied from class: ValueProvider
Optimized accessor for float values. If the provider can provide floats directly, then it should override this method. Otherwise, the default implementation will do its best to convert the Object returned by getValue() to a float. This conversion also parses strings, and returns 0 if it failed. An exception is thrown if, and only if, getValue throws it.

Overrides:
getFloatValue in class ValueProvider
Throws:
DataException

setFloatValue

public boolean setFloatValue(long index,
                             float value)
                      throws DataException
Throws:
DataException

getDoubleValue

public double getDoubleValue(long index)
                      throws DataException
Description copied from class: ValueProvider
Optimized accessor for double values. If the provider can provide doubles directly, then it should override this method. Otherwise, the default implementation will do its best to convert the Object returned by getValue() to a double. This conversion also parses strings, and returns 0 if it failed. An exception is thrown if, and only if, getValue throws it.

Overrides:
getDoubleValue in class ValueProvider
Throws:
DataException

setDoubleValue

public boolean setDoubleValue(long index,
                              double value)
                       throws DataException
Throws:
DataException

createManipulator

protected abstract Buffer.Manipulator createManipulator(int kind)
Manipulator factory Subclasses shall implement it to return their specialized versions of Manipulators.

Parameters:
kind -
Returns:
Manipulator a Manipulator which must be a subclass of the corresponding TypeManipulator.

getKind

public int getKind()
Description copied from class: ValueProvider
The kind of this provider may be used fo optimization purpose. This is just an indication for getXXXValue typed functions.

Overrides:
getKind in class ValueProvider
Returns:
the buffer type as one of the constants defined in this class

clear

public void clear()