Skip to content

Translation Target Factories

Geoffrey Wiseman edited this page May 21, 2014 · 1 revision

Instances of TranslationTargetFactory can be used to construct a 'target' instance before the properties are translated into it. This can be useful when translating from one class hierarchy to another, or when the destination class is an interface and you want a choice about the implementation.

Moo provides several of these instances which are used by default in specific cases.

DefaultObjectTargetFactory

When translating object properties (annotated with @Property or implicitly defined), the DefaultObjectTargetFactory is responsible for constructing the target object, invoking any NoArgument constructor of the destination type. This is also the class that constructs target objects for collection items, map keys and map targets.

DefaultCollectionTargetFactory

When translating collection properties (annotated with @CollectionProperty or implicitly defined), the DefaultCollectionTargetFactory is responsible for constructing the target collection. It does this by constructing a preferred collection implementation for each core collection interface:

  • SortedSet => TreeSet
  • Set => HashSet
  • List => ArrayList
  • Collection => ArrayList

DefaultMapTargetFactory

When translating map properties (annotated with @MapProperty or implicitly defined), the DefaultMapTargetFactory is responsible for constructing the target map. It does this by constructing a preferred collection implementation for the two core Map interfaces:

  • SortedMap => TreeMap
  • Map => HashMap

Custom Translation Target Factories

When these defaults aren't sufficient, you can take control over the construction of the target type by overriding the default translation target factory:

@Property(factory=MyOwnClassHierarchyFactory.class)
@CollectionProperty(factory=IPreferLinkedList.class)
@MapProperty(factory=MyCustomMapFactory.class)

Implementations of TranslationTargetFactory are pretty simple, and you can look at the core implementations for inspiration. I could also make these core implementations customizable with configuration but that doesn't seem necessary yet.

Clone this wiki locally