KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > binding > convert > ConversionExecutor


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.binding.convert;
17
18 import java.io.Serializable JavaDoc;
19
20 import org.springframework.core.style.ToStringCreator;
21 import org.springframework.util.Assert;
22
23 /**
24  * A command object that is parameterized with the information necessary to
25  * perform a conversion of a source input to a target output.
26  * <p>
27  * Specifically, encapsulates knowledge about how to convert source objects to a
28  * specific target type using a specific converter.
29  * @author Keith Donald
30  */

31 public class ConversionExecutor implements Serializable JavaDoc {
32
33     /**
34      * The source value type this executor will attempt to convert from.
35      */

36     private final Class JavaDoc sourceClass;
37
38     /**
39      * The target value type this executor will attempt to convert to.
40      */

41     private final Class JavaDoc targetClass;
42
43     /**
44      * The converter that will perform the conversion.
45      */

46     private final Converter converter;
47
48     /**
49      * Creates a conversion executor.
50      * @param converter The converter that will perform the conversion.
51      * @param targetClass The target type that the converter will convert to.
52      */

53     public ConversionExecutor(Class JavaDoc sourceClass, Class JavaDoc targetClass, Converter converter) {
54         Assert.notNull(sourceClass, "The source class is required");
55         Assert.notNull(targetClass, "The target class is required");
56         Assert.notNull(converter, "The converter is required");
57         this.sourceClass = sourceClass;
58         this.targetClass = targetClass;
59         this.converter = converter;
60     }
61
62     /**
63      * Returns the source class of conversions performed by this executor.
64      * @return the source class.
65      */

66     public Class JavaDoc getSourceClass() {
67         return sourceClass;
68     }
69
70     /**
71      * Returns the target class of conversions performed by this executor.
72      * @return the target class.
73      */

74     public Class JavaDoc getTargetClass() {
75         return targetClass;
76     }
77
78     /**
79      * Execute the conversion for the provided source object.
80      * @param source the source object to convert
81      */

82     public Object JavaDoc execute(Object JavaDoc source) throws ConversionException {
83         return execute(source, null);
84     }
85
86     /**
87      * Execute the conversion for the provided source object.
88      * @param source the source object to convert
89      * @param context the conversion context, useful for influencing the
90      * behavior of the converter.
91      */

92     public Object JavaDoc execute(Object JavaDoc source, ConversionContext context) throws ConversionException {
93         if (source != null) {
94             Assert.isInstanceOf(sourceClass, source, "Not of source type: ");
95         }
96         return converter.convert(source, targetClass, context);
97     }
98
99     public boolean equals(Object JavaDoc o) {
100         if (!(o instanceof ConversionExecutor)) {
101             return false;
102         }
103         ConversionExecutor other = (ConversionExecutor)o;
104         return sourceClass.equals(other.sourceClass) && targetClass.equals(other.targetClass);
105     }
106
107     public int hashCode() {
108         return sourceClass.hashCode() + targetClass.hashCode();
109     }
110
111     public String JavaDoc toString() {
112         return new ToStringCreator(this).append("sourceClass", sourceClass).append("targetClass", targetClass)
113                 .toString();
114     }
115 }
Popular Tags