View Javadoc

1   /*
2    * Copyright 2012 Vincent Demeester<vincent+shortbrain@demeester.fr>.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    * 
7    * http://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11   * and limitations under the License.
12   */
13  package org.shortbrain.vaadin.container;
14  
15  import java.util.Collection;
16  
17  import org.shortbrain.vaadin.container.annotation.ContainerType;
18  import org.shortbrain.vaadin.container.property.AnnotationReaderAlgorithm;
19  import org.shortbrain.vaadin.container.property.PropertyReaderAlgorithm;
20  
21  import com.vaadin.data.Container;
22  import com.vaadin.data.Container.Hierarchical;
23  import com.vaadin.data.util.AbstractBeanContainer;
24  import com.vaadin.data.util.AbstractBeanContainer.BeanIdResolver;
25  import com.vaadin.data.util.BeanContainer;
26  import com.vaadin.data.util.BeanItemContainer;
27  
28  /**
29   * ContainerFactory abstract class that define methods to facilitate the creation of Containers from a list of objects using different
30   * algorithms.
31   * 
32   * This Factory returns Container that are {@link com.vaadin.data.util.AbstractBeanContainer} implementation.
33   * 
34   * @author Vincent Demeester <vincent@shortbrain.org>
35   * 
36   * @param <IDTYPE>
37   *            The type of the item identifier
38   * @param <BEANTYPE>
39   *            The type of the Bean
40   * @since 0.2.0
41   * 
42   * @see ContainerFactory
43   * @see com.vaadin.data.util.BeanContainer
44   * @see com.vaadin.data.util.AbstractBeanContainer
45   */
46  @SuppressWarnings("rawtypes")
47  public abstract class BeanContainerFactory<IDTYPE, BEANTYPE> implements IContainerFactory<BEANTYPE> {
48  
49      /**
50       * Return a container of type BEAN from a collection of BEAN objects. It will update the given container if no null.
51       * 
52       * @param container
53       *            container to be populated.
54       * @param beans
55       *            collection of beans.
56       * @return a container of Beans.
57       * 
58       * @since 0.2.0
59       */
60      public abstract Container getContainerFromCollection(Container container, Collection<BEANTYPE> beans);
61  
62      /**
63       * Return a container of type BEAN from a collection of BEAN objects. It will update the given container if no null.
64       * 
65       * The returned container will be of the given type (containerClass).
66       * 
67       * @param container
68       *            container to be populated.
69       * @param beans
70       *            collection of beans.
71       * @param containerClass
72       *            type of the container to return.
73       * @return a container of Beans.
74       * 
75       * @since 0.2.0
76       */
77      public abstract Container getContainerFromCollection(Container container, Collection<BEANTYPE> beans,
78              Class<? extends Container> containerClass);
79  
80      /**
81       * Return a container of type BEAN from a collection of BEAN objects.
82       * 
83       * The returned container will be of the given type (containerClass).
84       * 
85       * @param beans
86       *            collection of beans.
87       * @param containerClass
88       *            type of the container to return.
89       * @return a container of Beans.
90       * 
91       * @since 0.2.0
92       */
93      public abstract Container getContainerFromCollection(Collection<BEANTYPE> beans,
94              Class<? extends Container> containerClass);
95  
96      /**
97       * Create a BeanContainerFactory of type T and idtype I using a default {@link AnnotationReaderAlgorithm} and the propertyId bean id
98       * resolver. It uses {@link BeanItemContainer} implementation by default.
99       * 
100      * @see BeanContainerFactory#getByAlgorithm(Class, PropertyReaderAlgorithm, Class, HierarchicalBeanBuilder, BeanIdResolver)
101      * 
102      * @param beanClass
103      *            type of the container.
104      * @param containerType
105      *            type of container (used by AnnotationReaderAlgorithm)
106      * @return a BeanContainerFactory.
107      * 
108      * @since 0.2.0
109      */
110     public final static <I, T, A extends Enum> BeanContainerFactory<I, T> getByAnnotation(Class<? super T> beanClass,
111             A containerType) {
112         return getByAlgorithm(beanClass, new AnnotationReaderAlgorithm<A>(containerType), BeanItemContainer.class,
113                 (BeanIdResolver<I, T>) null);
114     }
115 
116     /**
117      * Create a BeanContainerFactory of type T and idtype I using a default {@link AnnotationReaderAlgorithm} and the propertyId bean id
118      * resolver. It uses {@link BeanContainer} implemantation by default.
119      * 
120      * @see BeanContainerFactory#getByAlgorithm(Class, PropertyReaderAlgorithm, Class, HierarchicalBeanBuilder, String)
121      * 
122      * @param beanClass
123      *            type of the container.
124      * @param containerType
125      *            type of container (used by AnnotationReaderAlgorithm)
126      * @param propertyId
127      *            the identifier of the property to use to find item identifiers.
128      * @return a BeanContainerFactory.
129      * 
130      * @since 0.2.0
131      */
132     public final static <I, T, A extends Enum> BeanContainerFactory<I, T> getByAnnotation(Class<? super T> beanClass,
133             A containerType, String propertyId) {
134         return getByAlgorithm(beanClass, new AnnotationReaderAlgorithm<A>(containerType), BeanContainer.class, propertyId);
135     }
136 
137     /**
138      * Create a BeanContainerFactory of type T and idtype I using a default {@link AnnotationReaderAlgorithm} and the bean id resolver. It
139      * uses {@link BeanContainer} implemantation by default.
140      * 
141      * @see BeanContainerFactory#getByAlgorithm(Class, PropertyReaderAlgorithm, Class, HierarchicalBeanBuilder, BeanIdResolver)
142      * 
143      * @param beanClass
144      *            type of the container.
145      * @param containerType
146      *            type of container (used by AnnotationReaderAlgorithm)
147      * @param beanIdResolver
148      *            the beanIdResolver to use to find item identifiers.
149      * @return a BeanContainerFactory.
150      * 
151      * @since 0.2.0
152      */
153     public final static <I, T, A extends Enum> BeanContainerFactory<I, T> getByAnnotation(Class<? super T> beanClass,
154             A containerType, BeanIdResolver<I, T> beanIdResolver) {
155         return getByAlgorithm(beanClass, new AnnotationReaderAlgorithm<A>(containerType), BeanContainer.class,
156                 beanIdResolver);
157     }
158 
159     /**
160      * Create a BeanContainerFactory of type T and idtype I using a default {@link AnnotationReaderAlgorithm} and the propertyId bean id
161      * resolver.
162      * 
163      * @see BeanContainerFactory#getByAlgorithm(Class, PropertyReaderAlgorithm, Class, HierarchicalBeanBuilder, String)
164      * 
165      * @param beanClass
166      *            type of the container.
167      * @param containerType
168      *            type of container (used by AnnotationReaderAlgorithm)
169      * @param beanContainerType
170      *            the type of {@link AbstractBeanContainer} to use.
171      * @param propertyId
172      *            the identifier of the property to use to find item identifiers.
173      * @return a BeanContainerFactory.
174      * 
175      * @since 0.2.0
176      */
177     @SuppressWarnings("rawtypes")
178     public final static <I, T, A extends Enum> BeanContainerFactory<I, T> getByAnnotation(Class<? super T> beanClass,
179             A containerType, Class<? extends AbstractBeanContainer> beanContainerType, String propertyId) {
180         return getByAlgorithm(beanClass, new AnnotationReaderAlgorithm<A>(containerType), beanContainerType, propertyId);
181     }
182 
183     /**
184      * Create a BeanContainerFactory of type T and idtype I using a default {@link AnnotationReaderAlgorithm} and the bean id resolver.
185      * 
186      * @see BeanContainerFactory#getByAlgorithm(Class, PropertyReaderAlgorithm, Class, HierarchicalBeanBuilder, BeanIdResolver)
187      * 
188      * @param beanClass
189      *            type of the container.
190      * @param containerType
191      *            type of container (used by AnnotationReaderAlgorithm)
192      * @param beanContainerType
193      *            the type of {@link AbstractBeanContainer} to use.
194      * @param beanIdResolver
195      *            the beanIdResolver to use to find item identifiers.
196      * @return a BeanContainerFactory.
197      * 
198      * @since 0.2.0
199      */
200     @SuppressWarnings("rawtypes")
201     public final static <I, T, A extends Enum> BeanContainerFactory<I, T> getByAnnotation(Class<? super T> beanClass,
202             A containerType, Class<? extends AbstractBeanContainer> beanContainerType,
203             BeanIdResolver<I, T> beanIdResolver) {
204         return getByAlgorithm(beanClass, new AnnotationReaderAlgorithm<A>(containerType), beanContainerType,
205                 beanIdResolver);
206     }
207 
208     /**
209      * Create a BeanContainerFactory of type T and idtype I using no {@link PropertyReaderAlgorithm} and the bean id resolver. It uses
210      * {@link BeanItemContainer} implementation by default.
211      * 
212      * @see BeanContainerFactory#getByAlgorithm(Class, PropertyReaderAlgorithm, Class, HierarchicalBeanBuilder, BeanIdResolver)
213      * 
214      * @param beanClass
215      *            type of the container.
216      * @return a BeanContainerFactory.
217      * 
218      * @since 0.2.0
219      */
220     public final static <I, T> BeanContainerFactory<I, T> getSimple(Class<? super T> beanClass) {
221         return getByAlgorithm(beanClass, null, BeanItemContainer.class, (BeanIdResolver<I, T>) null);
222     }
223 
224     /**
225      * Create a BeanContainerFactory of type T and idtype I using no {@link PropertyReaderAlgorithm} and the bean id resolver. It uses
226      * {@link BeanContainer} implementation by default.
227      * 
228      * @see BeanContainerFactory#getByAlgorithm(Class, PropertyReaderAlgorithm, Class, HierarchicalBeanBuilder, BeanIdResolver)
229      * 
230      * @param beanClass
231      *            type of the container.
232      * @param beanIdResolver
233      *            the beanIdResolver to use to find item identifiers.
234      * @return a BeanContainerFactory.
235      * 
236      * @since 0.2.0
237      */
238     public final static <I, T> BeanContainerFactory<I, T> getSimple(Class<? super T> beanClass,
239             BeanIdResolver<I, T> beanIdResolver) {
240         return getByAlgorithm(beanClass, null, BeanContainer.class, beanIdResolver);
241     }
242 
243     /**
244      * Create a BeanContainerFactory of type T and idtype I using no {@link PropertyReaderAlgorithm} and the bean id resolver. It uses
245      * {@link BeanContainer} implementation by default.
246      * 
247      * @see BeanContainerFactory#getByAlgorithm(Class, PropertyReaderAlgorithm, Class, HierarchicalBeanBuilder, BeanIdResolver)
248      * 
249      * @param beanClass
250      *            type of the container.
251      * @param propertyId
252      *            the identifier of the property to use to find item identifiers.
253      * @return a BeanContainerFactory.
254      * 
255      * @since 0.2.0
256      */
257     public final static <I, T> BeanContainerFactory<I, T> getSimple(Class<? super T> beanClass, String propertyId) {
258         return getByAlgorithm(beanClass, null, BeanContainer.class, propertyId);
259     }
260 
261     /**
262      * Create a BeanContainerFactory of type T and idtype I using no {@link PropertyReaderAlgorithm} and the bean id resolver. It uses
263      * {@link BeanContainer} implementation by default.
264      * 
265      * @see BeanContainerFactory#getByAlgorithm(Class, PropertyReaderAlgorithm, Class, HierarchicalBeanBuilder, BeanIdResolver)
266      * 
267      * @param beanClass
268      *            type of the container.
269      * @param beanContainerType
270      *            the type of {@link AbstractBeanContainer} to use.
271      * @param beanIdResolver
272      *            the beanIdResolver to use to find item identifiers.
273      * @return a BeanContainerFactory.
274      * 
275      * @since 0.2.0
276      */
277     @SuppressWarnings("rawtypes")
278     public final static <I, T> BeanContainerFactory<I, T> getSimple(Class<? super T> beanClass,
279             Class<? extends AbstractBeanContainer> beanContainerType, BeanIdResolver<I, T> beanIdResolver) {
280         return getByAlgorithm(beanClass, null, beanContainerType, beanIdResolver);
281     }
282 
283     /**
284      * Create a BeanContainerFactory of type T and idtype I using no {@link PropertyReaderAlgorithm} and the bean id resolver. It uses
285      * {@link BeanContainer} implementation by default.
286      * 
287      * @see BeanContainerFactory#getByAlgorithm(Class, PropertyReaderAlgorithm, Class, HierarchicalBeanBuilder, BeanIdResolver)
288      * 
289      * @param beanClass
290      *            type of the container.
291      * @param beanContainerType
292      *            the type of {@link AbstractBeanContainer} to use.
293      * @param propertyId
294      *            the identifier of the property to use to find item identifiers.
295      * @return a BeanContainerFactory.
296      * 
297      * @since 0.2.0
298      */
299     @SuppressWarnings("rawtypes")
300     public final static <I, T> BeanContainerFactory<I, T> getSimple(Class<? super T> beanClass,
301             Class<? extends AbstractBeanContainer> beanContainerType, String propertyId) {
302         return getByAlgorithm(beanClass, null, beanContainerType, propertyId);
303     }
304 
305     /**
306      * Create a BeanContainerFactory of type T and idtype I using a {@link PropertyReaderAlgorithm} and the bean id resolver.
307      * 
308      * @param beanClass
309      *            type of the container.
310      * @param algorithm
311      *            PropertyReaderAlgorithm to use.
312      * @param beanContainerType
313      *            the type of {@link AbstractBeanContainer} to use.
314      * @param beanBuilder
315      *            the hierarchical beanBuilder to use to build a {@link Hierarchical}
316      * @param beanIdResolver
317      *            the beanIdResolver to use to find item identifiers.
318      * @return a BeanContainerFactory.
319      * 
320      * @since 0.2.0
321      */
322     @SuppressWarnings("rawtypes")
323     public final static <I, T> BeanContainerFactory<I, T> getByAlgorithm(Class<? super T> beanClass,
324             PropertyReaderAlgorithm algorithm, Class<? extends AbstractBeanContainer> beanContainerType,
325             BeanIdResolver<I, T> beanIdResolver) {
326         return new AbstractBeanContainerFactory<I, T>(beanClass, algorithm, beanContainerType, beanIdResolver) {
327         };
328     }
329 
330     /**
331      * Create a BeanContainerFactory of type T and idtype I using a {@link PropertyReaderAlgorithm} and the propertyId bean id resolver.
332      * 
333      * @param beanClass
334      *            type of the container.
335      * @param algorithm
336      *            PropertyReaderAlgorithm to use.
337      * @param beanContainerType
338      *            the type of {@link AbstractBeanContainer} to use.
339      * @param beanBuilder
340      *            the hierarchical beanBuilder to use to build a {@link Hierarchical}
341      * @param propertyId
342      *            the identifier of the property to use to find item identifiers.
343      * @return a BeanContainerFactory.
344      * 
345      * @since 0.2.0
346      */
347     @SuppressWarnings("rawtypes")
348     public final static <I, T> BeanContainerFactory<I, T> getByAlgorithm(Class<? super T> beanClass,
349             PropertyReaderAlgorithm algorithm, Class<? extends AbstractBeanContainer> beanContainerType,
350             String propertyId) {
351         return new AbstractBeanContainerFactory<I, T>(beanClass, algorithm, beanContainerType, propertyId) {
352         };
353     }
354 
355 }