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.IBeanProcessor;
18  
19  /**
20   * 
21   * Complex property transformer. Typically the case when we need to instanciate
22   * a bean from a list of Strings we want to set as a property of our final bean.
23   * 
24   * @param <B>
25   *            bean this property Processor deals with.
26   * 
27   */
28  public final class ComplexPropertyProcessor<B> extends AbstractPropertyProcessor<B> {
29  	/**
30  	 * the transformer used to product a bean from a list of Strings
31  	 */
32  	@SuppressWarnings("rawtypes")
33  	private final IBeanProcessor transformer;
34  
35  	/**
36  	 * 
37  	 * @param transformer
38  	 *            the transformer which products the value of the property to
39  	 *            set
40  	 * @param beanClass
41  	 *            the class of the bean to return. Must be not <code>null</code>
42  	 * @param propertyName
43  	 *            the name of the property to set. Must be not <code>null</code>
44  	 */
45  
46  	ComplexPropertyProcessor(@SuppressWarnings("rawtypes") IBeanProcessor transformer, Class<B> beanClass,
47  			String propertyName) {
48  		super();
49  		this.transformer = transformer;
50  		this.beanClass = beanClass;
51  		this.method = olg.csv.bean.Util.identifySetter(beanClass, propertyName);
52  		if (method == null) {
53  			throw new IllegalArgumentException("No such setter for " + beanClass.getName() + "." + propertyName
54  					+ " field");
55  		}
56  	}
57  
58  	public B transform(Row line, B bean) throws olg.csv.bean.parser.ParseException {
59  
60  		Object field = transformer.transform(line);
61  		return invoke(field, bean);
62  
63  	}
64  
65  }