View Javadoc

1   /*
2    * Copyright 2004-2005, 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.ArrayList;
19  import java.util.List;
20  import java.util.Locale;
21  
22  import net.sf.morph.Defaults;
23  import net.sf.morph.transform.Converter;
24  import net.sf.morph.transform.DecoratedConverter;
25  import net.sf.morph.transform.Transformer;
26  import net.sf.morph.transform.transformers.BaseCompositeTransformer;
27  import net.sf.morph.util.TransformerUtils;
28  
29  /**
30   * A converter which takes a single object and converts it into multiple objects.
31   *
32   * @author Matt Sgarlata
33   * @since Apr 18, 2005
34   * @deprecated since v1.1 in favor of {@link DisassemblerCopier}
35   */
36  public class MultipleDestinationConverter extends BaseCompositeTransformer implements
37  		DecoratedConverter {
38  
39  	private Converter containerConverter;
40  	private Class[] destinationClassesForEachDestination;
41  
42  	/**
43  	 * {@inheritDoc}
44  	 * @see net.sf.morph.transform.transformers.BaseTransformer#convertImpl(java.lang.Class, java.lang.Object, java.util.Locale)
45  	 */
46  	protected Object convertImpl(Class destinationClass, Object source, Locale locale)
47  			throws Exception {
48  		Converter[] converters = (Converter[]) getComponents();
49  		Class[] destTypes = getDestinationClassesForEachDestination();
50  		List destinationObjects = new ArrayList(converters.length);
51  		for (int i = 0; i < components.length; i++) {
52  			Object converted = converters[i].convert(destTypes[i], source, locale);
53  			destinationObjects.add(converted);
54  		}
55  		return getContainerConverter().convert(destinationClass, destinationObjects, locale);
56  	}
57  
58  	/**
59  	 * {@inheritDoc}
60  	 * @see net.sf.morph.transform.transformers.BaseCompositeTransformer#getComponentType()
61  	 */
62  	public Class getComponentType() {
63  		return Converter.class;
64  	}
65  
66  	/**
67  	 * {@inheritDoc}
68  	 * @see net.sf.morph.transform.transformers.BaseTransformer#setSourceClasses(java.lang.Class[])
69  	 */
70  	public synchronized void setSourceClasses(Class[] sourceClasses) {
71  		super.setSourceClasses(sourceClasses);
72  	}
73  
74  	/**
75  	 * {@inheritDoc}
76  	 * @see net.sf.morph.transform.transformers.BaseTransformer#getSourceClassesImpl()
77  	 */
78  	protected Class[] getSourceClassesImpl() throws Exception {
79  		return TransformerUtils
80  				.getSourceClassIntersection((Transformer[]) getComponents());
81  	}
82  
83  	/**
84  	 * {@inheritDoc}
85  	 * @see net.sf.morph.transform.transformers.BaseTransformer#setDestinationClasses(java.lang.Class[])
86  	 */
87  	public synchronized void setDestinationClasses(Class[] destinationClasses) {
88  		super.setDestinationClasses(destinationClasses);
89  	}
90  
91  	/**
92  	 * {@inheritDoc}
93  	 * @see net.sf.morph.transform.transformers.BaseTransformer#getDestinationClassesImpl()
94  	 */
95  	protected Class[] getDestinationClassesImpl() throws Exception {
96  		return getContainerConverter().getDestinationClasses();
97  	}
98  
99  	/**
100 	 * Get the container converter that will be used by this converter.
101 	 * @return Converter
102 	 */
103 	public Converter getContainerConverter() {
104 		if (containerConverter == null) {
105 			containerConverter = Defaults.createContainerCopier();
106 		}
107 		return containerConverter;
108 	}
109 
110 	/**
111 	 * Set the container converter that will be used by this converter.
112 	 * @param containerTransformer Converter
113 	 */
114 	public void setContainerConverter(Converter containerTransformer) {
115 		this.containerConverter = containerTransformer;
116 	}
117 
118 	/**
119 	 * Get the ordered array of destination classes.
120 	 * @return Class[]
121 	 */
122 	public Class[] getDestinationClassesForEachDestination() {
123 		return destinationClassesForEachDestination;
124 	}
125 
126 	/**
127 	 * Set an ordered array of destination classes.
128 	 * @param destinations
129 	 */
130 	public void setDestinationClassesForEachDestination(Class[] destinations) {
131 		this.destinationClassesForEachDestination = destinations;
132 	}
133 
134 	/**
135 	 * {@inheritDoc}
136 	 * @see net.sf.morph.transform.transformers.BaseCompositeTransformer#isWrappingRuntimeExceptions()
137 	 */
138 	protected boolean isWrappingRuntimeExceptions() {
139 		return false;
140 	}
141 
142 }