1 /*
2 * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
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 de.smartics.properties.spi.core.metadata;
17
18 import java.lang.reflect.Method;
19
20 import de.smartics.properties.api.core.annotations.PropertySet;
21 import de.smartics.properties.spi.core.metadata.projectdoc.ProjectdocAnnotationCollector.Defaults;
22
23 /**
24 * Implementation to provide defaults from the project set annotation.
25 */
26 public final class PropertyMetaDataDefaults implements Defaults
27 {
28 // ******************************** Fields ********************************
29
30 // --- constants ----------------------------------------------------------
31
32 // --- members ------------------------------------------------------------
33
34 /**
35 * The default title and name.
36 */
37 private final String titleAndName;
38
39 /**
40 * The default space.
41 */
42 private final String space;
43
44 // ***************************** Initializer ******************************
45
46 // ***************************** Constructors *****************************
47
48 /**
49 * Default constructor using the declaring type. The default lack the method
50 * name specifics.
51 *
52 * @param type the type to extract default values for title, name and space.
53 * @throws NullPointerException if {@code type} is <code>null</code>.
54 */
55 public PropertyMetaDataDefaults(final Class<?> type)
56 throws NullPointerException
57 {
58 this.space = createSpace(type);
59
60 final PropertySet annotation = type.getAnnotation(PropertySet.class);
61 this.titleAndName =
62 annotation != null ? annotation.value() : type.getSimpleName();
63 }
64
65 /**
66 * Default constructor using the declaring and method type. The default lack
67 * the method name specifics.
68 *
69 * @param type the type to extract default values for title, name and space.
70 * @param propertyMethod the method to check for a property set annotation
71 * that overrides the one of the type.
72 * @throws NullPointerException if {@code type} is <code>null</code>.
73 */
74 public PropertyMetaDataDefaults(final Class<?> type,
75 final Method propertyMethod) throws NullPointerException
76 {
77 this.space = createSpace(type);
78
79 final PropertySet annotation =
80 determinePropertySetAnnotation(type, propertyMethod);
81 this.titleAndName =
82 annotation != null ? annotation.value() : type.getSimpleName();
83 }
84
85 /**
86 * Default constructor using the method and adding the name of the method.
87 *
88 * @param propertyMethod the method to derive default values.
89 */
90 public PropertyMetaDataDefaults(final Method propertyMethod)
91 {
92 final Class<?> type = propertyMethod.getDeclaringClass();
93 this.space = createSpace(type);
94
95 final PropertySet annotation =
96 determinePropertySetAnnotation(propertyMethod.getDeclaringClass(),
97 propertyMethod);
98 this.titleAndName =
99 (annotation != null ? annotation.value() : type.getSimpleName()) + '.'
100 + propertyMethod.getName();
101 }
102
103 // ***************************** Inner Classes ****************************
104
105 // ******************************** Methods *******************************
106
107 // --- init ---------------------------------------------------------------
108
109 private static String createSpace(final Class<?> type)
110 {
111 return "property:" + type.getName();
112 }
113
114 private static PropertySet determinePropertySetAnnotation(
115 final Class<?> type, final Method propertyMethod)
116 {
117 PropertySet annotation = propertyMethod.getAnnotation(PropertySet.class);
118 if (annotation == null)
119 {
120 annotation = type.getAnnotation(PropertySet.class);
121 }
122 return annotation;
123 }
124
125 // --- get&set ------------------------------------------------------------
126
127 @Override
128 public String getTitle()
129 {
130 return titleAndName;
131 }
132
133 @Override
134 public String getName()
135 {
136 return titleAndName;
137 }
138
139 @Override
140 public String getSpace()
141 {
142 return space;
143 }
144
145 // --- business -----------------------------------------------------------
146
147 // --- object basics ------------------------------------------------------
148
149 /**
150 * Returns the string representation of the object.
151 *
152 * @return the string representation of the object.
153 */
154 @Override
155 public String toString()
156 {
157 return space + '/' + titleAndName;
158 }
159 }