View Javadoc

1   package net.sf.chainedoptions;
2   
3   import java.util.ArrayList;
4   import java.util.Iterator;
5   import java.util.List;
6   
7   /***
8    * Abstract class that implements the BeanConverter interface and provides an
9    * implementation of {@link #convert(List)}that loops through a list of beans
10   * and calls a template method for each bean.
11   * <p>
12   * Subclasses must implement the template method {@link #convertBean(Object)}.
13   * Below is an example of such an implementation:
14   * 
15   * <pre>
16   * package com.acme.valuehandler;
17   * 
18   * import com.acme.valuehandler.model.RegionBean;
19   * 
20   * public class RegionBeanConverter extends AbstractBeanConverter {
21   * 
22   *     protected LabelValueBean convertBean(Object bean) {
23   *         RegionBean regionBean = ((RegionBean) bean);
24   *         return new LabelValueBean(regionBean.getName(), regionBean.getCode());
25   *     }
26   * }
27   * </pre>
28   * 
29   * @author Mattias Arthursson
30   * @author Ulrik Sandberg
31   */
32  public abstract class AbstractBeanConverter implements BeanConverter {
33  
34      /***
35       * Default implementation that loops through the supplied list of beans and
36       * calls {@link #convertBean(Object)}on each one of them.
37       * 
38       * @param beans
39       *            list of beans of any kind.
40       * @return a new List containing the corresponding
41       *         <code>LabelValueBean</code> objects.
42       */
43      public List convert(List beans) {
44          List retval = new ArrayList(beans.size());
45          for (Iterator iter = beans.iterator(); iter.hasNext();) {
46              LabelValueBean bean = convertBean(iter.next());
47              retval.add(bean);
48          }
49  
50          return retval;
51      }
52  
53      /***
54       * Template method that must provide the conversion of a single object into
55       * a <code>LabelValueBean</code>.
56       * 
57       * @param object
58       *            An object to convert.
59       * @return A LabelValueBean object.
60       */
61      protected abstract LabelValueBean convertBean(Object object);
62  }