org.qi4j.spi.value
Class ValueDeserializerAdapter<InputType,InputNodeType>

java.lang.Object
  extended by org.qi4j.spi.value.ValueDeserializerAdapter<InputType,InputNodeType>
Type Parameters:
InputType - Implementor pull-parser type
InputNodeType - Implementor tree-parser node type
All Implemented Interfaces:
ValueDeserializer
Direct Known Subclasses:
OrgJsonValueDeserializer

public abstract class ValueDeserializerAdapter<InputType,InputNodeType>
extends java.lang.Object
implements ValueDeserializer

Adapter for pull-parsing and tree-parsing capable ValueDeserializers.

Among Plain values (see ValueDeserializer) some are considered primitives to underlying serialization mechanisms and by so handed/come without conversion to/from implementations. Primitive values can be one of:


Constructor Summary
protected ValueDeserializerAdapter(Application application, Module module, Function<Application,Module> valuesModuleFinder)
           
  ValueDeserializerAdapter(Application application, Module module, ServiceReference<ValueDeserializer> serviceRef)
           
 
Method Summary
protected abstract  InputType adaptInput(java.io.InputStream input)
          This method is always called first, this is a chance to wrap the input type.
protected abstract  java.lang.Object asSimpleValue(InputNodeType inputNode)
           
<T> Function2<ValueType,java.lang.String,T>
deserialize()
          Factory method for an untyped deserialize function.
<T> T
deserialize(java.lang.Class<?> type, java.io.InputStream input)
          Deserialize a value from a state.
<T> T
deserialize(java.lang.Class<?> type, java.lang.String input)
          Deserialize a value from a state.
<T> Function<java.lang.String,T>
deserialize(java.lang.Class<T> type)
          Factory method for a typed deserialize function.
<T> Function<java.lang.String,T>
deserialize(ValueType valueType)
          Factory method for a typed deserialize function.
<T> T
deserialize(ValueType valueType, java.io.InputStream input)
          Deserialize a value from a state.
<T> T
deserialize(ValueType valueType, java.lang.String input)
          Deserialize a value from a state.
protected abstract
<T> T
getObjectFieldValue(InputNodeType inputNode, java.lang.String key, Function<InputNodeType,T> valueDeserializer)
          Return null if the field do not exists.
protected abstract  boolean isObjectValue(InputNodeType inputNode)
           
protected abstract  boolean objectHasField(InputNodeType inputNode, java.lang.String key)
           
protected  void onDeserializationEnd(ValueType valueType, InputType input)
          Called by the adapter on deserialization end.
protected  void onDeserializationStart(ValueType valueType, InputType input)
          Called by the adapter on deserialization start, after adaptInput(java.io.InputStream).
protected abstract
<T> void
putArrayNodeInCollection(InputNodeType inputNode, Function<InputNodeType,T> deserializer, java.util.Collection<T> collection)
           
protected abstract
<K,V> void
putArrayNodeInMap(InputNodeType inputNode, Function<InputNodeType,K> keyDeserializer, Function<InputNodeType,V> valueDeserializer, java.util.Map<K,V> map)
           
protected abstract
<T> java.util.Collection<T>
readArrayInCollection(InputType input, Function<InputType,T> deserializer, java.util.Collection<T> collection)
           
protected abstract
<K,V> java.util.Map<K,V>
readMapInMap(InputType input, Function<InputType,K> keyDeserializer, Function<InputType,V> valueDeserializer, java.util.Map<K,V> map)
          A Map<K,V> is serialized in an array of entries objects.
protected abstract  InputNodeType readObjectTree(InputType input)
           
protected abstract  java.lang.Object readPlainValue(InputType input)
           
protected
<T> void
registerDeserializer(java.lang.Class<T> type, Function<java.lang.Object,T> deserializer)
          Register a Plain Value type deserialization Function.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ValueDeserializerAdapter

public ValueDeserializerAdapter(@Structure
                                Application application,
                                @Structure
                                Module module,
                                @Service
                                ServiceReference<ValueDeserializer> serviceRef)

ValueDeserializerAdapter

protected ValueDeserializerAdapter(Application application,
                                   Module module,
                                   Function<Application,Module> valuesModuleFinder)
Method Detail

registerDeserializer

protected final <T> void registerDeserializer(java.lang.Class<T> type,
                                              Function<java.lang.Object,T> deserializer)
Register a Plain Value type deserialization Function.

Type Parameters:
T - Plain Value parametrized Type
Parameters:
type - Plain Value Type
deserializer - Deserialization Function

deserialize

public <T> Function<java.lang.String,T> deserialize(java.lang.Class<T> type)
Description copied from interface: ValueDeserializer
Factory method for a typed deserialize function.

The returned Function may throw ValueSerializationException.

Specified by:
deserialize in interface ValueDeserializer
Type Parameters:
T - the parametrized function return type
Parameters:
type - the value type
Returns:
a deserialization function

deserialize

public final <T> Function<java.lang.String,T> deserialize(ValueType valueType)
Description copied from interface: ValueDeserializer
Factory method for a typed deserialize function.

The returned Function may throw ValueSerializationException.

Specified by:
deserialize in interface ValueDeserializer
Type Parameters:
T - the parametrized function return type
Parameters:
valueType - the value type
Returns:
a deserialization function

deserialize

public final <T> Function2<ValueType,java.lang.String,T> deserialize()
Description copied from interface: ValueDeserializer
Factory method for an untyped deserialize function.

The returned Function may throw ValueSerializationException.

Specified by:
deserialize in interface ValueDeserializer
Type Parameters:
T - the parametrized function return type
Returns:
a deserialization function

deserialize

public final <T> T deserialize(java.lang.Class<?> type,
                               java.lang.String input)
                    throws ValueSerializationException
Description copied from interface: ValueDeserializer
Deserialize a value from a state.

Specified by:
deserialize in interface ValueDeserializer
Type Parameters:
T - the parametrized returned type
Parameters:
type - the value type
input - the state
Returns:
the value
Throws:
ValueSerializationException - if the deserialization failed

deserialize

public final <T> T deserialize(ValueType valueType,
                               java.lang.String input)
                    throws ValueSerializationException
Description copied from interface: ValueDeserializer
Deserialize a value from a state.

Specified by:
deserialize in interface ValueDeserializer
Type Parameters:
T - the parametrized returned type
Parameters:
valueType - the value type
input - the state
Returns:
the value
Throws:
ValueSerializationException - if the deserialization failed

deserialize

public final <T> T deserialize(java.lang.Class<?> type,
                               java.io.InputStream input)
                    throws ValueSerializationException
Description copied from interface: ValueDeserializer
Deserialize a value from a state.

Specified by:
deserialize in interface ValueDeserializer
Type Parameters:
T - the parametrized returned type
Parameters:
type - the value type
input - the state stream
Returns:
the value
Throws:
ValueSerializationException - if the deserialization failed

deserialize

public final <T> T deserialize(ValueType valueType,
                               java.io.InputStream input)
                    throws ValueSerializationException
Description copied from interface: ValueDeserializer
Deserialize a value from a state.

Specified by:
deserialize in interface ValueDeserializer
Type Parameters:
T - the parametrized returned type
Parameters:
valueType - the value type
input - the state stream
Returns:
the value
Throws:
ValueSerializationException - if the deserialization failed

onDeserializationStart

protected void onDeserializationStart(ValueType valueType,
                                      InputType input)
                               throws java.lang.Exception
Called by the adapter on deserialization start, after adaptInput(java.io.InputStream).

Throws:
java.lang.Exception - that will be wrapped in a ValueSerializationException

onDeserializationEnd

protected void onDeserializationEnd(ValueType valueType,
                                    InputType input)
                             throws java.lang.Exception
Called by the adapter on deserialization end.

Throws:
java.lang.Exception - that will be wrapped in a ValueSerializationException

adaptInput

protected abstract InputType adaptInput(java.io.InputStream input)
                                 throws java.lang.Exception
This method is always called first, this is a chance to wrap the input type.

Parameters:
input - InputStream to adapt
Returns:
Adapted input
Throws:
java.lang.Exception - that will be wrapped in a ValueSerializationException

readPlainValue

protected abstract java.lang.Object readPlainValue(InputType input)
                                            throws java.lang.Exception
Returns:
a Plain Value read from the input
Throws:
java.lang.Exception - that will be wrapped in a ValueSerializationException

readArrayInCollection

protected abstract <T> java.util.Collection<T> readArrayInCollection(InputType input,
                                                                     Function<InputType,T> deserializer,
                                                                     java.util.Collection<T> collection)
                                                          throws java.lang.Exception
Returns:
The filled collection or null if no array
Throws:
java.lang.Exception - that will be wrapped in a ValueSerializationException

readMapInMap

protected abstract <K,V> java.util.Map<K,V> readMapInMap(InputType input,
                                                         Function<InputType,K> keyDeserializer,
                                                         Function<InputType,V> valueDeserializer,
                                                         java.util.Map<K,V> map)
                                            throws java.lang.Exception
A Map<K,V> is serialized in an array of entries objects.

Here is an example in JSON:

 [
     { "key": "foo",       "value": "bar"   },
     { "key": "cathedral", "value": "bazar" }
 ]
 

And an empty Map:

[]

This allow to use any type as keys and values while keeping the Map order at the cost of having non-predictible order of key/value inside an entry object.

Returns:
The filled map or null if no array
Throws:
java.lang.Exception - that will be wrapped in a ValueSerializationException

readObjectTree

protected abstract InputNodeType readObjectTree(InputType input)
                                         throws java.lang.Exception
Returns:
an InputNodeType or null if the value was null
Throws:
java.lang.Exception - that will be wrapped in a ValueSerializationException

asSimpleValue

protected abstract java.lang.Object asSimpleValue(InputNodeType inputNode)
                                           throws java.lang.Exception
Throws:
java.lang.Exception

isObjectValue

protected abstract boolean isObjectValue(InputNodeType inputNode)
                                  throws java.lang.Exception
Throws:
java.lang.Exception

objectHasField

protected abstract boolean objectHasField(InputNodeType inputNode,
                                          java.lang.String key)
                                   throws java.lang.Exception
Throws:
java.lang.Exception

getObjectFieldValue

protected abstract <T> T getObjectFieldValue(InputNodeType inputNode,
                                             java.lang.String key,
                                             Function<InputNodeType,T> valueDeserializer)
                                  throws java.lang.Exception
Return null if the field do not exists.

Throws:
java.lang.Exception - that will be wrapped in a ValueSerializationException

putArrayNodeInCollection

protected abstract <T> void putArrayNodeInCollection(InputNodeType inputNode,
                                                     Function<InputNodeType,T> deserializer,
                                                     java.util.Collection<T> collection)
                                          throws java.lang.Exception
Throws:
java.lang.Exception

putArrayNodeInMap

protected abstract <K,V> void putArrayNodeInMap(InputNodeType inputNode,
                                                Function<InputNodeType,K> keyDeserializer,
                                                Function<InputNodeType,V> valueDeserializer,
                                                java.util.Map<K,V> map)
                                   throws java.lang.Exception
Throws:
java.lang.Exception