KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dataview > LookupCache


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

19
20 package org.apache.cayenne.dataview;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.cayenne.DataObject;
28
29 /**
30  * @since 1.1
31  * @author Andriy Shapochka
32  */

33 public class LookupCache {
34
35     private Map JavaDoc fieldCache = new HashMap JavaDoc();
36     private static final Object JavaDoc[] EMPTY_ARRAY = new Object JavaDoc[] {};
37
38     public LookupCache() {
39     }
40
41     public void cache(ObjEntityViewField field, List JavaDoc dataObjects) {
42         Lookup lookup = getLookup(field);
43         if (lookup == null) {
44             lookup = new Lookup();
45             fieldCache.put(field, lookup);
46         }
47         lookup.cache(field, dataObjects);
48     }
49
50     public void clear() {
51         fieldCache.clear();
52     }
53
54     public boolean removeFromCache(ObjEntityViewField field) {
55         return fieldCache.remove(field) != null;
56     }
57
58     public Object JavaDoc[] getCachedValues(ObjEntityViewField field) {
59         Lookup lookup = getLookup(field);
60         Object JavaDoc[] values = (lookup != null ? lookup.values : EMPTY_ARRAY);
61         if (values.length == 0)
62             return values;
63         else {
64             Object JavaDoc[] valuesCopy = new Object JavaDoc[values.length];
65             System.arraycopy(values, 0, valuesCopy, 0, values.length);
66             return valuesCopy;
67         }
68     }
69
70     public DataObject getDataObject(ObjEntityViewField field, Object JavaDoc value) {
71         Lookup lookup = getLookup(field);
72         if (lookup == null)
73             return null;
74         return lookup.getDataObject(value);
75     }
76
77     private Lookup getLookup(ObjEntityViewField field) {
78         if (field == null)
79             return null;
80         return (Lookup) fieldCache.get(field);
81     }
82
83     private class Lookup {
84
85         ObjEntityViewField field;
86         Object JavaDoc[] values = EMPTY_ARRAY;
87         Map JavaDoc valueDataObjectMap;
88
89         void cache(ObjEntityViewField field, List JavaDoc dataObjects) {
90             this.field = field;
91             if (values.length != dataObjects.size())
92                 values = new Object JavaDoc[dataObjects.size()];
93             valueDataObjectMap = new HashMap JavaDoc(values.length + 1);
94             int index = 0;
95             for (Iterator JavaDoc i = dataObjects.iterator(); i.hasNext();) {
96                 DataObject item = (DataObject) i.next();
97                 values[index] = field.getValue(item);
98                 valueDataObjectMap.put(values[index], item);
99                 index++;
100             }
101         }
102
103         DataObject getDataObject(Object JavaDoc value) {
104             return (DataObject) valueDataObjectMap.get(value);
105         }
106     }
107 }
108
Popular Tags