01: /*
02: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
03: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
04: */
05: package com.sun.portal.rewriter.util.collections;
06:
07: import com.sun.portal.rewriter.util.Debug;
08:
09: import java.util.Collections;
10: import java.util.HashMap;
11: import java.util.Map;
12: import java.util.Observable;
13: import java.util.Observer;
14:
15: /**
16: * Utility class which provide cache capability , one needs to register this
17: * object with observer class to make cache listen to changes and update it
18: * self (uses Event/Listener concept as opposed to ping method for updating)
19: *
20: * @version 1.0 12/15/2001
21: * @author Raja Nagendra Kumar, Nagendra.Raja@sun.com
22: */
23: public final class TypedHashCache implements Observer {
24: private final Map hashMap = Collections
25: .synchronizedMap(new HashMap());
26: private final Class classType;
27:
28: public TypedHashCache(final Class aClassType) {
29: classType = aClassType;
30: }//constructor
31:
32: public Object get(final Object aObject) {
33: return hashMap.get(aObject);
34: }//get()
35:
36: public Object put(final Object aKey, final Object aValue) {
37: if (!(classType.isInstance(aValue))) {
38: throw new UnsupportedOperationException(
39: "Invalid Object type " + aValue);
40: }
41: return hashMap.put(((String) aKey).trim(), aValue);
42: }//put()
43:
44: public void update(final Observable aObservable, final Object aKey) {
45: if (Debug.isMessageEnabled()) {
46: //Debug.message( "Remove from the cache of : " + classType.getName() + " Key: " + aKey );
47: Object[] param = { classType.getName(), aKey };
48: Debug.message("PSRW_CSPR_0034", param);
49: }
50:
51: hashMap.remove(aKey);
52: }//update()
53: }//TypedHashCache
|