public abstract class ChainedFactory extends Object implements Factory<Converter<?>>
public class NullConverter extends Wrapper<Converter<Object>> implements Converter<Object> { private final Converter<Object> converter; public NullConverter(Converter<Object> converter) { super(converter); this.converter = converter; } public void serialize(Object obj, ObjectWriter writer, Context ctx) { if (obj == null) writer.writeNull(); else converter.serialize(obj, writer, ctx); } public Object deserialize(ObjectReader reader, Context ctx) { if (TypeValue.NULL == reader.getTypeValue()) { return null; } else return converter.deserialize(reader, ctx); } } // now we need a factory to create the nullconverter for type T and wire it with the existing // factories so we can get an instance of the converter for that type. public class NullConverterFactory extends ChainedFactory { public NullConverterFactory(Factory<Converter<?>> next) { super(next); } public Converter<?> create(Type type, Genson genson, Converter<?> nextConverter) { return new NullConverter(nextConverter); } }As you can see it is pretty simple but also powerful. Note that our NullConverter extends Wrapper class. When you encapsulate converters you should extend Wrapper class this way Genson can access the class information of wrapped converters. Imagine for example that you put some annotation on converter A and wrap it in converter B, now if you wrap B in C you wont be able to get class information of A (ex: its annotations). Wrapper class allows to merge class information of current implementation and the wrapped one.
NullConverter
,
BasicConvertersFactory
,
Wrapper
Modifier | Constructor and Description |
---|---|
protected |
ChainedFactory() |
protected |
ChainedFactory(Factory<Converter<?>> next) |
Modifier and Type | Method and Description |
---|---|
<T extends Factory<? extends Converter<?>>> |
append(T next) |
Converter<?> |
create(Type type,
Genson genson)
Implementations of this method must try to create an instance of type T based on the
parameter "type".
|
protected abstract Converter<?> |
create(Type type,
Genson genson,
Converter<?> nextConverter)
This method will be called by
create(Type, Genson) with nextConverter being the
converter created for current type by the next factory. |
Factory<? extends Converter<?>> |
next() |
<T extends Factory<? extends Converter<?>>> |
withNext(T next)
Chains this factory with next and returns next (the tail) so you can do things like
chain1.withNext(new chain2).withNext(new chain3); the resulting chain is
chain1=>chain2=>chain3.
|
public Converter<?> create(Type type, Genson genson)
Factory
protected abstract Converter<?> create(Type type, Genson genson, Converter<?> nextConverter)
create(Type, Genson)
with nextConverter being the
converter created for current type by the next factory. This means that ChainedFactory will
first create a converter with the next factory and then use it's own create method.type
- for which this factory must provide a convertergenson
- instance that you can use when you need a converter for some other type (for
example a converter of List<Integer> will need a converter for Integer type).nextConverter
- created by the next factory, may be null.public final <T extends Factory<? extends Converter<?>>> T withNext(T next)
next
- factoryCopyright © 2019. All rights reserved.