|
@@ -66,4 +66,105 @@ |
66 |
66 |
|
} |
67 |
67 |
|
|
68 |
68 |
|
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 |
+ |
} |
69 |
170 |
|
} |