Skip to content

Commit c86c378

Browse files
author
Brendan W. McAdams
committed
New methods for loading and unloading encoding & decoding hooks in more detail
1 parent 847ea63 commit c86c378

1 file changed

Lines changed: 73 additions & 7 deletions

File tree

src/main/org/bson/BSON.java

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private static void _warnUnsupportedRegex( String flag ) {
150150
// --- (en|de)coding hooks -----
151151

152152
public static void addEncodingHook( Class c , Transformer t ){
153-
_anyHooks = true;
153+
_encodeHooks = true;
154154
List<Transformer> l = _encodingHooks.get( c );
155155
if ( l == null ){
156156
l = new Vector<Transformer>();
@@ -160,7 +160,7 @@ public static void addEncodingHook( Class c , Transformer t ){
160160
}
161161

162162
public static void addDecodingHook( Class c , Transformer t ){
163-
_anyHooks = true;
163+
_decodeHooks = true;
164164
List<Transformer> l = _decodingHooks.get( c );
165165
if ( l == null ){
166166
l = new Vector<Transformer>();
@@ -170,7 +170,7 @@ public static void addDecodingHook( Class c , Transformer t ){
170170
}
171171

172172
public static Object applyEncodingHooks( Object o ){
173-
if ( ! _anyHooks )
173+
if ( ! _anyHooks() )
174174
return o;
175175

176176
if ( _encodingHooks.size() == 0 || o == null )
@@ -183,7 +183,7 @@ public static Object applyEncodingHooks( Object o ){
183183
}
184184

185185
public static Object applyDecodingHooks( Object o ){
186-
if ( ! _anyHooks || o == null )
186+
if ( ! _anyHooks() || o == null )
187187
return o;
188188

189189
List<Transformer> l = _decodingHooks.get( o.getClass() );
@@ -193,14 +193,80 @@ public static Object applyDecodingHooks( Object o ){
193193
return o;
194194
}
195195

196+
/**
197+
* Returns the encoding hook(s) associated with the specified class
198+
*
199+
*/
200+
public static List<Transformer> getEncodingHooks( Class c ){
201+
return _encodingHooks.get( c );
202+
}
196203

197-
public static void clearAllHooks(){
198-
_anyHooks = false;
204+
/**
205+
* Clears *all* encoding hooks.
206+
*/
207+
public static void clearEncodingHooks(){
208+
_encodeHooks = false;
199209
_encodingHooks.clear();
210+
}
211+
212+
/**
213+
* Remove all encoding hooks for a specific class.
214+
*/
215+
public static void removeEncodingHooks( Class c ){
216+
_encodingHooks.remove( c );
217+
}
218+
219+
/**
220+
* Remove a specific encoding hook for a specific class.
221+
*/
222+
public static void removeEncodingHook( Class c , Transformer t ){
223+
getEncodingHooks( c ).remove( t );
224+
}
225+
226+
/**
227+
* Returns the decoding hook(s) associated with the specific class
228+
*/
229+
public static List<Transformer> getDecodingHooks( Class c ){
230+
return _decodingHooks.get( c );
231+
}
232+
233+
/**
234+
* Clears *all* decoding hooks.
235+
*/
236+
public static void clearDecodingHooks(){
237+
_decodeHooks = false;
200238
_decodingHooks.clear();
201239
}
202240

203-
private static boolean _anyHooks = false;
241+
/**
242+
* Remove all decoding hooks for a specific class.
243+
*/
244+
public static void removeDecodingHooks( Class c ){
245+
_decodingHooks.remove( c );
246+
}
247+
248+
/**
249+
* Remove a specific encoding hook for a specific class.
250+
*/
251+
public static void removeDecodingHook( Class c , Transformer t ){
252+
getDecodingHooks( c ).remove( t );
253+
}
254+
255+
256+
public static void clearAllHooks(){
257+
clearEncodingHooks();
258+
clearDecodingHooks();
259+
}
260+
261+
/**
262+
* Returns true if any encoding or decoding hooks are loaded.
263+
*/
264+
private static boolean _anyHooks(){
265+
return _encodeHooks | _decodeHooks;
266+
}
267+
268+
private static boolean _encodeHooks = false;
269+
private static boolean _decodeHooks = false;
204270
static ClassMap<List<Transformer>> _encodingHooks =
205271
new ClassMap<List<Transformer>>();
206272

0 commit comments

Comments
 (0)