1 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 39 public class Cache { 40 private Map caches; 41 private Map loaders; 42 43 public static Cache instance; 44 private static Object monitor = new Object (); 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 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 type, String name, Object 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 type, String 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 type) { 93 102 return new ArrayList(((Map) caches.get(type)).values()); 103 } 104 105 106 public List querySorted(Comparator c, int size, Class 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 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 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 type) { 125 Iterator iterator = getCache(type).iterator(); 126 List result = new ArrayList(); 127 while (iterator.hasNext()) { 128 Object object = iterator.next(); 129 if (query.fit(object)) { 130 result.add(object); 131 } 132 } 133 return result; 134 } 135 136 public Object get(Class type, String 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 type, String 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 load(Class type, String name) { 154 Map cache = (Map) caches.get(type); 155 Object 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 |