KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > walend > somnifugi > sql92 > WeakValueHashMap


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
9  * See the License for the specific language governing
10  * permissions and limitations under the License.
11  *
12  * When distributing Covered Code, include this CDDL
13  * HEADER in each file and include the License file at
14  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
15  *
16  * If applicable add the following below this CDDL HEADER,
17  * with the fields enclosed by brackets "[]" replaced with
18  * your own identifying information: Portions Copyright
19  * [year] [name of copyright owner]
20  */

21
22 /*
23  * @(#)WeakValueHashMap.java 1.8 05/02/06
24  *
25  * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package net.walend.somnifugi.sql92;
29
30 import java.lang.ref.Reference JavaDoc;
31 import java.lang.ref.ReferenceQueue JavaDoc;
32 import java.lang.ref.WeakReference JavaDoc;
33 import java.util.AbstractCollection JavaDoc;
34 import java.util.AbstractSet JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Collection JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.Map JavaDoc;
40 import java.util.Set JavaDoc;
41
42 class WeakValueHashMap
43     implements Map JavaDoc
44 {
45     String JavaDoc name = "Unknown";
46     
47     HashMap JavaDoc baseMap = new HashMap JavaDoc();
48     /**
49      * Reference queue for cleared WeakEntries
50      */

51     private ReferenceQueue JavaDoc myqueue = new ReferenceQueue JavaDoc();
52     
53     WeakValueHashMap(String JavaDoc name)
54     {
55         super();
56         this.name = name;
57     }
58     
59     private void cleanupMap()
60     {
61         Object JavaDoc r;
62         
63         while ( (r = myqueue.poll()) != null)
64         {
65             MyWeakValueReference wr = (MyWeakValueReference) r;
66             baseMap.remove(wr.getOriginalKey());
67         }
68         
69     }
70     @SuppressWarnings JavaDoc("unchecked")
71     static class MyWeakValueReference extends java.lang.ref.WeakReference JavaDoc
72     {
73         Object JavaDoc mykey = null;
74         
75         public MyWeakValueReference(Object JavaDoc key, Object JavaDoc value, ReferenceQueue JavaDoc q)
76         {
77             super(value,q);
78             this.mykey = key;
79         }
80         
81         public int hashCode()
82         {
83             return mykey.hashCode();
84         }
85         
86         public Object JavaDoc getOriginalKey()
87         {
88             return mykey;
89         }
90         public String JavaDoc toString()
91         {
92             return mykey.toString();
93         }
94     }
95     
96     // Query Operations
97

98     /**
99      * Returns the number of key-value mappings in this map. If the
100      * map contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
101      * <tt>Integer.MAX_VALUE</tt>.
102      *
103      * @return the number of key-value mappings in this map.
104      */

105     public int size()
106     {
107         cleanupMap();
108         return baseMap.size();
109     }
110     
111     /**
112      * Returns <tt>true</tt> if this map contains no key-value mappings.
113      *
114      * @return <tt>true</tt> if this map contains no key-value mappings.
115      */

116     public boolean isEmpty()
117     {
118         cleanupMap();
119         return baseMap.isEmpty();
120     }
121     
122     
123     /**
124      * Returns <tt>true</tt> if this map contains a mapping for the specified
125      * key. More formally, returns <tt>true</tt> if and only if
126      * this map contains at a mapping for a key <tt>k</tt> such that
127      * <tt>(key==null ? k==null : key.equals(k))</tt>. (There can be
128      * at most one such mapping.)
129      *
130      * @param key key whose presence in this map is to be tested.
131      * @return <tt>true</tt> if this map contains a mapping for the specified
132      * key.
133      *
134      * @throws ClassCastException if the key is of an inappropriate type for
135      * this map (optional).
136      * @throws NullPointerException if the key is <tt>null</tt> and this map
137      * does not not permit <tt>null</tt> keys (optional).
138      */

139     public boolean containsKey(Object JavaDoc key)
140     {
141         WeakReference JavaDoc k = (WeakReference JavaDoc)baseMap.get(key);
142         return k != null && !k.isEnqueued();
143     }
144     
145     /**
146      * Returns <tt>true</tt> if this map maps one or more keys to the
147      * specified value. More formally, returns <tt>true</tt> if and only if
148      * this map contains at least one mapping to a value <tt>v</tt> such that
149      * <tt>(value==null ? v==null : value.equals(v))</tt>. This operation
150      * will probably require time linear in the map size for most
151      * implementations of the <tt>Map</tt> interface.
152      *
153      * @param value value whose presence in this map is to be tested.
154      * @return <tt>true</tt> if this map maps one or more keys to the
155      * specified value.
156      * @throws ClassCastException if the value is of an inappropriate type for
157      * this map (optional).
158      * @throws NullPointerException if the value is <tt>null</tt> and this map
159      * does not not permit <tt>null</tt> values (optional).
160      */

161     public boolean containsValue(Object JavaDoc value)
162     {
163         cleanupMap();
164         return baseMap.containsValue(value);
165     }
166     
167     /**
168      * Returns the value to which this map maps the specified key. Returns
169      * <tt>null</tt> if the map contains no mapping for this key. A return
170      * value of <tt>null</tt> does not <i>necessarily</i> indicate that the
171      * map contains no mapping for the key; it's also possible that the map
172      * explicitly maps the key to <tt>null</tt>. The <tt>containsKey</tt>
173      * operation may be used to distinguish these two cases.
174      *
175      * <p>More formally, if this map contains a mapping from a key
176      * <tt>k</tt> to a value <tt>v</tt> such that <tt>(key==null ? k==null :
177      * key.equals(k))</tt>, then this method returns <tt>v</tt>; otherwise
178      * it returns <tt>null</tt>. (There can be at most one such mapping.)
179      *
180      * @param key key whose associated value is to be returned.
181      * @return the value to which this map maps the specified key, or
182      * <tt>null</tt> if the map contains no mapping for this key.
183      *
184      * @throws ClassCastException if the key is of an inappropriate type for
185      * this map (optional).
186      * @throws NullPointerException key is <tt>null</tt> and this map does not
187      * not permit <tt>null</tt> keys (optional).
188      *
189      * @see #containsKey(Object)
190      */

191     public Object JavaDoc get(Object JavaDoc key)
192     {
193         cleanupMap();
194         Reference JavaDoc ref = (Reference JavaDoc)baseMap.get(key);
195         return (ref == null ? null : (ref.isEnqueued() ? null :ref.get()));
196     }
197     
198     // Modification Operations
199

200     /**
201      * Associates the specified value with the specified key in this map
202      * (optional operation). If the map previously contained a mapping for
203      * this key, the old value is replaced by the specified value. (A map
204      * <tt>m</tt> is said to contain a mapping for a key <tt>k</tt> if and only
205      * if {@link #containsKey(Object) m.containsKey(k)} would return
206      * <tt>true</tt>.))
207      *
208      * @param key key with which the specified value is to be associated.
209      * @param value value to be associated with the specified key.
210      * @return previous value associated with specified key, or <tt>null</tt>
211      * if there was no mapping for key. A <tt>null</tt> return can
212      * also indicate that the map previously associated <tt>null</tt>
213      * with the specified key, if the implementation supports
214      * <tt>null</tt> values.
215      *
216      * @throws UnsupportedOperationException if the <tt>put</tt> operation is
217      * not supported by this map.
218      * @throws ClassCastException if the class of the specified key or value
219      * prevents it from being stored in this map.
220      * @throws IllegalArgumentException if some aspect of this key or value
221      * prevents it from being stored in this map.
222      * @throws NullPointerException this map does not permit <tt>null</tt>
223      * keys or values, and the specified key or value is
224      * <tt>null</tt>.
225      */

226      @SuppressWarnings JavaDoc("unchecked")
227     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
228     {
229         cleanupMap();
230         MyWeakValueReference ref = new MyWeakValueReference(key, value, myqueue);
231         WeakReference JavaDoc oldref = (WeakReference JavaDoc)baseMap.put(key, ref);
232         return (oldref == null ? null : (oldref.isEnqueued() ? null :oldref.get()));
233     }
234     
235     /**
236      * Removes the mapping for this key from this map if it is present
237      * (optional operation). More formally, if this map contains a mapping
238      * from key <tt>k</tt> to value <tt>v</tt> such that
239      * <code>(key==null ? k==null : key.equals(k))</code>, that mapping
240      * is removed. (The map can contain at most one such mapping.)
241      *
242      * <p>Returns the value to which the map previously associated the key, or
243      * <tt>null</tt> if the map contained no mapping for this key. (A
244      * <tt>null</tt> return can also indicate that the map previously
245      * associated <tt>null</tt> with the specified key if the implementation
246      * supports <tt>null</tt> values.) The map will not contain a mapping for
247      * the specified key once the call returns.
248      *
249      * @param key key whose mapping is to be removed from the map.
250      * @return previous value associated with specified key, or <tt>null</tt>
251      * if there was no mapping for key.
252      *
253      * @throws ClassCastException if the key is of an inappropriate type for
254      * this map (optional).
255      * @throws NullPointerException if the key is <tt>null</tt> and this map
256      * does not not permit <tt>null</tt> keys (optional).
257      * @throws UnsupportedOperationException if the <tt>remove</tt> method is
258      * not supported by this map.
259      */

260     public Object JavaDoc remove(Object JavaDoc key)
261     {
262         cleanupMap();
263         WeakReference JavaDoc ref = (WeakReference JavaDoc)baseMap.remove(key);
264         return (ref == null ? null : ref.get());
265     }
266     
267     
268     // Bulk Operations
269

270     /**
271      * Copies all of the mappings from the specified map to this map
272      * (optional operation). The effect of this call is equivalent to that
273      * of calling {@link #put(Object,Object) put(k, v)} on this map once
274      * for each mapping from key <tt>k</tt> to value <tt>v</tt> in the
275      * specified map. The behavior of this operation is unspecified if the
276      * specified map is modified while the operation is in progress.
277      *
278      * @param t Mappings to be stored in this map.
279      *
280      * @throws UnsupportedOperationException if the <tt>putAll</tt> method is
281      * not supported by this map.
282      *
283      * @throws ClassCastException if the class of a key or value in the
284      * specified map prevents it from being stored in this map.
285      *
286      * @throws IllegalArgumentException some aspect of a key or value in the
287      * specified map prevents it from being stored in this map.
288      * @throws NullPointerException the specified map is <tt>null</tt>, or if
289      * this map does not permit <tt>null</tt> keys or values, and the
290      * specified map contains <tt>null</tt> keys or values.
291      */

292      @SuppressWarnings JavaDoc("unchecked")
293     public void putAll(Map JavaDoc t)
294     {
295         cleanupMap();
296         baseMap.putAll(t);
297     }
298     
299     /**
300      * Removes all mappings from this map (optional operation).
301      *
302      * @throws UnsupportedOperationException clear is not supported by this
303      * map.
304      */

305     public void clear()
306     {
307         cleanupMap();
308         baseMap.clear();
309     }
310     
311     
312     // Views
313

314     /**
315      * Returns a set view of the keys contained in this map. The set is
316      * backed by the map, so changes to the map are reflected in the set, and
317      * vice-versa. If the map is modified while an iteration over the set is
318      * in progress, the results of the iteration are undefined. The set
319      * supports element removal, which removes the corresponding mapping from
320      * the map, via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
321      * <tt>removeAll</tt> <tt>retainAll</tt>, and <tt>clear</tt> operations.
322      * It does not support the add or <tt>addAll</tt> operations.
323      *
324      * @return a set view of the keys contained in this map.
325      */

326     public Set JavaDoc keySet()
327     {
328         cleanupMap();
329         return baseMap.keySet();
330     }
331     
332     /**
333      * Returns a collection view of the values contained in this map. The
334      * collection is backed by the map, so changes to the map are reflected in
335      * the collection, and vice-versa. If the map is modified while an
336      * iteration over the collection is in progress, the results of the
337      * iteration are undefined. The collection supports element removal,
338      * which removes the corresponding mapping from the map, via the
339      * <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>,
340      * <tt>removeAll</tt>, <tt>retainAll</tt> and <tt>clear</tt> operations.
341      * It does not support the add or <tt>addAll</tt> operations.
342      *
343      * @return a collection view of the values contained in this map.
344      */

345     private transient Collection JavaDoc values = null;
346     public Collection JavaDoc values()
347     {
348         cleanupMap();
349         Collection JavaDoc vs = values;
350         return (vs != null ? vs : (values = new Values()));
351     }
352     
353     private class ValueIterator implements Iterator JavaDoc
354     {
355         WeakReference JavaDoc next = null;
356         Iterator JavaDoc itr = null;
357         public ValueIterator(Iterator JavaDoc itr)
358         {
359             this.itr = itr;
360         }
361         
362         public boolean hasNext()
363         {
364             if (next != null)
365             {
366                 return true;
367             }
368             if (!itr.hasNext())
369             {
370                 return false;
371             }
372             next = (WeakReference JavaDoc)itr.next();
373             while (next.isEnqueued())
374             {
375                 itr.remove();
376                 if (itr.hasNext())
377                 {
378                     next = (WeakReference JavaDoc)itr.next();
379                 }
380                 else
381                 {
382                     next = null;
383                     break;
384                 }
385             }
386             return next != null;
387             
388         }
389         public Object JavaDoc next()
390         {
391             if (next == null)
392             {
393                 if (!hasNext())
394                 {
395                     return null;
396                 }
397             }
398             while (next.isEnqueued())
399             {
400                 itr.remove();
401                 next = null;
402                 if (! hasNext())
403                 {
404                     break;
405                 }
406             }
407             Object JavaDoc refval = (next == null ? null : next.get());
408             next = null;
409             return refval;
410         }
411         
412         public void remove()
413         {
414             itr.remove();
415         }
416     }
417     
418     private class Values extends AbstractCollection JavaDoc
419     {
420         public Iterator JavaDoc iterator()
421         {
422             return new ValueIterator(baseMap.values().iterator());
423         }
424         
425         public int size()
426         {
427             return WeakValueHashMap.this.size();
428         }
429         
430         public boolean contains(Object JavaDoc o)
431         {
432             return containsValue(o);
433         }
434         
435         public void clear()
436         {
437             WeakValueHashMap.this.clear();
438         }
439         @SuppressWarnings JavaDoc("unchecked")
440         public Object JavaDoc[] toArray()
441         {
442             Collection JavaDoc c = new ArrayList JavaDoc(size());
443             for (Iterator JavaDoc i = iterator(); i.hasNext(); )
444                 c.add(i.next());
445             return c.toArray();
446         }
447         @SuppressWarnings JavaDoc("unchecked")
448         public Object JavaDoc[] toArray(Object JavaDoc a[])
449         {
450             Collection JavaDoc c = new ArrayList JavaDoc(size());
451             for (Iterator JavaDoc i = iterator(); i.hasNext(); )
452                 c.add(i.next());
453             return c.toArray(a);
454         }
455     }
456     
457     /**
458      * Returns a set view of the mappings contained in this map. Each element
459      * in the returned set is a {@link Map.Entry}. The set is backed by the
460      * map, so changes to the map are reflected in the set, and vice-versa.
461      * If the map is modified while an iteration over the set is in progress,
462      * the results of the iteration are undefined. The set supports element
463      * removal, which removes the corresponding mapping from the map, via the
464      * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, <tt>removeAll</tt>,
465      * <tt>retainAll</tt> and <tt>clear</tt> operations. It does not support
466      * the <tt>add</tt> or <tt>addAll</tt> operations.
467      *
468      * @return a set view of the mappings contained in this map.
469      */

470     private transient Set JavaDoc entrySet = null;
471     public Set JavaDoc entrySet()
472     {
473         cleanupMap();
474         Set JavaDoc vs = entrySet;
475         return (vs != null ? vs : (entrySet = new EntrySet()));
476     }
477     
478     private class newEntryIterator implements Iterator JavaDoc
479     {
480         Map.Entry JavaDoc nextentry = null;
481         Iterator JavaDoc itr = null;
482         
483         public newEntryIterator(Iterator JavaDoc itr)
484         {
485             this.itr = itr;
486         }
487         
488         public boolean hasNext()
489         {
490             if (nextentry != null)
491             {
492                 return true;
493             }
494             if (!itr.hasNext())
495             {
496                 return false;
497             }
498             Map.Entry JavaDoc mentry = (Map.Entry JavaDoc)itr.next();
499             
500             WeakReference JavaDoc nextref = (WeakReference JavaDoc)mentry.getValue();
501             while (nextref.isEnqueued())
502             {
503                 itr.remove();
504                 mentry = (Map.Entry JavaDoc)itr.next();
505                 nextref = (WeakReference JavaDoc)mentry.getValue();
506             }
507             if (nextref != null)
508             {
509                 nextentry = new WeakValueEntry(mentry.getKey(),
510                         nextref.get());
511             }
512             return nextentry != null;
513             
514         }
515         public Object JavaDoc next()
516         {
517             if (nextentry == null)
518             {
519                 if (!hasNext())
520                 {
521                     return null;
522                 }
523             }
524             Object JavaDoc returnval = nextentry;
525             nextentry = null;
526             return returnval;
527         }
528         
529         public void remove()
530         {
531             itr.remove();
532         }
533     }
534     
535     private class EntrySet extends AbstractSet JavaDoc
536     {
537         public Iterator JavaDoc iterator()
538         {
539             return new newEntryIterator(baseMap.entrySet().iterator());
540         }
541         public boolean contains(Object JavaDoc o)
542         {
543             if (!(o instanceof Map.Entry JavaDoc))
544                 return false;
545             Map.Entry JavaDoc e = (Map.Entry JavaDoc)o;
546             return WeakValueHashMap.this.containsKey(e.getKey());
547         }
548         public boolean remove(Object JavaDoc o)
549         {
550             if (!(o instanceof Map.Entry JavaDoc))
551                 return false;
552             Map.Entry JavaDoc e = (Map.Entry JavaDoc)o;
553             return WeakValueHashMap.this.remove(e.getKey()) != null;
554         }
555         public int size()
556         {
557             return WeakValueHashMap.this.size();
558         }
559         public void clear()
560         {
561             WeakValueHashMap.this.clear();
562         }
563     }
564     
565     /**
566      * A map entry (key-value pair). The <tt>Map.entrySet</tt> method returns
567      * a collection-view of the map, whose elements are of this class. The
568      * <i>only</i> way to obtain a reference to a map entry is from the
569      * iterator of this collection-view. These <tt>Map.Entry</tt> objects are
570      * valid <i>only</i> for the duration of the iteration; more formally,
571      * the behavior of a map entry is undefined if the backing map has been
572      * modified after the entry was returned by the iterator, except through
573      * the iterator's own <tt>remove</tt> operation, or through the
574      * <tt>setValue</tt> operation on a map entry returned by the iterator.
575      *
576      * @see Map#entrySet()
577      * @since 1.2
578      */

579     class WeakValueEntry implements Map.Entry JavaDoc
580     {
581         
582         Object JavaDoc key = null;
583         Object JavaDoc value = null;
584         
585         public WeakValueEntry(Object JavaDoc key, Object JavaDoc value)
586         {
587             this.key = key;
588             this.value = value;
589         }
590         
591         /**
592          * Returns the key corresponding to this entry.
593          *
594          * @return the key corresponding to this entry.
595          */

596         public Object JavaDoc getKey()
597         {
598             return key;
599         }
600         
601         
602         /**
603          * Returns the value corresponding to this entry. If the mapping
604          * has been removed from the backing map (by the iterator's
605          * <tt>remove</tt> operation), the results of this call are undefined.
606          *
607          * @return the value corresponding to this entry.
608          */

609         public Object JavaDoc getValue()
610         {
611             return value;
612         }
613         
614         /**
615          * Replaces the value corresponding to this entry with the specified
616          * value (optional operation). (Writes through to the map.) The
617          * behavior of this call is undefined if the mapping has already been
618          * removed from the map (by the iterator's <tt>remove</tt> operation).
619          *
620          * @param value new value to be stored in this entry.
621          * @return old value corresponding to the entry.
622          *
623          * @throws UnsupportedOperationException if the <tt>put</tt> operation
624          * is not supported by the backing map.
625          * @throws ClassCastException if the class of the specified value
626          * prevents it from being stored in the backing map.
627          * @throws IllegalArgumentException if some aspect of this value
628          * prevents it from being stored in the backing map.
629          * @throws NullPointerException the backing map does not permit
630          * <tt>null</tt> values, and the specified value is
631          * <tt>null</tt>.
632          */

633         public Object JavaDoc setValue(Object JavaDoc value)
634         {
635             throw new UnsupportedOperationException JavaDoc("not supported");
636         }
637         
638         /**
639          * Compares the specified object with this entry for equality.
640          * Returns <tt>true</tt> if the given object is also a map entry and
641          * the two entries represent the same mapping. More formally, two
642          * entries <tt>e1</tt> and <tt>e2</tt> represent the same mapping
643          * if<pre>
644          * (e1.getKey()==null ?
645          * e2.getKey()==null : e1.getKey().equals(e2.getKey())) &&
646          * (e1.getValue()==null ?
647          * e2.getValue()==null : e1.getValue().equals(e2.getValue()))
648          * </pre>
649          * This ensures that the <tt>equals</tt> method works properly across
650          * different implementations of the <tt>Map.Entry</tt> interface.
651          *
652          * @param o object to be compared for equality with this map entry.
653          * @return <tt>true</tt> if the specified object is equal to this map
654          * entry.
655          */

656         public boolean equals(Object JavaDoc o)
657         {
658             
659             if (o instanceof Map.Entry JavaDoc)
660             {
661                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)o;
662                 
663                 return (getKey()==null ?
664                     entry.getKey()==null : getKey().equals(entry.getKey())) &&
665                         (getValue()==null ?
666                             entry.getValue()==null : getValue().equals(entry.getValue()));
667             }
668             else
669             {
670                 return super.equals(o);
671             }
672         }
673         
674         /**
675          * Returns the hash code value for this map entry. The hash code
676          * of a map entry <tt>e</tt> is defined to be: <pre>
677          * (e.getKey()==null ? 0 : e.getKey().hashCode()) ^
678          * (e.getValue()==null ? 0 : e.getValue().hashCode())
679          * </pre>
680          * This ensures that <tt>e1.equals(e2)</tt> implies that
681          * <tt>e1.hashCode()==e2.hashCode()</tt> for any two Entries
682          * <tt>e1</tt> and <tt>e2</tt>, as required by the general
683          * contract of <tt>Object.hashCode</tt>.
684          *
685          * @return the hash code value for this map entry.
686          * @see Object#hashCode()
687          * @see Object#equals(Object)
688          * @see #equals(Object)
689          */

690         public int hashCode()
691         {
692             return (getKey()==null ? 0 : getKey().hashCode()) ^
693                     (getValue()==null ? 0 : getValue().hashCode());
694         }
695     }
696     
697     // Comparison and hashing
698

699     /**
700      * Compares the specified object with this map for equality. Returns
701      * <tt>true</tt> if the given object is also a map and the two Maps
702      * represent the same mappings. More formally, two maps <tt>t1</tt> and
703      * <tt>t2</tt> represent the same mappings if
704      * <tt>t1.entrySet().equals(t2.entrySet())</tt>. This ensures that the
705      * <tt>equals</tt> method works properly across different implementations
706      * of the <tt>Map</tt> interface.
707      *
708      * @param o object to be compared for equality with this map.
709      * @return <tt>true</tt> if the specified object is equal to this map.
710      */

711     public boolean equals(Object JavaDoc o)
712     {
713         cleanupMap();
714         if (o instanceof WeakValueHashMap)
715         {
716             return baseMap.equals(((WeakValueHashMap)o).baseMap);
717         }
718         else
719         {
720             return baseMap.equals(o);
721         }
722     }
723     
724     /**
725      * Returns the hash code value for this map. The hash code of a map
726      * is defined to be the sum of the hashCodes of each entry in the map's
727      * entrySet view. This ensures that <tt>t1.equals(t2)</tt> implies
728      * that <tt>t1.hashCode()==t2.hashCode()</tt> for any two maps
729      * <tt>t1</tt> and <tt>t2</tt>, as required by the general
730      * contract of Object.hashCode.
731      *
732      * @return the hash code value for this map.
733      * @see Map.Entry#hashCode()
734      * @see Object#hashCode()
735      * @see Object#equals(Object)
736      * @see #equals(Object)
737      */

738     public int hashCode()
739     {
740         cleanupMap();
741         return baseMap.hashCode();
742     }
743     
744     public static void main(String JavaDoc args[])
745     {
746         WeakValueHashMap hm = new WeakValueHashMap("test");
747         Object JavaDoc key1 = new Object JavaDoc();
748         Object JavaDoc key2 = new Object JavaDoc();
749         String JavaDoc value1 = new String JavaDoc("value1");
750         String JavaDoc value2 = new String JavaDoc("value2");
751         hm.put(key1, value1);
752         hm.put(key2, value2);
753         for (int i=0; i < 10; i ++)
754         {
755             System.gc();
756         }
757         System.out.println("K1 get " + hm.get(key1));
758         System.out.println("K2 get " + hm.get(key2));
759         value2=null;
760         value1=null;
761         for (int i=0; i < 100; i ++)
762         {
763             System.gc();
764         }
765         System.out.println("K1 get " + hm.get(key1));
766         System.out.println("K2 get " + hm.get(key2));
767     }
768     
769     
770     
771     
772 }
773 /*
774  * EOF
775  */

776
Popular Tags