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.lang.reflect.Array;
19  import java.util.Locale;
20  import java.util.Set;
21  
22  import net.sf.morph.transform.DecoratedConverter;
23  import net.sf.morph.transform.DecoratedCopier;
24  import net.sf.morph.transform.ExplicitTransformer;
25  import net.sf.morph.transform.transformers.BaseTransformer;
26  import net.sf.morph.util.ClassUtils;
27  import net.sf.morph.util.ContainerUtils;
28  import net.sf.morph.util.TransformerUtils;
29  
30  /**
31   * Copies arrays of matching immutable component types using System.arraycopy.
32   * @since Morph 1.1
33   * @author mbenson
34   */
35  public class ImmutableComponentArrayCopier extends BaseTransformer implements
36  		DecoratedConverter, DecoratedCopier, ExplicitTransformer {
37  	private static final Class[] SOURCE_AND_DEST_TYPES;
38  
39  	static {
40  		Class[] componentTypes = ClassUtils.getImmutableTypes();
41  		Set arrayTypes = ContainerUtils.createOrderedSet();
42  		for (int i = 0; i < componentTypes.length; i++) {
43  			if (componentTypes[i] != null) {
44  				arrayTypes.add(ClassUtils.getArrayClass(componentTypes[i]));
45  			}
46  		}
47  		SOURCE_AND_DEST_TYPES = (Class[]) arrayTypes
48  				.toArray(new Class[arrayTypes.size()]);
49  	}
50  
51  	/**
52  	 * {@inheritDoc}
53  	 * @see net.sf.morph.transform.transformers.BaseTransformer#getDestinationClassesImpl()
54  	 */
55  	protected Class[] getDestinationClassesImpl() throws Exception {
56  		return SOURCE_AND_DEST_TYPES;
57  	}
58  
59  	/**
60  	 * {@inheritDoc}
61  	 * @see net.sf.morph.transform.transformers.BaseTransformer#getSourceClassesImpl()
62  	 */
63  	protected Class[] getSourceClassesImpl() throws Exception {
64  		return SOURCE_AND_DEST_TYPES;
65  	}
66  
67  	/**
68  	 * {@inheritDoc}
69  	 * @see net.sf.morph.transform.transformers.BaseTransformer#isTransformableImpl(java.lang.Class, java.lang.Class)
70  	 */
71  	protected boolean isTransformableImpl(Class destinationType, Class sourceType)
72  			throws Exception {
73  		return TransformerUtils.isImplicitlyTransformable(this, destinationType,
74  				sourceType)
75  				&& sourceType.getComponentType() == destinationType.getComponentType();
76  	}
77  
78  	/**
79  	 * {@inheritDoc}
80  	 * @see net.sf.morph.transform.transformers.BaseTransformer#convertImpl(java.lang.Class, java.lang.Object, java.util.Locale)
81  	 */
82  	protected Object convertImpl(Class destinationClass, Object source, Locale locale) throws Exception {
83  		Object array = ClassUtils.createArray(destinationClass.getComponentType(), Array.getLength(source));
84  		copyImpl(array, source, locale, TRANSFORMATION_TYPE_CONVERT);
85  		return array;
86  	}
87  
88  	/**
89  	 * {@inheritDoc}
90  	 * @see net.sf.morph.transform.transformers.BaseTransformer#copyImpl(java.lang.Object, java.lang.Object, java.util.Locale, java.lang.Integer)
91  	 */
92  	protected void copyImpl(Object destination, Object source, Locale locale,
93  			Integer preferredTransformationType) throws Exception {
94  		System.arraycopy(source, 0, destination, 0, Array.getLength(source));
95  	}
96  
97  	/**
98  	 * {@inheritDoc}
99  	 * @see net.sf.morph.transform.transformers.BaseTransformer#isPerformingLogging()
100 	 */
101 	protected boolean isPerformingLogging() {
102 		return false;
103 	}
104 }