KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > util > CounterMap


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 /*
24  * Pool.java
25  *
26  * Created on 5 mars 2001, 16:34
27  */

28
29 package org.xquark.mapper.util;
30
31 import java.util.*;
32
33 /**
34  * Map inherited from hashmap mainting a counter for addition/removal.
35  *
36  */

37 public class CounterMap extends HashMap
38 {
39     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
40     private static final String JavaDoc RCSName = "$Name: $";
41
42     public boolean containsValue(Object JavaDoc value)
43     {
44         return super.containsValue(new ValueInfo(value));
45     }
46     
47     public Object JavaDoc get(Object JavaDoc key)
48     {
49         ValueInfo value = (ValueInfo)super.get(key);
50         if (value == null)
51             return null;
52         else
53             return value.get();
54     }
55     
56     public Object JavaDoc checkOut(Object JavaDoc key)
57     {
58         ValueInfo info = (ValueInfo)super.get(key);
59         
60         if (info == null)
61             return null;
62         else
63             info.inc();
64         
65         return info.get();
66     }
67     
68     /* If the key is already in the map, then counter is incremented and the
69      * existing object is not replaced by the new
70      */

71     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
72     {
73         Object JavaDoc info = checkOut(key);
74         
75         if (info == null)
76         {
77             super.put(key, new ValueInfo(value));
78             return null;
79         }
80         
81         return info;
82     }
83     /* not supported */
84     public void putAll(Map t) { throw new UnsupportedOperationException JavaDoc();}
85     
86     /* returns null if no mapping or if the object is not really removed
87      * i.e. in use by another user
88      **/

89     public Object JavaDoc remove(Object JavaDoc key)
90     {
91         ValueInfo info = (ValueInfo)super.get(key);
92         
93         if (info == null)
94             return null;
95         else
96             info.dec();
97         
98         if (info.isUsed())
99             return null;
100         else
101             super.remove(key);
102         
103         return info.get();
104     }
105         
106     /* removes unconditionnally the object whatever it is used.
107      **/

108     public Object JavaDoc removeAll(Object JavaDoc key)
109     {
110         ValueInfo info = (ValueInfo)super.remove(key);
111         if (info == null)
112             return null;
113         else
114             return info.get();
115     }
116     
117     public Set entrySet()
118     {
119         Map.Entry wEntry;
120         HashSet res = new HashSet();
121         Iterator it = super.entrySet().iterator();
122         while (it.hasNext())
123         {
124             wEntry = (Map.Entry)it.next();
125             res.add(((ValueInfo)wEntry.getValue()).get());
126         }
127         return res;
128     }
129     
130     public Collection values()
131     {
132         ArrayList list = new ArrayList();
133         Iterator it = super.values().iterator();
134         while (it.hasNext())
135         {
136             list.add(((ValueInfo)it.next()).get());
137         }
138         return list;
139     }
140     
141     /* change a key for a value without losing the counter */
142     public void setKey(Object JavaDoc oldKey, Object JavaDoc newKey)
143     {
144         if (containsKey(oldKey))
145             super.put(newKey, super.remove(oldKey));
146     }
147     
148     ///////////////////////////////////////////////////////////////////////
149
// INNER CLASS
150
///////////////////////////////////////////////////////////////////////
151
/*
152      * Data structure for keeping a connection per thread and a counter
153      * for the number of times the connection has been asked.
154      */

155     public class ValueInfo
156     {
157         private Object JavaDoc o;
158         private int use = 1;
159         
160         ValueInfo(Object JavaDoc o) { this.o = o;}
161         
162         void inc() { use++;}
163         void dec() { use--;}
164         public boolean isNotUsed() { return (use < 1); }
165         public boolean isUsed() { return (use > 0); }
166         public Object JavaDoc get() { return o;}
167         public boolean equals(Object JavaDoc o) {return o.equals(this.o);}
168     }
169 }
170
Popular Tags