com.dyuproject.protostuff.runtime
Class IdStrategy

java.lang.Object
  extended by com.dyuproject.protostuff.runtime.IdStrategy
Direct Known Subclasses:
DefaultIdStrategy

public abstract class IdStrategy
extends java.lang.Object

This base class handles all the IO for reading and writing polymorphic fields. When a field's type is polymorphic/dynamic (e.g interface/abstract/object), the type (id) needs to be written (ahead) before its value/content to be able to deserialize it correctly. The underlying impl will determine how the type (id) should be written. An IdStrategy is standalone if the primaryGroup is not set.

Author:
Leo Romanoff, David Yu

Nested Class Summary
static interface IdStrategy.Factory
          Responsible for instantiating custom IdStrategy impls.
static class IdStrategy.UnknownTypeException
          Thrown when a type is not known by the IdStrategy.
 
Field Summary
 int groupId
           
 IdStrategy primaryGroup
           
 
Constructor Summary
protected IdStrategy(IdStrategy primaryGroup, int groupId)
           
 
Method Summary
protected abstract  CollectionSchema.MessageFactory getCollectionFactory(java.lang.Class<?> clazz)
          Returns the CollectionSchema.MessageFactory.
abstract
<T> Delegate<T>
getDelegate(java.lang.Class<? super T> typeClass)
          Returns the delegate.
protected abstract  EnumIO<? extends java.lang.Enum<?>> getEnumIO(java.lang.Class<?> enumClass)
          Returns the EnumIO.
protected abstract  MapSchema.MessageFactory getMapFactory(java.lang.Class<?> clazz)
          Returns the MapSchema.MessageFactory.
abstract
<T> HasSchema<T>
getSchemaWrapper(java.lang.Class<T> typeClass, boolean create)
          Returns the schema wrapper.
abstract  boolean isDelegateRegistered(java.lang.Class<?> typeClass)
          Returns true if there is a Delegate explicitly registered for the typeClass.
abstract  boolean isRegistered(java.lang.Class<?> typeClass)
          Returns true if the typeClass is explicitly registered.
protected
<T> Schema<T>
newSchema(java.lang.Class<T> typeClass)
          Generates a schema from the given class.
protected abstract  java.lang.Class<?> resolveArrayComponentTypeFrom(Input input, boolean mapped)
           
protected abstract  java.lang.Class<?> resolveClassFrom(Input input, boolean mapped, boolean array)
           
protected abstract  CollectionSchema.MessageFactory resolveCollectionFrom(Input input)
           
protected abstract
<T> Delegate<T>
resolveDelegateFrom(Input input)
           
protected abstract  EnumIO<?> resolveEnumFrom(Input input)
           
protected abstract  MapSchema.MessageFactory resolveMapFrom(Input input)
           
protected abstract
<T> HasSchema<T>
resolvePojoFrom(Input input, int fieldNumber)
           
protected abstract  void transferArrayId(Input input, Output output, int fieldNumber, boolean mapped)
           
protected abstract  void transferClassId(Input input, Output output, int fieldNumber, boolean mapped, boolean array)
           
protected abstract  void transferCollectionId(Input input, Output output, int fieldNumber)
           
protected abstract
<T> Delegate<T>
transferDelegateId(Input input, Output output, int fieldNumber)
           
protected abstract  void transferEnumId(Input input, Output output, int fieldNumber)
           
protected abstract  void transferMapId(Input input, Output output, int fieldNumber)
           
protected abstract
<T> HasSchema<T>
transferPojoId(Input input, Output output, int fieldNumber)
           
protected abstract
<T> Delegate<T>
tryWriteDelegateIdTo(Output output, int fieldNumber, java.lang.Class<T> clazz)
          If this method returns null, the clazz was not registered as a delegate.
protected abstract  void writeArrayIdTo(Output output, java.lang.Class<?> componentType)
           
protected abstract  void writeClassIdTo(Output output, java.lang.Class<?> componentType, boolean array)
           
protected abstract  void writeCollectionIdTo(Output output, int fieldNumber, java.lang.Class<?> clazz)
           
protected abstract  void writeEnumIdTo(Output output, int fieldNumber, java.lang.Class<?> clazz)
           
protected abstract  void writeMapIdTo(Output output, int fieldNumber, java.lang.Class<?> clazz)
           
protected abstract
<T> Schema<T>
writeMessageIdTo(Output output, int fieldNumber, Message<T> message)
           
protected abstract
<T> Schema<T>
writePojoIdTo(Output output, int fieldNumber, java.lang.Class<T> clazz)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

primaryGroup

public final IdStrategy primaryGroup

groupId

public final int groupId
Constructor Detail

IdStrategy

protected IdStrategy(IdStrategy primaryGroup,
                     int groupId)
Method Detail

newSchema

protected <T> Schema<T> newSchema(java.lang.Class<T> typeClass)
Generates a schema from the given class. If this strategy is part of a group, the existing fields of that group's schema will be re-used.


isDelegateRegistered

public abstract boolean isDelegateRegistered(java.lang.Class<?> typeClass)
Returns true if there is a Delegate explicitly registered for the typeClass.


getDelegate

public abstract <T> Delegate<T> getDelegate(java.lang.Class<? super T> typeClass)
Returns the delegate.


isRegistered

public abstract boolean isRegistered(java.lang.Class<?> typeClass)
Returns true if the typeClass is explicitly registered.


getSchemaWrapper

public abstract <T> HasSchema<T> getSchemaWrapper(java.lang.Class<T> typeClass,
                                                  boolean create)
Returns the schema wrapper. The caller is responsible that the typeClass is a pojo (e.g not an enum/array/etc).


getEnumIO

protected abstract EnumIO<? extends java.lang.Enum<?>> getEnumIO(java.lang.Class<?> enumClass)
Returns the EnumIO. The callers (internal field factories) are responsible that the class provided is an enum class.


getCollectionFactory

protected abstract CollectionSchema.MessageFactory getCollectionFactory(java.lang.Class<?> clazz)
Returns the CollectionSchema.MessageFactory. The callers (internal field factories) are responsible that the class provided implements Collection.


getMapFactory

protected abstract MapSchema.MessageFactory getMapFactory(java.lang.Class<?> clazz)
Returns the MapSchema.MessageFactory. The callers (internal field factories}) are responsible that the class provided implements .


writeCollectionIdTo

protected abstract void writeCollectionIdTo(Output output,
                                            int fieldNumber,
                                            java.lang.Class<?> clazz)
                                     throws java.io.IOException
Throws:
java.io.IOException

transferCollectionId

protected abstract void transferCollectionId(Input input,
                                             Output output,
                                             int fieldNumber)
                                      throws java.io.IOException
Throws:
java.io.IOException

resolveCollectionFrom

protected abstract CollectionSchema.MessageFactory resolveCollectionFrom(Input input)
                                                                  throws java.io.IOException
Throws:
java.io.IOException

writeMapIdTo

protected abstract void writeMapIdTo(Output output,
                                     int fieldNumber,
                                     java.lang.Class<?> clazz)
                              throws java.io.IOException
Throws:
java.io.IOException

transferMapId

protected abstract void transferMapId(Input input,
                                      Output output,
                                      int fieldNumber)
                               throws java.io.IOException
Throws:
java.io.IOException

resolveMapFrom

protected abstract MapSchema.MessageFactory resolveMapFrom(Input input)
                                                    throws java.io.IOException
Throws:
java.io.IOException

writeEnumIdTo

protected abstract void writeEnumIdTo(Output output,
                                      int fieldNumber,
                                      java.lang.Class<?> clazz)
                               throws java.io.IOException
Throws:
java.io.IOException

transferEnumId

protected abstract void transferEnumId(Input input,
                                       Output output,
                                       int fieldNumber)
                                throws java.io.IOException
Throws:
java.io.IOException

resolveEnumFrom

protected abstract EnumIO<?> resolveEnumFrom(Input input)
                                      throws java.io.IOException
Throws:
java.io.IOException

writePojoIdTo

protected abstract <T> Schema<T> writePojoIdTo(Output output,
                                               int fieldNumber,
                                               java.lang.Class<T> clazz)
                                    throws java.io.IOException
Throws:
java.io.IOException

transferPojoId

protected abstract <T> HasSchema<T> transferPojoId(Input input,
                                                   Output output,
                                                   int fieldNumber)
                                        throws java.io.IOException
Throws:
java.io.IOException

resolvePojoFrom

protected abstract <T> HasSchema<T> resolvePojoFrom(Input input,
                                                    int fieldNumber)
                                         throws java.io.IOException
Throws:
java.io.IOException

writeMessageIdTo

protected abstract <T> Schema<T> writeMessageIdTo(Output output,
                                                  int fieldNumber,
                                                  Message<T> message)
                                       throws java.io.IOException
Throws:
java.io.IOException

tryWriteDelegateIdTo

protected abstract <T> Delegate<T> tryWriteDelegateIdTo(Output output,
                                                        int fieldNumber,
                                                        java.lang.Class<T> clazz)
                                             throws java.io.IOException
If this method returns null, the clazz was not registered as a delegate.

Throws:
java.io.IOException

transferDelegateId

protected abstract <T> Delegate<T> transferDelegateId(Input input,
                                                      Output output,
                                                      int fieldNumber)
                                           throws java.io.IOException
Throws:
java.io.IOException

resolveDelegateFrom

protected abstract <T> Delegate<T> resolveDelegateFrom(Input input)
                                            throws java.io.IOException
Throws:
java.io.IOException

writeArrayIdTo

protected abstract void writeArrayIdTo(Output output,
                                       java.lang.Class<?> componentType)
                                throws java.io.IOException
Throws:
java.io.IOException

transferArrayId

protected abstract void transferArrayId(Input input,
                                        Output output,
                                        int fieldNumber,
                                        boolean mapped)
                                 throws java.io.IOException
Throws:
java.io.IOException

resolveArrayComponentTypeFrom

protected abstract java.lang.Class<?> resolveArrayComponentTypeFrom(Input input,
                                                                    boolean mapped)
                                                             throws java.io.IOException
Throws:
java.io.IOException

writeClassIdTo

protected abstract void writeClassIdTo(Output output,
                                       java.lang.Class<?> componentType,
                                       boolean array)
                                throws java.io.IOException
Throws:
java.io.IOException

transferClassId

protected abstract void transferClassId(Input input,
                                        Output output,
                                        int fieldNumber,
                                        boolean mapped,
                                        boolean array)
                                 throws java.io.IOException
Throws:
java.io.IOException

resolveClassFrom

protected abstract java.lang.Class<?> resolveClassFrom(Input input,
                                                       boolean mapped,
                                                       boolean array)
                                                throws java.io.IOException
Throws:
java.io.IOException


Copyright © 2009-2014. All Rights Reserved.