1 14 package org.compiere.model; 15 16 import java.util.*; 17 18 import org.compiere.util.*; 19 20 28 public class MLookupCache 29 { 30 35 protected static void loadStart (MLookupInfo info) 36 { 37 } 39 45 protected static void loadEnd (MLookupInfo info, HashMap lookup) 46 { 47 m_loadedLookups.put(info, lookup); 48 } 50 58 protected static boolean loadFromCache (MLookupInfo info, HashMap lookupTarget) 59 { 60 Object [] keys = null; 62 for (int i = 0; keys == null && i < 3; i++) 63 { 64 try 66 { 67 keys = m_loadedLookups.keySet().toArray(); 68 } 69 catch (Exception e) 70 { 71 keys = null; 72 } 73 } 74 if (keys == null || keys.length == 0) 76 return false; 77 78 for (int i = 0; i < keys.length; i++) 80 { 81 MLookupInfo key = (MLookupInfo)keys[i]; 82 if (info.WindowNo == key.WindowNo && info.Query.equals(key.Query)) 84 return copyLookup (key, lookupTarget); 85 } 86 return false; 87 } 89 95 private static boolean copyLookup (MLookupInfo key, HashMap lookupTarget) 96 { 97 HashMap cache = (HashMap)m_loadedLookups.get(key); 98 if (cache == null) 99 return false; 100 if (cache.size() == 0) 102 { 103 m_loadedLookups.remove(key); 104 return false; 105 } 106 107 110 Iterator iterator = cache.keySet().iterator(); 113 while (iterator.hasNext()) 114 { 115 Object cacheKey = iterator.next(); 116 Object cacheData = cache.get(cacheKey); 117 lookupTarget.put(cacheKey, cacheData); 118 } 119 120 Log.trace(Log.l5_DData, "MLookupCache.copyLookup", "Count=" + lookupTarget.size()); 121 return true; 122 } 124 128 public static void cacheReset (int WindowNo) 129 { 130 int startNo = m_loadedLookups.size(); 131 ArrayList toDelete = new ArrayList(); 133 Iterator iterator = m_loadedLookups.keySet().iterator(); 134 while (iterator.hasNext()) 135 { 136 MLookupInfo info = (MLookupInfo)iterator.next(); 137 if (info.WindowNo == WindowNo) 138 toDelete.add(info); 139 } 140 141 for (int i = 0; i < toDelete.size(); i++) 143 m_loadedLookups.remove(toDelete.get(i)); 144 int endNo = m_loadedLookups.size(); 145 Log.trace(Log.l4_Data, "MLookupCache.cacheReset - WindowNo=" + WindowNo 146 + " - " + startNo + " -> " + endNo); 147 } 149 150 private static CCache m_loadedLookups = new CCache("lookups", + 50); 151 152 153 154 157 private MLookupCache() 158 { 159 } 161 } | Popular Tags |