1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.rblasch.convert.converters.collections;
17
18 import org.rblasch.convert.MetaConverter;
19 import org.rblasch.convert.converters.AbstractConverter;
20 import org.rblasch.convert.type.Type;
21 import org.rblasch.convert.type.Types;
22
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26
27 /***
28 * @author Ronald Blaschke
29 */
30 public class ListConverter extends AbstractConverter {
31 private final Type sourceCType, destinationCType;
32 private final MetaConverter cConverter;
33
34 public ListConverter(final Type sourceCType, final Type destinationCType, final MetaConverter cConverter) {
35 this.sourceCType = sourceCType;
36 this.destinationCType = destinationCType;
37 this.cConverter = cConverter;
38 }
39
40 public Type getSourceType() {
41 return Types.findPTypeByType(Types.findTypeByClass(List.class), sourceCType);
42 }
43
44 public Type getDestinationType() {
45 return Types.findPTypeByType(Types.findTypeByClass(ArrayList.class), destinationCType);
46 }
47
48 public int getWeight() {
49 return 100;
50 }
51
52 public Object convert(final Object obj) throws Exception {
53 final List in = (List) obj;
54
55 final List out = new ArrayList(in.size());
56 for (final Iterator i = in.iterator(); i.hasNext();) {
57 out.add(cConverter.getConverter().convert(i.next()));
58 }
59
60 return out;
61 }
62 }