1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.tomgibara.pronto.config;
19
20 import java.lang.reflect.Array;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25
26 import com.tomgibara.pronto.util.Reflect;
27
28 /**
29 * If no policy is explicitly specified for a config, an instance of this class
30 * is used by default.
31 *
32 * @author Tom Gibara
33 *
34 */
35
36 public class DefaultConfigPolicy implements ConfigPolicy {
37
38 /**
39 * The value returned by the getMinReadPeriod() method.
40 */
41
42 public static final long DEFAULT_MIN_READ_PERIOD = 3000L;
43
44 /**
45 * This default implementation returns the value of DEFAULT_MIN_READ_PERIOD.
46 *
47 * @return DEFAULT_MIN_READ_PERIOD
48 */
49
50 public long getMinReadPeriod() {
51 return DEFAULT_MIN_READ_PERIOD;
52 }
53
54 /**
55 * The default implementation returns true if and only if the new timestamp
56 * is strictly greater than the old timestamp.
57 *
58 * @param newTimestamp the timestamp of a new set of properties
59 * @param oldTimestamp the timestamp of the previous set of properties.
60 *
61 * @return newTimestamp > oldTimestamp
62 */
63
64 public boolean isTimestampNewer(final long newTimestamp, final long oldTimestamp) {
65 return newTimestamp > oldTimestamp;
66 }
67
68 /**
69 * By default exceptions are thrown.
70 *
71 * @return true
72 */
73
74 public boolean isExceptionThrown() {
75 return true;
76 }
77
78 /**
79 * By default exceptions are not logged because they are expected to be
80 * caught and logged by the client code.
81 *
82 * @return false
83 */
84
85 public boolean isExceptionLogged() {
86 return false;
87 }
88
89 /**
90 * The default implementation drops "get" and "is" prefixes and replaces
91 * progressions to upper case into hyphens eg. "getBaseURL" becomes
92 * "base-url".
93 *
94 * @param methodName the name of a method which is to be satisfied
95 * @return the name of the property to which the method maps
96 */
97
98 public String propertyFromMethod(final String methodName) {
99 return Reflect.propertyName(methodName);
100 }
101
102 /**
103 * Returns -1 for whole number primitives, 0.0 for floating point
104 * primitives, false for boolean primitives and \0 for character primitives.
105 * For the standard collection interfaces (Set, Map and List) empty
106 * collections are returned. For arrays, a zero length array is returned.
107 * For all other classes null is returned.
108 *
109 * @param clss
110 * the type of value for which a primitive value is needed
111 *
112 * @return the default values for primitives, collections and arrays
113 */
114
115 public Object defaultForClass(final Class clss) {
116
117
118
119 if (clss == Boolean.TYPE) return Boolean.FALSE;
120 if (clss == Integer.TYPE) return Integer.valueOf(-1);
121 if (clss == Double.TYPE) return Double.NaN;
122 if (clss == Long.TYPE) return Long.valueOf(-1L);
123 if (clss == Float.TYPE) return Float.NaN;
124 if (clss == Short.TYPE) return Short.valueOf((short) -1);
125 if (clss == Byte.TYPE) return Byte.valueOf((byte) -1);
126 if (clss == Character.TYPE) return Character.valueOf((char) 0);
127
128 if (clss == Set.class) return Collections.EMPTY_SET;
129 if (clss == List.class) return Collections.EMPTY_LIST;
130 if (clss == Map.class) return Collections.EMPTY_MAP;
131
132 if (clss.isArray()) return Array.newInstance(clss.getComponentType(), 0);
133
134 return null;
135 }
136
137 /**
138 * By default, the default values generated by defaultForClass are cached.
139 *
140 * @return true
141 */
142
143 public boolean isDefaultsCached() {
144 return true;
145 }
146
147 /**
148 * By default, values from possibly different class loaders are cacheable.
149 * This provides a very narrow opportunity for class incompatibilities but
150 * this is expected to be rare.
151 *
152 * @return true;
153 */
154
155 public boolean isCachingEager() {
156 return true;
157 }
158 }