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.object;
17  
18  import be.objectify.led.FactoryResolver;
19  import be.objectify.led.ObjectFactory;
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  
23  import java.lang.reflect.Field;
24  import java.util.Collection;
25  import java.util.Set;
26  
27  /**
28   * @author Steve Chaloner
29   */
30  public abstract class SetFactory<T> extends AbstractObjectFactory<Set>
31  {
32      private static final Logger LOGGER = LoggerFactory.getLogger(SetFactory.class);
33  
34      private final Class clazz;
35  
36      private final Field field;
37  
38      private final FactoryResolver factoryResolver;
39  
40      public SetFactory(Class clazz,
41                         Field field,
42                         FactoryResolver factoryResolver)
43      {
44          this.clazz = clazz;
45          this.field = field;
46          this.factoryResolver = factoryResolver;
47      }
48  
49      public Set<T> createObject(String propertyName,
50                                 String propertyValue)
51      {
52          Set<T> set = createSet();
53          ObjectFactory objectFactory = factoryResolver.resolveFactory(clazz,
54                                                                       field);
55          if (objectFactory == null)
56          {
57              LOGGER.info("No factory available for type [{}]",
58                          clazz.getCanonicalName());
59          }
60          else
61          {
62              Collection<T> values = parse(propertyName,
63                                           propertyValue,
64                                           objectFactory);
65              if (values != null)
66              {
67                  set.addAll(values);
68              }
69              else
70              {
71                  LOGGER.info("Property value [{}] did not parse into collection of type [{}]",
72                              propertyValue,
73                              clazz.getCanonicalName());
74              }
75          }
76  
77          return set;
78      }
79  
80      public Class<Set> getBoundClass()
81      {
82          return Set.class;
83      }
84  
85      protected abstract Collection<T> parse(String propertyName,
86                                             String propertyValue,
87                                             ObjectFactory<T> objectFactory);
88  
89      protected abstract Set<T> createSet();
90  }