KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > collection > MapImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free Software Foundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.amber.collection;
30
31 import com.caucho.amber.AmberQuery;
32 import com.caucho.amber.AmberRuntimeException;
33 import com.caucho.amber.manager.AmberConnection;
34 import com.caucho.amber.query.UserQuery;
35 import com.caucho.util.Alarm;
36
37 import java.lang.reflect.Method JavaDoc;
38 import java.sql.SQLException JavaDoc;
39 import java.util.AbstractMap JavaDoc;
40 import java.util.HashMap JavaDoc;
41 import java.util.Map JavaDoc;
42 import java.util.Set JavaDoc;
43
44 /**
45  * Represents a lazy collection.
46  */

47 public class MapImpl<K, V> extends AbstractMap JavaDoc<K, V>
48   implements AmberCollection {
49   private AmberQuery _query;
50
51   private AmberConnection _aConn;
52
53   private HashMap JavaDoc<K, V> _values = new HashMap JavaDoc<K, V>();
54   private long _expireTime;
55
56   private Method JavaDoc _methodGetMapKey;
57
58   public MapImpl(AmberConnection aConn,
59                  String JavaDoc query,
60                  Method JavaDoc methodGetMapKey)
61   {
62     _aConn = aConn;
63     _methodGetMapKey = methodGetMapKey;
64
65     try {
66       _query = _aConn.prepareQuery(query);
67     } catch (SQLException JavaDoc e) {
68       throw new AmberRuntimeException(e);
69     }
70   }
71
72   public MapImpl(AmberQuery query,
73                  Method JavaDoc methodGetMapKey)
74   {
75     _query = query;
76     _methodGetMapKey = methodGetMapKey;
77
78     setSession(((UserQuery) _query).getConnection());
79   }
80
81   /**
82    * Sets the session.
83    */

84   public void setSession(AmberConnection aConn)
85   {
86     _aConn = aConn;
87
88     _aConn.register(this);
89   }
90
91   /**
92    * Returns the query.
93    */

94   public AmberQuery getQuery()
95   {
96     return _query;
97   }
98
99   /**
100    * Returns the number of items in the collection.
101    */

102   public int size()
103   {
104     fill();
105
106     return _values.size();
107   }
108
109   /**
110    * Returns a set view of the mappings contained in this map.
111    */

112   public Set JavaDoc<Map.Entry JavaDoc<K, V>> entrySet()
113   {
114     fill();
115
116     return _values.entrySet();
117   }
118
119   /**
120    * Returns an iterator of the items.
121    */

122   public V get(Object JavaDoc key)
123   {
124     fill();
125
126     return _values.get(key);
127   }
128
129   /**
130    * Returns a set view of the keys contained in this map.
131    */

132   public Set JavaDoc<K> keySet()
133   {
134     fill();
135
136     return _values.keySet();
137   }
138
139   /**
140    * Clears the collection.
141    */

142   public void clear()
143   {
144     _values.clear();
145     _expireTime = Alarm.getCurrentTime();
146   }
147
148   /**
149    * Updates the collection.
150    */

151   public void update()
152   {
153     _expireTime = 0;
154   }
155
156   protected boolean isValid()
157   {
158     return Alarm.getCurrentTime() <= _expireTime;
159   }
160
161   private void fill()
162   {
163     if (Alarm.getCurrentTime() <= _expireTime)
164       return;
165
166     try {
167       _expireTime = Alarm.getCurrentTime();
168
169       ((UserQuery) _query).setSession(_aConn);
170       _values.clear();
171       _query.list((HashMap JavaDoc) _values, _methodGetMapKey);
172     } catch (Exception JavaDoc e) {
173       throw new AmberRuntimeException(e);
174     }
175   }
176 }
177
Popular Tags