1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package be.objectify.led.factory.type;
17
18 import be.objectify.led.FactoryResolver;
19 import be.objectify.led.GenericTypes;
20 import be.objectify.led.ObjectFactory;
21 import be.objectify.led.TypeFactory;
22 import be.objectify.led.factory.object.ListFactory;
23 import be.objectify.led.util.StringUtils;
24
25 import java.lang.reflect.Field;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.List;
29 import java.util.StringTokenizer;
30
31
32
33
34
35 public class ListTypeFactory implements TypeFactory<List>
36 {
37 private final FactoryResolver factoryResolver;
38
39 public ListTypeFactory(FactoryResolver factoryResolver)
40 {
41 this.factoryResolver = factoryResolver;
42 }
43
44
45
46
47 public ListFactory createObjectFactory(Class[] classes,
48 Field field)
49 {
50 return new ListFactory(classes[0],
51 field,
52 factoryResolver)
53 {
54 protected Collection parse(String propertyName,
55 String propertyValue,
56 ObjectFactory objectFactory)
57 {
58 List list = new ArrayList();
59 if (!StringUtils.isEmpty(propertyValue))
60 {
61 for (StringTokenizer stringTokenizer = new StringTokenizer(propertyValue); stringTokenizer.hasMoreTokens();)
62 {
63 list.add(objectFactory.createObject(propertyName,
64 stringTokenizer.nextToken()));
65 }
66 }
67 return list;
68 }
69
70 protected List createList()
71 {
72 return new ArrayList();
73 }
74 };
75 }
76
77
78
79
80 public Class<List> getBoundClass()
81 {
82 return List.class;
83 }
84
85
86
87
88 public Class[] determineClassType(Class fieldType,
89 GenericTypes genericTypes)
90 {
91 if (genericTypes == null || genericTypes.value().length != 1)
92 {
93 throw new RuntimeException("GenericTypes(value class) annotation required to determine Collection value type");
94 }
95
96 return genericTypes.value();
97 }
98 }