KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > binding > mapping > MappingBuilder


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.mapping;
17
18 import org.springframework.binding.convert.ConversionExecutor;
19 import org.springframework.binding.convert.ConversionService;
20 import org.springframework.binding.convert.support.DefaultConversionService;
21 import org.springframework.binding.expression.Expression;
22 import org.springframework.binding.expression.ExpressionParser;
23 import org.springframework.binding.expression.SettableExpression;
24 import org.springframework.util.Assert;
25
26 /**
27  * A stateful builder that builds {@link Mapping} objects. Designed for
28  * convenience to build mappings in a clear, readable manner.
29  * <p>
30  * Example usage:
31  *
32  * <pre>
33  * MappingBuilder mapping = new MappingBuilder();
34  * Mapping result = mapping.source(&quot;foo&quot;).target(&quot;bar&quot;).from(String.class).to(Long.class).value();
35  * </pre>
36  *
37  * Calling the {@link #value()} result method clears out this builder's state so
38  * it can be reused to build another mapping.
39  * @author Keith Donald
40  */

41 public class MappingBuilder {
42
43     /**
44      * The source mapping expression.
45      */

46     private Expression sourceExpression;
47
48     /**
49      * The target mapping property expression.
50      */

51     private SettableExpression targetExpression;
52
53     /**
54      * The type of the object returned by evaluating the source expression.
55      */

56     private Class JavaDoc sourceType;
57
58     /**
59      * The type of the property settable by the target expression.
60      */

61     private Class JavaDoc targetType;
62
63     /**
64      * The expression string parser.
65      */

66     private ExpressionParser expressionParser;
67
68     /**
69      * The conversion service for applying type conversions.
70      */

71     private ConversionService conversionService = new DefaultConversionService();
72
73     /**
74      * Whether or not the built mapping is a required mapping.
75      */

76     private boolean required;
77     
78     /**
79      * Creates a mapping builder that uses the expression parser to parse
80      * attribute mapping expressions.
81      * @param expressionParser the expression parser.
82      */

83     public MappingBuilder(ExpressionParser expressionParser) {
84         Assert.notNull(expressionParser, "The expression parser is required");
85         this.expressionParser = expressionParser;
86     }
87
88     /**
89      * Sets the conversion service that will convert the object returned by
90      * evaluating the source expression to the {@link #to(Class)} type if
91      * necessary.
92      * @param conversionService the conversion service
93      */

94     public void setConversionService(ConversionService conversionService) {
95         this.conversionService = conversionService;
96     }
97
98     /**
99      * Sets the source expression of the mapping built by this builder.
100      * @param expressionString the expression string
101      * @return this, to support call-chaining
102      */

103     public MappingBuilder source(String JavaDoc expressionString) {
104         sourceExpression = expressionParser.parseExpression(expressionString);
105         return this;
106     }
107
108     /**
109      * Sets the target property expression of the mapping built by this builder.
110      * @param expressionString the expression string
111      * @return this, to support call-chaining
112      */

113     public MappingBuilder target(String JavaDoc expressionString) {
114         targetExpression = (SettableExpression)expressionParser.parseExpression(expressionString);
115         return this;
116     }
117
118     /**
119      * Sets the expected type of the object returned by evaluating the source
120      * expression. Used in conjunction with {@link #to(Class)} to perform a type
121      * conversion during the mapping process.
122      * @param sourceType the source type
123      * @return this, to support call-chaining
124      */

125     public MappingBuilder from(Class JavaDoc sourceType) {
126         this.sourceType = sourceType;
127         return this;
128     }
129
130     /**
131      * Sets the target type of the property writeable by the target expression.
132      * @param targetType the target type
133      * @return this, to support call-chaining
134      */

135     public MappingBuilder to(Class JavaDoc targetType) {
136         this.targetType = targetType;
137         return this;
138     }
139
140     /**
141      * Marks the mapping to be built a "required" mapping.
142      * @return this, to support call-chaining
143      */

144     public MappingBuilder required() {
145         this.required = true;
146         return this;
147     }
148     
149     /**
150      * The logical GOF builder getResult method, returning a fully constructed
151      * Mapping from the configured pieces. Once called, the state of this
152      * builder is nulled out to support building a new mapping object again.
153      * @return the mapping result
154      */

155     public Mapping value() {
156         Assert.notNull(sourceExpression, "The source expression must be set at a minimum");
157         if (targetExpression == null) {
158             targetExpression = (SettableExpression)sourceExpression;
159         }
160         ConversionExecutor typeConverter = null;
161         if (sourceType != null) {
162             Assert.notNull(targetType, "The target type is required when the source type is specified");
163             typeConverter = conversionService.getConversionExecutor(sourceType, targetType);
164         }
165         Mapping result;
166         if (required) {
167             result = new RequiredMapping(sourceExpression, targetExpression, typeConverter);
168         } else {
169             result = new Mapping(sourceExpression, targetExpression, typeConverter);
170         }
171         sourceExpression = null;
172         targetExpression = null;
173         sourceType = null;
174         targetType = null;
175         required = false;
176         return result;
177     }
178 }
Popular Tags