1 /* Copyright (c) 2008 Sascha Kohlmann
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU Affero General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Affero General Public License for more details.
12 *
13 * You should have received a copy of the GNU Affero General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16 package net.sf.eos.config;
17
18 import static java.lang.annotation.ElementType.FIELD;
19 import static java.lang.annotation.RetentionPolicy.RUNTIME;
20
21 import java.lang.annotation.Documented;
22 import java.lang.annotation.Retention;
23 import java.lang.annotation.Target;
24
25 import net.sf.eos.Experimental;
26 import net.sf.eos.Predicate;
27
28 /**
29 * Indicates that a field name is a key of the {@link Configuration} data.
30 * The annotation describes the usage of the configuration parameter.
31 * <p>The {@code ConfigurationKey} annotation is an convention. Only
32 * {@code fields} with</p>
33 * <ul>
34 * <li>{@code public}, {@code static} and {@code final} modifiers</p>
35 * <li>which are initialized at compile time</li>
36 * <li>are of type {@link String}
37 * </ul>
38 * <p>should be annotated. Note, that the compiler couldn't check this
39 * conventions. Other tools like {@code apt} implementation should do the
40 * checking job.</p>
41 *
42 * @author Sascha Kohlmann
43 */
44 @Documented
45 @Retention(value=RUNTIME)
46 @Target(value=FIELD)
47 @Experimental
48 public @interface ConfigurationKey {
49
50 /**
51 * The {@code predicate} always returns {@code true}.
52 * @author Sascha Kohlmann
53 */
54 public static final class AlwaysTruePredicate implements Predicate<String> {
55 /**
56 * @param value will be ignored.
57 * @return always {@code true}
58 */
59 public boolean evaluate(final String value) {
60 return true;
61 }
62 }
63
64 /** The possible type of the configuration parameter.
65 * @author Sascha Kohlmann
66 * @see ConfigurationKey#type()
67 * @see Configuration */
68 public enum Type {
69 /** Value should be transformable into a {@code boolean} value.
70 * @see Boolean#parseBoolean(String)
71 * @see Configuration#getBoolean(String, boolean) */
72 BOOLEAN,
73 /** Value should be transformable into an {@code int} value.
74 * @see Integer#parseInt(String)
75 * @see Configuration#getInt(String, int) */
76 INTEGER,
77 /** Value should be transformable into an {@code float} value.
78 * @see Float#parseFloat(String)
79 * @see Configuration#getFloat(String, float) */
80 FLOAT,
81 /** Default type. */
82 STRING,
83 /** The value of the configuration parameter must be the fully
84 * qualified name of the desired class.
85 * @see Class#forName(String) */
86 CLASSNAME
87 }
88
89 /** The description of the configuration parameter. */
90 String description() default "";
91
92 /** The default value of the configuration parameter. */
93 String defaultValue() default "";
94
95 /** The type of the configuration parameter. */
96 Type type() default Type.STRING;
97
98
99 /** A {@code predicate} to valid the configuration value. {@code default}
100 * returns always {@code true}. */
101 Class<? extends net.sf.eos.Predicate<String>> validator()
102 default AlwaysTruePredicate.class;
103 }