View Javadoc

1   /*
2    * Copyright 2004-2005 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;
17  
18  import java.util.Locale;
19  
20  /**
21   * <p>
22   * An object that can convert one type of object into another type of object.
23   * Usually, the sourceClasses and destinationClasses attributes determine the
24   * set of sources and destinations which the converter can convert.  However,
25   * if any of the sourceClasses is not convertible to a destinationClass, then
26   * a converter may implement the {@link net.sf.morph.transform.ExplicitTransformer}
27   * to explicitly define which sources are convertible to which destinations.
28   * </p>
29   *  
30   * <p>
31   * Note that the Converter interface can be easily implemented by a Copier (see
32   * {@link net.sf.morph.transform.converters.CopierConverter}), so it is
33   * recommended that Converters only be implemented for basic data types
34   * (numbers, primitives) or immutable data types (e.g. Strings, but note Dates).
35   * </p>
36   * 
37   * @author Matt Sgarlata
38   * @since October 24, 2004
39   * @see net.sf.morph.transform.ExplicitTransformer
40   */
41  public interface Converter extends Transformer {
42  	
43  	/**
44  	 * Converts the given <code>source</code> into an object of class
45  	 * <code>destinationClass</code>. The returned object may be a reference
46  	 * to <code>source</code> itself. This isn't an issue for immutable
47  	 * classes (String, Long, etc) but is an issue for Collection and Array
48  	 * types.
49  	 * 
50  	 * @param destinationClass
51  	 *            the destination class to test
52  	 * @param source
53  	 *            the source object to test
54  	 * @param locale
55  	 *            the locale in which the conversion should take place, or
56  	 *            <code>null</code> if the locale is not applicable
57  	 * @return the result of the conversion
58  	 * @throws TransformationException
59  	 *             if <code>destinationClass</code> is <code>null</code>,
60  	 *             an error occurred while performing the conversion
61  	 */
62  	public Object convert(Class destinationClass, Object source, Locale locale)
63  		throws TransformationException;
64  }