1 /*
2 * Copyright (c) 2010 Carman Consulting, Inc.
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
17 package org.metastopheles;
18
19 import java.io.Serializable;
20 import java.lang.annotation.Annotation;
21 import java.lang.reflect.AnnotatedElement;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.Map;
25 import java.util.Set;
26
27 /**
28 * A MetaDataObject contains data about a particular aspect of a bean class.
29 * @author James Carman
30 * @since 1.0
31 */
32 public abstract class MetaDataObject implements Serializable
33 {
34 //**********************************************************************************************************************
35 // Fields
36 //**********************************************************************************************************************
37
38 private static final long serialVersionUID = 1L;
39 private Map<FacetKey<?>,Object> facetMap = new HashMap<FacetKey<?>, Object>();
40
41 //**********************************************************************************************************************
42 // Abstract Methods
43 //**********************************************************************************************************************
44
45 /**
46 * Returns the default source for annotation data for this MetaDataObject.
47 * @return the default source for annotation data for this MetaDataObject
48 */
49 protected abstract AnnotatedElement getDefaultAnnotationSource();
50
51 //**********************************************************************************************************************
52 // Other Methods
53 //**********************************************************************************************************************
54
55 /**
56 * Retrieves a facet value associated with this MetaDataObject.
57 * @param key the facet key
58 * @return the facet value
59 */
60 @SuppressWarnings("unchecked")
61 public <T> T getFacet(FacetKey<T> key)
62 {
63 return (T) facetMap.get(key);
64 }
65
66 /**
67 * Returns the annotation associated with this MetaDataObject's default annotation source.
68 *
69 * @param annotationType the annotation type
70 * @return the annotation
71 * @see #getDefaultAnnotationSource()
72 */
73 public <A extends Annotation> A getAnnotation(Class<A> annotationType)
74 {
75 return getDefaultAnnotationSource().getAnnotation(annotationType);
76 }
77
78 /**
79 * Returns a snapshot of the keys of the facets currently associated with this MetaDataObject.
80 * @return a snapshot of the keys of the facets currently associated with this MetaDataObject
81 */
82 public Set<FacetKey<?>> getFacetKeys()
83 {
84 return new HashSet<FacetKey<?>>(facetMap.keySet());
85 }
86
87 /**
88 * Associates a facet with this MetaDataObject.
89 * @param key the facet key
90 * @param value the facet value
91 */
92 public <T> void setFacet(FacetKey<T> key, T value)
93 {
94 facetMap.put(key, value);
95 }
96 }