View Javadoc

1   /*
2    * Copyright 2007-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
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  }