View Javadoc
1   /*
2    * Copyright 2012 Olivier Godineau
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 http://www.apache.org/licenses/LICENSE-2.0
7    * 
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11   * License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package olg.csv.bean.impl;
15  
16  import olg.csv.base.Row;
17  import olg.csv.bean.getter.AbstractGetter;
18  import olg.csv.bean.parser.AbstractParser;
19  
20  /**
21   * 
22   * Basic property transformer. Allows to set a property from a string.
23   * 
24   * @param <B>
25   *            dedicated bean the PropertyProcessor deals with.
26   * 
27   */
28  public final class PropertyProcessor<B> extends AbstractPropertyProcessor<B> {
29  	/**
30  	 * the getter. Allows to extract the property as string from a list of
31  	 * Strings.
32  	 */
33  	private final AbstractGetter getter;
34  
35  	/**
36  	 * The parser to product the property to set from a String.
37  	 */
38  	@SuppressWarnings("rawtypes")
39  	private final AbstractParser parser;
40  
41  	@Override
42  	public B transform(Row line, B bean) throws olg.csv.bean.parser.ParseException {
43  		Object object = parser.parse(getter.get(line));
44  
45  		return invoke(object, bean);
46  
47  	}
48  
49  	/**
50  	 * 
51  	 * @param getter
52  	 *            the getter that extracts the string value to parse as a
53  	 *            property. . Must be not <code>null</code>
54  	 * @param parser
55  	 *            the parser that parses the string as a property value. . Must
56  	 *            be not <code>null</code>
57  	 * @param propertyName
58  	 *            the name of the bean property.
59  	 * @param clazz
60  	 *            the bean class.
61  	 */
62  	PropertyProcessor(AbstractGetter getter, @SuppressWarnings("rawtypes") AbstractParser parser, String propertyName,
63  			Class<B> clazz) {
64  		super();
65  		this.getter = getter;
66  		this.parser = parser;
67  		this.beanClass = clazz;
68  		this.method = olg.csv.bean.Util.identifySetter(clazz, propertyName);
69  
70  		if (method == null) {
71  			throw new IllegalArgumentException("No such setter for " + beanClass.getName() + "." + propertyName
72  					+ " field");
73  		}
74  
75  	}
76  
77  }