KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > model > MLookupCache


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.model;
15
16 import java.util.*;
17
18 import org.compiere.util.*;
19
20 /**
21  * MLookup Data Cache.
22  * - not synchronized on purpose -
23  * Called from MLookup.
24  * Only caches multiple use for a single window!
25  * @author Jorg Janke
26  * @version $Id: MLookupCache.java,v 1.2 2003/07/06 18:40:34 jjanke Exp $
27  */

28 public class MLookupCache
29 {
30     /**
31      * MLookup Loader starts loading - ignore for now
32      *
33      * @param info MLookupInfo
34      */

35     protected static void loadStart (MLookupInfo info)
36     {
37     } // loadStart
38

39     /**
40      * MLookup Loader ends loading, so add it to cache
41      *
42      * @param info
43      * @param lookup
44      */

45     protected static void loadEnd (MLookupInfo info, HashMap lookup)
46     {
47         m_loadedLookups.put(info, lookup);
48     } // loadEnd
49

50     /**
51      * Load from Cache if applicable
52      * Called from MLookup constructor
53      *
54      * @param info MLookupInfo to search
55      * @param lookupTarget Target HashMap
56      * @return true, if lookup found
57      */

58     protected static boolean loadFromCache (MLookupInfo info, HashMap lookupTarget)
59     {
60         // Get the array of existing MLookupInfo
61
Object JavaDoc[] keys = null;
62         for (int i = 0; keys == null && i < 3; i++)
63         {
64             // we may get a ConcurrentModificationException
65
try
66             {
67                 keys = m_loadedLookups.keySet().toArray();
68             }
69             catch (Exception JavaDoc e)
70             {
71                 keys = null;
72             }
73         }
74         // Load if not found or nothing cached
75
if (keys == null || keys.length == 0)
76             return false;
77
78         // search for ir
79
for (int i = 0; i < keys.length; i++)
80         {
81             MLookupInfo key = (MLookupInfo)keys[i];
82             // Window and Query needs to be the same
83
if (info.WindowNo == key.WindowNo && info.Query.equals(key.Query))
84                 return copyLookup (key, lookupTarget);
85         }
86         return false;
87     } // loadFromCache
88

89     /**
90      * Copy lookup entries to target
91      * @param key key of MLookup data to copy
92      * @param lookupTarget HashMap where to load the data
93      * @return true if copied from cache
94      */

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         // Nothing cached
101
if (cache.size() == 0)
102         {
103             m_loadedLookups.remove(key);
104             return false;
105         }
106
107         // Copy Asynchronously to speed things up
108
// if (cache.size() > ?) copyAsync
109

110         // copy cache
111
// we can use iterator, as the lookup loading is complete (i.e. no additional entries)
112
Iterator iterator = cache.keySet().iterator();
113         while (iterator.hasNext())
114         {
115             Object JavaDoc cacheKey = iterator.next();
116             Object JavaDoc 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     } // copyLookup
123

124     /**
125      * Clear Static Lookup Cache for Window
126      * @param WindowNo WindowNo of Cache entries to delete
127      */

128     public static void cacheReset (int WindowNo)
129     {
130         int startNo = m_loadedLookups.size();
131         // find keys of Lookups to delete
132
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         // Do the actual delete
142
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     } // cacheReset
148

149     /** Static Lookup data with MLookupInfo -> HashMap */
150     private static CCache m_loadedLookups = new CCache("lookups", + 50);
151
152     /*************************************************************************/
153
154     /**
155      * Private constructor
156      */

157     private MLookupCache()
158     {
159     } // MLookupCache
160

161 } // MLookupCache
162
Popular Tags