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
5    * use this file except in compliance with the License. You may obtain a copy of
6    * 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.shortbrain.vaadin.container;
17  
18  import java.util.Collection;
19  import java.util.List;
20  
21  import org.shortbrain.vaadin.container.annotation.ContainerType;
22  import org.shortbrain.vaadin.container.property.AnnotationReaderAlgorithm;
23  import org.shortbrain.vaadin.container.property.AttributeReaderAlgorithm;
24  import org.shortbrain.vaadin.container.property.GetterReaderAlgorithm;
25  import org.shortbrain.vaadin.container.property.PropertyReaderAlgorithm;
26  
27  import com.vaadin.data.Container;
28  
29  /**
30   * ContainerFactory abstract class that define methods to facilitate the
31   * creation of Containers from a list of objects using different algorithms.
32   * 
33   * @author Vincent Demeester <vincent@demeester.fr>
34   * 
35   * @param <BEAN>
36   *            type of the beans.
37   */
38  public abstract class ContainerFactory<BEAN> implements IContainerFactory<BEAN> {
39  
40  	/**
41  	 * Return a container of type BEAN from a list of BEAN objects. It will
42  	 * update the given container if no null.
43  	 * 
44  	 * @param container
45  	 *            container to be populated.
46  	 * @param beans
47  	 *            list of beans.
48  	 * @return a container of Beans.
49  	 * 
50  	 * @deprecated should use getContainerFromCollection instead.
51  	 * @since 0.1.0
52  	 */
53  	@Deprecated
54  	public abstract Container getContainerFromList(Container container,
55  			List<BEAN> beans);
56  
57  	/**
58  	 * Return a container of type BEAN from a list of BEAN objects. It will
59  	 * update the given container if no null.
60  	 * 
61  	 * The returned container will be of the given type (containerClass).
62  	 * 
63  	 * @param container
64  	 *            container to be populated.
65  	 * @param beans
66  	 *            list of beans.
67  	 * @param containerClass
68  	 *            type of the container to return.
69  	 * @return a container of Beans.
70  	 * 
71  	 * @deprecated should use getContainerFromCollection instead.
72  	 * @since 0.1.0
73  	 */
74  	@Deprecated
75  	public abstract Container getContainerFromList(Container container,
76  			List<BEAN> beans, Class<? extends Container> containerClass);
77  
78  	/**
79  	 * Return a container of type BEAN from a list of BEAN objects.
80  	 * 
81  	 * The returned container will be of the given type (containerClass).
82  	 * 
83  	 * @param beans
84  	 *            list of beans.
85  	 * @param containerClass
86  	 *            type of the container to return.
87  	 * @return a container of Beans.
88  	 * 
89  	 * @deprecated should use getContainerFromCollection instead.
90  	 * @since 0.1.0
91  	 */
92  	@Deprecated
93  	public abstract Container getContainerFromList(List<BEAN> beans,
94  			Class<? extends Container> containerClass);
95  	
96  	/**
97  	 * Return a container of type BEAN from a collection of BEAN objects. It will
98  	 * update the given container if no null.
99  	 * 
100 	 * @param container
101 	 *            container to be populated.
102 	 * @param beans
103 	 *            collection of beans.
104 	 * @return a container of Beans.
105 	 * 
106 	 * @since 0.1.3
107 	 */
108 	public abstract Container getContainerFromCollection(Container container,
109 			Collection<BEAN> beans);
110 
111 	/**
112 	 * Return a container of type BEAN from a collection of BEAN objects. It will
113 	 * update the given container if no null.
114 	 * 
115 	 * The returned container will be of the given type (containerClass).
116 	 * 
117 	 * @param container
118 	 *            container to be populated.
119 	 * @param beans
120 	 *            collection of beans.
121 	 * @param containerClass
122 	 *            type of the container to return.
123 	 * @return a container of Beans.
124 	 * 
125 	 * @since 0.1.3
126 	 */
127 	public abstract Container getContainerFromCollection(Container container,
128 			Collection<BEAN> beans, Class<? extends Container> containerClass);
129 
130 	/**
131 	 * Return a container of type BEAN from a collection of BEAN objects.
132 	 * 
133 	 * The returned container will be of the given type (containerClass).
134 	 * 
135 	 * @param beans
136 	 *            collection of beans.
137 	 * @param containerClass
138 	 *            type of the container to return.
139 	 * @return a container of Beans.
140 	 * 
141 	 * @since 0.1.3
142 	 */
143 	public abstract Container getContainerFromCollection(Collection<BEAN> beans,
144 			Class<? extends Container> containerClass);
145 
146 	/**
147 	 * Sets the name of the bean property.
148 	 *
149 	 * @param name
150 	 *            the name.
151 	 */
152 	public abstract void setBeanProperty(String name);
153 
154 	/**
155 	 * Create a ContainerFactory of type T using a default
156 	 * {@link AttributeReaderAlgorithm}.
157 	 * 
158 	 * @see ContainerFactory#getByAlgorithm(Class, PropertyReaderAlgorithm)
159 	 * 
160 	 * @param beanClass
161 	 *            type of the container.
162 	 * @return a ContainerFactory.
163 	 */
164 	public final static <BEANTYPE> ContainerFactory<BEANTYPE> getByAttributes(
165 			Class<? extends BEANTYPE> beanClass) {
166 		return getByAlgorithm(beanClass, new AttributeReaderAlgorithm());
167 	}
168 
169 	/**
170 	 * Create a ContainerFactory of type T using a default
171 	 * {@link GetterReaderAlgorithm}.
172 	 * 
173 	 * @see ContainerFactory#getByAlgorithm(Class, PropertyReaderAlgorithm)
174 	 * 
175 	 * @param beanClass
176 	 *            type of the container.
177 	 * @return a ContainerFactory.
178 	 */
179 	public final static <BEANTYPE> ContainerFactory<BEANTYPE> getByGetters(
180 			Class<? extends BEANTYPE> beanClass) {
181 		return getByAlgorithm(beanClass, new GetterReaderAlgorithm());
182 	}
183 
184 	/**
185 	 * Create a ContainerFactory of type T using a default
186 	 * {@link AnnotationReaderAlgorithm}.
187 	 * 
188 	 * @see ContainerFactory#getByAlgorithm(Class, PropertyReaderAlgorithm)
189 	 * 
190 	 * @param beanClass
191 	 *            type of the container.
192 	 * @param containerType
193 	 *            type of container (used by AnnotationReaderAlgorithm)
194 	 * @return a ContainerFactory.
195 	 */
196 	public final static <BEANTYPE, ANNOTATIONTYPE extends Enum> ContainerFactory<BEANTYPE> getByAnnotation(
197 			Class<? extends BEANTYPE> beanClass, ANNOTATIONTYPE containerType) {
198 		return getByAlgorithm(beanClass, new AnnotationReaderAlgorithm<ANNOTATIONTYPE>(
199 				containerType));
200 	}
201 
202 	/**
203 	 * Create a ContainerFactory of type T using a
204 	 * {@link PropertyReaderAlgorithm}.
205 	 * 
206 	 * @param beanClass
207 	 *            type of the container.
208 	 * @param algorithm
209 	 *            PropertyReaderAlgorithm to use.
210 	 * @return a ContainerFactory.
211 	 */
212 	public final static <T> ContainerFactory<T> getByAlgorithm(
213 			Class<? extends T> beanClass, PropertyReaderAlgorithm algorithm) {
214 		return new AbstractContainerFactory<T>(beanClass, algorithm) {
215 		};
216 	}
217 }