KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > cache > Cache


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.cache;
27
28 import org.snipsnap.snip.storage.Storage;
29 import org.snipsnap.snip.storage.query.Query;
30
31 import java.util.*;
32
33 /**
34  * CacheManager for caching Objects (Snips).
35  *
36  * @author stephan
37  * @version $Id: Cache.java 689 2003-02-03 14:07:06Z stephan $
38  */

39 public class Cache {
40   private Map caches;
41   private Map loaders;
42
43   public static Cache instance;
44   private static Object JavaDoc monitor = new Object JavaDoc();
45
46   public static Cache getInstance() {
47     synchronized (monitor) {
48       if (null == instance) {
49         instance = new Cache();
50       }
51     }
52     return instance;
53   }
54
55   public static void removeInstance() {
56     synchronized (monitor) {
57       if (null != instance) {
58         instance = null;
59       }
60     }
61   }
62
63   private Cache() {
64     caches = new HashMap();
65     loaders = new HashMap();
66   }
67
68   public void setLoader(Class JavaDoc type, Storage loader) {
69     loaders.put(type, loader);
70     if (!caches.containsKey(type)) {
71       caches.put(type, new HashMap());
72     }
73   }
74
75   public void put(Class JavaDoc type, String JavaDoc name, Object JavaDoc snip) {
76     Map cache = (Map) caches.get(type);
77     if (null != cache) {
78       cache.put(name.toUpperCase(), snip);
79     }
80     return;
81   }
82
83   public boolean contains(Class JavaDoc type, String JavaDoc name) {
84     Map cache = (Map) caches.get(type);
85     if (null != cache) {
86       return cache.containsKey(name.toUpperCase());
87     } else {
88       return false;
89     }
90   }
91
92   public List getCache(Class JavaDoc type) {
93     //@TODO optimize to use value list, too
94
/* Map m = (Map) caches.get(type);
95     Iterator iterator = m.keySet().iterator();
96     while (iterator.hasNext()) {
97       String name = (String) iterator.next();
98       Snip snip = (Snip) m.get(name);
99       Logger.debug(name+"="+snip.getName());
100     }
101 */

102     return new ArrayList(((Map) caches.get(type)).values());
103   }
104
105
106   public List querySorted(Comparator c, int size, Class JavaDoc type) {
107     ArrayList result = new ArrayList(getCache(type));
108     Collections.sort(result, c);
109     return result.subList(0, Math.min(size, result.size()));
110   }
111
112   public List querySorted(Query query, Comparator c, Class JavaDoc type) {
113     List result = query(query, type);
114     Collections.sort(result, c);
115     return result;
116   }
117
118   public List querySorted(Query query, Comparator c, int size, Class JavaDoc type) {
119     List result = query(query, type);
120     Collections.sort(result, c);
121     return result.subList(0, Math.min(size, result.size()));
122   }
123
124   public List query(Query query, Class JavaDoc type) {
125     Iterator iterator = getCache(type).iterator();
126     List result = new ArrayList();
127     while (iterator.hasNext()) {
128       Object JavaDoc object = iterator.next();
129       if (query.fit(object)) {
130         result.add(object);
131       }
132     }
133     return result;
134   }
135
136   public Object JavaDoc get(Class JavaDoc type, String JavaDoc name) {
137     Map cache = (Map) caches.get(type);
138     if (null != cache) {
139       return cache.get(name.toUpperCase());
140     } else {
141       return null;
142     }
143   }
144
145   public void remove(Class JavaDoc type, String JavaDoc name) {
146     Map cache = (Map) caches.get(type);
147     if (null != cache) {
148       cache.remove(name.toUpperCase());
149     }
150     return;
151   }
152
153   public Object JavaDoc load(Class JavaDoc type, String JavaDoc name) {
154     Map cache = (Map) caches.get(type);
155     Object JavaDoc object = null;
156     if (contains(type, name)) {
157       object = get(type, name);
158     } else {
159       Storage loader = (Storage) loaders.get(type);
160       object = loader.loadObject(name);
161       if (null != object) {
162         put(type, name, object);
163       }
164     }
165     return object;
166   }
167 }
168
Popular Tags