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.SetFactory;
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.HashSet;
29  import java.util.List;
30  import java.util.Set;
31  import java.util.StringTokenizer;
32  
33  /**
34   * @author Steve Chaloner
35   * @version <!-- $Revision$ -->, <!-- $Date$ -->
36   */
37  public class SetTypeFactory implements TypeFactory<Set>
38  {
39      private final FactoryResolver factoryResolver;
40  
41      public SetTypeFactory(FactoryResolver factoryResolver)
42      {
43          this.factoryResolver = factoryResolver;
44      }
45  
46      /**
47       * {@inheritDoc}
48       */
49      public SetFactory createObjectFactory(Class[] classes,
50                                            Field field)
51      {
52          return new SetFactory(classes[0],
53                                field,
54                                factoryResolver)
55          {
56              protected Collection parse(String propertyName,
57                                         String propertyValue,
58                                         ObjectFactory objectFactory)
59              {
60                  List list = new ArrayList();
61                  if (!StringUtils.isEmpty(propertyValue))
62                  {
63                      for (StringTokenizer stringTokenizer = new StringTokenizer(propertyValue); stringTokenizer.hasMoreTokens();)
64                      {
65                          list.add(objectFactory.createObject(propertyName,
66                                                              stringTokenizer.nextToken()));
67                      }
68                  }
69                  return list;
70              }
71  
72              protected Set createSet()
73              {
74                  return new HashSet();
75              }
76          };
77      }
78  
79      /**
80       * {@inheritDoc}
81       */
82      public Class<Set> getBoundClass()
83      {
84          return Set.class;
85      }
86  
87      /**
88       * {@inheritDoc}
89       */
90      public Class[] determineClassType(Class fieldType,
91                                        GenericTypes genericTypes)
92      {
93          if (genericTypes == null || genericTypes.value().length != 1)
94          {
95              throw new RuntimeException("GenericTypes(value class) annotation required to determine Collection value type");
96          }
97  
98          return genericTypes.value();
99      }
100 }