KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > util > internal > cache > ClassLevelCache


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.util.internal.cache;
19
20 import java.util.Map JavaDoc;
21 import org.apache.beehive.netui.util.internal.concurrent.InternalConcurrentHashMap;
22
23
24 /**
25  * Thread-safe cache that is stored statically per-Class.
26  */

27 public final class ClassLevelCache {
28
29     private static InternalConcurrentHashMap _classCaches = new InternalConcurrentHashMap();
30     private InternalConcurrentHashMap _caches = new InternalConcurrentHashMap();
31
32
33     public static ClassLevelCache getCache(Class JavaDoc c) {
34         String JavaDoc className = c.getName();
35         ClassLevelCache cache = (ClassLevelCache)_classCaches.get(className);
36
37         if(cache == null) {
38             cache = new ClassLevelCache();
39             _classCaches.put(className, cache);
40         }
41
42         return cache;
43     }
44
45     protected ClassLevelCache() {
46     }
47
48     public Object JavaDoc get(String JavaDoc majorKey, String JavaDoc minorKey) {
49         InternalConcurrentHashMap cache = (InternalConcurrentHashMap)_caches.get(majorKey);
50         return cache != null ? cache.get(minorKey) : null;
51     }
52
53     public Object JavaDoc getCacheObject(String JavaDoc cacheID) {
54         return _caches.get(cacheID);
55     }
56
57     public void setCacheObject(String JavaDoc cacheID, Object JavaDoc object) {
58         _caches.put(cacheID, object);
59     }
60
61     public Map JavaDoc getCacheMap(String JavaDoc cacheID) {
62         return getCacheMap(cacheID, true);
63     }
64
65     public Map JavaDoc getCacheMap(String JavaDoc cacheID, boolean createIfMissing) {
66         InternalConcurrentHashMap cache = (InternalConcurrentHashMap)_caches.get(cacheID);
67
68         if(cache == null && createIfMissing) {
69             cache = new InternalConcurrentHashMap();
70             _caches.put(cacheID, cache);
71         }
72
73         return cache;
74     }
75
76     public void put(String JavaDoc cacheID, String JavaDoc minorKey, Object JavaDoc value) {
77         //
78
// ConcurrentHashMap can't accept null. For now we'll just assert; if it becomes necessary to add null,
79
// then we can use a marker value.
80
//
81
assert value != null;
82         getCacheMap(cacheID).put(minorKey, value);
83     }
84
85     public static void clearAll() {
86         _classCaches.clear();
87     }
88 }
89
Popular Tags