|
@@ -5,45 +5,36 @@ |
5 |
5 |
|
import java.util.*; |
6 |
6 |
|
|
7 |
7 |
|
@SuppressWarnings({"UnusedDeclaration"}) |
8 |
|
- |
public class Parameter |
9 |
|
- |
{ |
10 |
|
- |
public enum Form |
11 |
|
- |
{ |
|
8 |
+ |
public class Parameter { |
|
9 |
+ |
public enum Form { |
12 |
10 |
|
STRING, STRING_LIST, PATHS |
13 |
11 |
|
} |
14 |
12 |
|
|
15 |
|
- |
public static Parameter def( String pName, Form pForm, String pDescription, String pDescriptionForDefaulting ) |
16 |
|
- |
{ |
|
13 |
+ |
public static Parameter def( String pName, Form pForm, String pDescription, String pDescriptionForDefaulting ) { |
17 |
14 |
|
return new Parameter( pName, pForm, pDescription, pDescriptionForDefaulting ); |
18 |
15 |
|
} |
19 |
16 |
|
|
20 |
|
- |
public static Parameter def( String pName, Form pForm, String pDescription ) |
21 |
|
- |
{ |
|
17 |
+ |
public static Parameter def( String pName, Form pForm, String pDescription ) { |
22 |
18 |
|
return def( pName, pForm, pDescription, null ); |
23 |
19 |
|
} |
24 |
20 |
|
|
25 |
|
- |
public static Set<String> reservedNames() |
26 |
|
- |
{ |
|
21 |
+ |
public static Set<String> reservedNames() { |
27 |
22 |
|
return Collections.unmodifiableSet( RESERVED_NAMES ); |
28 |
23 |
|
} |
29 |
24 |
|
|
30 |
|
- |
public String getName() |
31 |
|
- |
{ |
|
25 |
+ |
public String getName() { |
32 |
26 |
|
return mName; |
33 |
27 |
|
} |
34 |
28 |
|
|
35 |
|
- |
public Form getForm() |
36 |
|
- |
{ |
|
29 |
+ |
public Form getForm() { |
37 |
30 |
|
return mForm; |
38 |
31 |
|
} |
39 |
32 |
|
|
40 |
|
- |
public String getDescription() |
41 |
|
- |
{ |
|
33 |
+ |
public String getDescription() { |
42 |
34 |
|
return mDescription; |
43 |
35 |
|
} |
44 |
36 |
|
|
45 |
|
- |
public String getDescriptionForDefaulting() |
46 |
|
- |
{ |
|
37 |
+ |
public String getDescriptionForDefaulting() { |
47 |
38 |
|
return mDescriptionForDefaulting; |
48 |
39 |
|
} |
49 |
40 |
|
|
|
@@ -52,119 +43,21 @@ |
52 |
43 |
|
private final String mDescription; |
53 |
44 |
|
private final String mDescriptionForDefaulting; |
54 |
45 |
|
|
55 |
|
- |
private Parameter( String pName, Form pForm, String pDescription, String pDescriptionForDefaulting ) |
56 |
|
- |
{ |
57 |
|
- |
mName = Util.assertNotEmpty( "Name", pName ).toLowerCase(); |
|
46 |
+ |
@Override |
|
47 |
+ |
public String toString() { |
|
48 |
+ |
return mName; |
|
49 |
+ |
} |
|
50 |
+ |
|
|
51 |
+ |
private Parameter( String pName, Form pForm, String pDescription, String pDescriptionForDefaulting ) { |
|
52 |
+ |
mName = Util.assertNotEmpty( "Name", pName ); |
58 |
53 |
|
mForm = pForm; |
59 |
54 |
|
mDescription = pDescription; |
60 |
55 |
|
mDescriptionForDefaulting = pDescriptionForDefaulting; |
61 |
56 |
|
|
62 |
|
- |
if ( !RESERVED_NAMES.add( mName ) ) |
63 |
|
- |
{ |
|
57 |
+ |
if ( !RESERVED_NAMES.add( mName ) ) { |
64 |
58 |
|
throw new IllegalArgumentException( "Duplicate Parameter declared with name: " + mName ); |
65 |
59 |
|
} |
66 |
60 |
|
} |
67 |
61 |
|
|
68 |
62 |
|
private static final Set<String> RESERVED_NAMES = new HashSet<String>(); |
69 |
|
- |
|
70 |
|
- |
public static class Manager |
71 |
|
- |
{ |
72 |
|
- |
private final Map<Object, Object> mData = new HashMap<Object, Object>(); |
73 |
|
- |
private final Map<Object, Object> mCachedResponses = new HashMap<Object, Object>(); |
74 |
|
- |
|
75 |
|
- |
public Manager( Manager them ) |
76 |
|
- |
{ |
77 |
|
- |
this( them.mData ); |
78 |
|
- |
} |
79 |
|
- |
|
80 |
|
- |
public Manager( Map<Object, Object> pData ) |
81 |
|
- |
{ |
82 |
|
- |
if ( pData != null ) |
83 |
|
- |
{ |
84 |
|
- |
Object zValue; |
85 |
|
- |
for ( Object key : pData.keySet() ) |
86 |
|
- |
{ |
87 |
|
- |
if ( null != (zValue = normalizeValue( pData.get( key ) )) ) |
88 |
|
- |
{ |
89 |
|
- |
mData.put( normalizeKey( key ), zValue ); |
90 |
|
- |
} |
91 |
|
- |
} |
92 |
|
- |
} |
93 |
|
- |
} |
94 |
|
- |
|
95 |
|
- |
public Object normalizeKey( Object pKey ) |
96 |
|
- |
{ |
97 |
|
- |
if ( pKey instanceof String ) |
98 |
|
- |
{ |
99 |
|
- |
pKey = Util.noEmpty( pKey.toString().toLowerCase() ); |
100 |
|
- |
} |
101 |
|
- |
Util.assertNotNull( "key", pKey ); |
102 |
|
- |
return pKey; |
103 |
|
- |
} |
104 |
|
- |
|
105 |
|
- |
public Object normalizeValue( Object pValue ) |
106 |
|
- |
{ |
107 |
|
- |
return (pValue instanceof String) ? Util.noEmpty( pValue.toString() ) : pValue; |
108 |
|
- |
} |
109 |
|
- |
|
110 |
|
- |
public synchronized <T> T getCachedResponse( Object pKey ) |
111 |
|
- |
{ |
112 |
|
- |
//noinspection unchecked |
113 |
|
- |
return (T) mCachedResponses.get( pKey ); |
114 |
|
- |
} |
115 |
|
- |
|
116 |
|
- |
public synchronized void addCachedResponse( Object pKey, Object pValue ) |
117 |
|
- |
{ |
118 |
|
- |
mCachedResponses.put( pKey, pValue ); |
119 |
|
- |
} |
120 |
|
- |
|
121 |
|
- |
public synchronized void put( Object pKey, Object pValue ) |
122 |
|
- |
{ |
123 |
|
- |
if ( pValue != null ) |
124 |
|
- |
{ |
125 |
|
- |
mData.put( pKey, pValue ); |
126 |
|
- |
} |
127 |
|
- |
else if ( null == mData.remove( pKey ) ) |
128 |
|
- |
{ |
129 |
|
- |
return; // Nothing Changed |
130 |
|
- |
} |
131 |
|
- |
mCachedResponses.remove( pKey ); |
132 |
|
- |
} |
133 |
|
- |
|
134 |
|
- |
/** |
135 |
|
- |
* Removes an item from a list or map. If the mData under the specified key is a list, the entry equal to the specified value is |
136 |
|
- |
* removed. If the mData under the specified key is a map, the entry with the key specified by value is removed. |
137 |
|
- |
*/ |
138 |
|
- |
public synchronized void remove( Object pKey, Object pValue ) |
139 |
|
- |
{ |
140 |
|
- |
boolean zUpdate; |
141 |
|
- |
Object object = mData.get( pKey ); |
142 |
|
- |
if ( object instanceof Map ) |
143 |
|
- |
{ |
144 |
|
- |
zUpdate = (null != ((Map) object).remove( pValue )); |
145 |
|
- |
} |
146 |
|
- |
else if ( object instanceof List ) |
147 |
|
- |
{ |
148 |
|
- |
zUpdate = ((List) object).remove( pValue ); |
149 |
|
- |
} |
150 |
|
- |
else |
151 |
|
- |
{ |
152 |
|
- |
zUpdate = (null != mData.remove( pKey )); |
153 |
|
- |
} |
154 |
|
- |
if ( zUpdate ) |
155 |
|
- |
{ |
156 |
|
- |
mCachedResponses.remove( pKey ); |
157 |
|
- |
} |
158 |
|
- |
} |
159 |
|
- |
|
160 |
|
- |
public synchronized Object get( Object pKey ) |
161 |
|
- |
{ |
162 |
|
- |
return mData.get( pKey ); |
163 |
|
- |
} |
164 |
|
- |
|
165 |
|
- |
public synchronized Object[] keys() |
166 |
|
- |
{ |
167 |
|
- |
return mData.keySet().toArray(); |
168 |
|
- |
} |
169 |
|
- |
} |
170 |
63 |
|
} |