View Javadoc
1   package org.cyclopsgroup.jcli.impl;
2   
3   import java.util.Collection;
4   import java.util.List;
5   
6   import org.cyclopsgroup.caff.conversion.Converter;
7   import org.cyclopsgroup.caff.ref.ValueReference;
8   
9   class MultiValueReference<T> extends Reference<T> {
10    private final Class<?> listType;
11  
12    MultiValueReference(Class<? extends T> beanType, Converter<?> converter, ValueReference<T> ref,
13        String longName, Class<?> listType) {
14      super(converter, ref, longName);
15      this.listType = listType;
16    }
17  
18    /**
19     * Write multi value to bean
20     *
21     * @param <P> Type of value to convert
22     * @param bean Bean to set values
23     * @param values List of values to set
24     */
25    @SuppressWarnings("unchecked")
26    <P> void setValues(T bean, List<String> values) {
27      Collection<P> col;
28      try {
29        col = (Collection<P>) listType.newInstance();
30      } catch (InstantiationException e) {
31        throw new RuntimeException("Can't instantiate " + listType, e);
32      } catch (IllegalAccessException e) {
33        throw new RuntimeException("Can't access default constructor of " + listType);
34      }
35      for (String value : values) {
36        col.add((P) converter.fromCharacters(value));
37      }
38      ref.writeValue(col, bean);
39    }
40  }