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.property;
17  
18  import static org.shortbrain.vaadin.container.annotation.reader.ContainerBeanAnnotationReader.getMetadataByContainerType;
19  
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.shortbrain.vaadin.container.annotation.Container;
24  import org.shortbrain.vaadin.container.annotation.ContainerType;
25  
26  /**
27   * Implementation of {@link PropertyReaderAlgorithm} that looks for annotation
28   * {@link Container}.
29   * 
30   * @author Vincent Demeester <vincent@demeester.fr>
31   * 
32   */
33  public class AnnotationReaderAlgorithm<T extends Enum> implements PropertyReaderAlgorithm {
34  
35  	/**
36  	 * Container type.
37  	 */
38  	private final T containerType;
39  	private Map<ContainerType, List<PropertyMetadata>> containersMeta;
40  
41  	/**
42  	 * Create an {@link AnnotationReaderAlgorithm} with a {@link ContainerType}
43  	 * specified.
44  	 * 
45  	 * If the containerType argument is null, it will throw an
46  	 * {@link IllegalArgumentException}
47  	 * 
48  	 * @param containerType
49  	 * @throws IllegalArgumentException
50  	 */
51  	public AnnotationReaderAlgorithm(T containerType) {
52  		if (containerType == null) {
53  			throw new IllegalArgumentException("containerType cannot be null.");
54  		}
55  		this.containerType = containerType;
56  	}
57  
58  	/**
59  	 * {@inheritDoc}
60  	 * 
61  	 * FIXME: Handle Exception better.
62  	 */
63  	@Override
64  	public List<PropertyMetadata> getProperties(Class<?> beanClass) {
65  		if (containersMeta == null) {
66  			try {
67  				containersMeta = getMetadataByContainerType(beanClass);
68  			} catch (Exception e) {
69  				// The beanClass passed is not annotated with Container
70  				throw new IllegalArgumentException(
71  						"the beanClass (or parent class) has to be annotated witch @Container.", e);
72  			}
73  		}
74  		if (containersMeta == null) {
75  			// The beanClass passed is not annotated with Container
76  			throw new IllegalArgumentException(
77  					"the beanClass (or parent class) has to be annotated witch @Container.");
78  		}
79  		return containersMeta.get(containerType);
80  	}
81  }