View Javadoc

1   /*
2    * Copyright 2009-2010 Steve Chaloner
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * @author Steve Chaloner
33   * @version <!-- $Revision$ -->, <!-- $Date$ -->
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       * {@inheritDoc}
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       * {@inheritDoc}
79       */
80      public Class<List> getBoundClass()
81      {
82          return List.class;
83      }
84  
85      /**
86       * {@inheritDoc}
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  }