step.encoding
Class IdentifierStrategy

java.lang.Object
  |
  +--step.encoding.Encoder
        |
        +--step.encoding.Strategy
              |
              +--step.encoding.RegularValueStrategy
                    |
                    +--step.encoding.IdentifierStrategy
Direct Known Subclasses:
PIdentifierStrategy

public class IdentifierStrategy
extends RegularValueStrategy

Assume that values derive from a relatively small, fixed distribution. Instead of encoding the full value, use a small integer ID in its place.

NOTE: The ID values are encoded with a CreepIntEncoder, thus the strategy is optimized for distributions with < 128 distinct values.


Field Summary
static String STRATEGY_NAME
           
 
Fields inherited from class step.encoding.RegularValueStrategy
_nextIsRegular
 
Fields inherited from class step.encoding.Strategy
_baseEncoder
 
Constructor Summary
IdentifierStrategy(Encoder baseEncoder)
          Create a new "identifier" strategy.
 
Method Summary
protected  void add(StepObject value)
          Pre-map a value in the expected distribution.
 StepObject decode(DecodeContext context)
          Decode an ID, map it to a value.
 void dump(int level)
           
 void encode(StepObject o, EncodeContext context)
          Encode a value as an ID.
protected  Object idToValue(StepObject id)
          Get the current value for this ID.
protected  Object valueToId(StepObject value)
          Get the current ID for this value.
 
Methods inherited from class step.encoding.RegularValueStrategy
setNextIsRegular
 
Methods inherited from class step.encoding.Strategy
baseDecode, baseEncode, eventMessage
 
Methods inherited from class step.encoding.Encoder
getFactory, getName, indent, makeFieldName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STRATEGY_NAME

public static final String STRATEGY_NAME
See Also:
Constant Field Values
Constructor Detail

IdentifierStrategy

public IdentifierStrategy(Encoder baseEncoder)
Create a new "identifier" strategy.

Parameters:
baseEncoder - the encoder to defer to when writing previously unmapped values.
Method Detail

encode

public void encode(StepObject o,
                   EncodeContext context)
            throws EncoderException,
                   IOException
Encode a value as an ID.

Specified by:
encode in class Encoder
Throws:
IOException - if the encode operation encountered an I/O failure.
EncoderException - if the encode operation encountered an error.

decode

public StepObject decode(DecodeContext context)
                  throws EncoderException,
                         IOException
Decode an ID, map it to a value.

Specified by:
decode in class Encoder
Throws:
IOException - if the decode operation encountered an I/O failure.
EncoderException - if the decode operation encountered an error.

valueToId

protected Object valueToId(StepObject value)
Get the current ID for this value.

Returns:
the current ID (as a StepInt) or null if no ID is defined for the value.

idToValue

protected Object idToValue(StepObject id)
Get the current value for this ID.

Returns:
the current value associated with the ID or null if no value is defined for the ID.

add

protected void add(StepObject value)
Pre-map a value in the expected distribution.

WARNING: This method is provided for bootstrapping purposes. Don't use it unless you understand the consequences.

Throws:
IllegalArgumentException - if a mapping already exists for the value.

dump

public void dump(int level)
Specified by:
dump in class Encoder