1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.morph.transform.copiers;
17
18 import java.util.Iterator;
19 import java.util.Locale;
20
21 import net.sf.morph.transform.DecoratedConverter;
22 import net.sf.morph.transform.DecoratedCopier;
23 import net.sf.morph.transform.Transformer;
24 import net.sf.morph.util.TransformerUtils;
25
26 /**
27 * A copier that copies multiple source objects to a single destination object, implementing an "Assembler."
28 *
29 * @see http://www.martinfowler.com/eaaCatalog/dataTransferObject.html
30 *
31 * @author Matt Benson
32 * @since Morph 1.1
33 */
34 public class AssemblerCopier extends AssemblyCopierSupport implements DecoratedCopier,
35 DecoratedConverter {
36
37 /**
38 * Create a new AssemblerCopier.
39 */
40 public AssemblerCopier() {
41 super();
42 }
43
44 /**
45 * Create a new AssemblerCopier.
46 * @param components
47 */
48 public AssemblerCopier(Object[] components) {
49 super(components);
50 }
51
52 /**
53 * {@inheritDoc}
54 * @see net.sf.morph.transform.transformers.BaseTransformer#copyImpl(java.lang.Object, java.lang.Object, java.util.Locale, java.lang.Integer)
55 */
56 protected void copyImpl(Object destination, Object source, Locale locale,
57 Integer preferredTransformationType) throws Exception {
58
59 int index = 0;
60 for (Iterator iter = getContainerReflector().getIterator(source); iter.hasNext(); index++) {
61 getCopier(index).copy(destination, iter.next(), locale);
62 }
63 }
64
65 /**
66 * {@inheritDoc}
67 * @see net.sf.morph.transform.transformers.BaseTransformer#getSourceClassesImpl()
68 */
69 protected Class[] getSourceClassesImpl() throws Exception {
70 return getContainerReflector().getReflectableClasses();
71 }
72
73 /**
74 * {@inheritDoc}
75 * @see net.sf.morph.transform.transformers.BaseTransformer#getDestinationClassesImpl()
76 */
77 protected synchronized Class[] getDestinationClassesImpl() throws Exception {
78 Object[] components = getComponents();
79 if (components == null) {
80 return getNestedTransformer().getDestinationClasses();
81 }
82 if (components.length == 0) {
83 return new Class[0];
84 }
85 return TransformerUtils
86 .getDestinationClassIntersection((Transformer[]) getComponents());
87 }
88 }