KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v1 > identicator > IdMap


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v1.identicator;
25
26 import java.util.*;
27 import com.mchange.v1.util.*;
28
29 /*
30  * Implementation notes: many AbstractMap methods are written in
31  * terms of entrySet(). It is most important to get that right.
32  */

33 abstract class IdMap extends AbstractMap implements Map
34 {
35     Map inner;
36     Identicator id;
37
38     protected IdMap(Map inner, Identicator id)
39     {
40     this.inner = inner;
41     this.id = id;
42     }
43     
44     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
45     { return inner.put( createIdKey( key ), value ); }
46
47     public boolean containsKey(Object JavaDoc key)
48     { return inner.containsKey( createIdKey( key ) ); }
49
50     public Object JavaDoc get(Object JavaDoc key)
51     { return inner.get( createIdKey( key ) ); }
52
53     public Object JavaDoc remove(Object JavaDoc key)
54     { return inner.remove( createIdKey( key ) ); }
55
56     protected Object JavaDoc removeIdHashKey( IdHashKey idhk )
57     { return inner.remove( idhk ); }
58
59     public Set entrySet()
60     { return new UserEntrySet(); }
61
62     protected final Set internalEntrySet()
63     { return inner.entrySet(); }
64
65     protected abstract IdHashKey createIdKey(Object JavaDoc o);
66
67     protected final Entry createIdEntry(Object JavaDoc key, Object JavaDoc val)
68     { return new SimpleMapEntry( createIdKey( key ), val); }
69     
70     protected final Entry createIdEntry(Entry entry)
71     { return createIdEntry( entry.getKey(), entry.getValue() ); }
72
73     private final class UserEntrySet extends AbstractSet
74     {
75     Set innerEntries = inner.entrySet();
76     
77     public Iterator iterator()
78     {
79         return new WrapperIterator(innerEntries.iterator(), true)
80         {
81             protected Object JavaDoc transformObject(Object JavaDoc o)
82             { return new UserEntry( (Entry) o ); }
83         };
84     }
85     
86     public int size()
87     { return innerEntries.size(); }
88     
89     public boolean contains(Object JavaDoc o)
90     {
91         if (o instanceof Entry)
92         {
93             Entry entry = (Entry) o;
94             return innerEntries.contains( createIdEntry( entry ) );
95         }
96         else
97         return false;
98     }
99     
100     public boolean remove(Object JavaDoc o)
101     {
102         if (o instanceof Entry)
103         {
104             Entry entry = (Entry) o;
105             return innerEntries.remove( createIdEntry( entry ) );
106         }
107         else
108         return false;
109     }
110
111     public void clear()
112     { inner.clear(); }
113     }
114
115     protected static class UserEntry extends AbstractMapEntry
116     {
117     private Entry innerEntry;
118
119     UserEntry(Entry innerEntry)
120     { this.innerEntry = innerEntry; }
121
122     public final Object JavaDoc getKey()
123     { return ((IdHashKey) innerEntry.getKey()).getKeyObj(); }
124
125     public final Object JavaDoc getValue()
126     { return innerEntry.getValue(); }
127
128     public final Object JavaDoc setValue(Object JavaDoc value)
129     { return innerEntry.setValue( value ); }
130     }
131 }
132
Popular Tags