KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > lib > HashMappedList


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.lib;
33
34 /**
35  * Implementation of an Map which maintains the user-defined order of the keys.
36  * Key/value pairs can be accessed by index or by key. Iterators return the
37  * keys or values in the index order.
38  *
39  * This class does not store null keys.
40  *
41  * @author fredt@users
42  * @version 1.7.2
43  * @since 1.7.2
44  */

45 public class HashMappedList extends HashMap {
46
47     public HashMappedList() {
48         this(16, 0.75f);
49     }
50
51     public HashMappedList(int initialCapacity)
52     throws IllegalArgumentException JavaDoc {
53         this(initialCapacity, 0.75f);
54     }
55
56     public HashMappedList(int initialCapacity,
57                           float loadFactor) throws IllegalArgumentException JavaDoc {
58         super(initialCapacity, loadFactor);
59     }
60
61     public Object JavaDoc get(int index) throws IndexOutOfBoundsException JavaDoc {
62
63         checkRange(index);
64
65         return objectValueTable[index];
66     }
67
68     public Object JavaDoc remove(Object JavaDoc key) {
69
70         int lookup = getLookup(key, key.hashCode());
71
72         if (lookup < 0) {
73             return null;
74         }
75
76         Object JavaDoc returnValue = super.remove(key);
77
78         removeRow(lookup);
79
80         return returnValue;
81     }
82
83     public Object JavaDoc remove(int index) throws IndexOutOfBoundsException JavaDoc {
84
85         checkRange(index);
86
87         return remove(objectKeyTable[index]);
88     }
89
90     public boolean add(Object JavaDoc key, Object JavaDoc value) {
91
92         if (keySet().contains(key)) {
93             return false;
94         }
95
96         super.put(key, value);
97
98         return true;
99     }
100
101     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
102         return super.put(key, value);
103     }
104
105     public Object JavaDoc set(int index,
106                       Object JavaDoc value) throws IndexOutOfBoundsException JavaDoc {
107
108         checkRange(index);
109
110         Object JavaDoc returnValue = objectKeyTable[index];
111
112         objectKeyTable[index] = value;
113
114         return returnValue;
115     }
116
117     public boolean insert(int index, Object JavaDoc key,
118                           Object JavaDoc value) throws IndexOutOfBoundsException JavaDoc {
119
120         if (index < 0 || index > size()) {
121             throw new IndexOutOfBoundsException JavaDoc();
122         }
123
124         if (keySet().contains(key)) {
125             return false;
126         }
127
128         if (index == size()) {
129             return add(key, value);
130         }
131
132         HashMappedList hm = new HashMappedList(size());
133
134         for (int i = index; i < size(); i++) {
135             hm.add(getKey(i), get(i));
136         }
137
138         for (int i = size() - 1; i >= index; i--) {
139             remove(i);
140         }
141
142         for (int i = 0; i < hm.size(); i++) {
143             add(hm.getKey(i), hm.get(i));
144         }
145
146         return true;
147     }
148
149     public boolean set(int index, Object JavaDoc key,
150                        Object JavaDoc value) throws IndexOutOfBoundsException JavaDoc {
151
152         checkRange(index);
153
154         if (keySet().contains(key) && getIndex(key) != index) {
155             return false;
156         }
157
158         super.remove(objectKeyTable[index]);
159         super.put(key, value);
160
161         return true;
162     }
163
164     public boolean setKey(int index,
165                           Object JavaDoc key) throws IndexOutOfBoundsException JavaDoc {
166
167         checkRange(index);
168
169         Object JavaDoc value = objectValueTable[index];
170
171         return set(index, key, value);
172     }
173
174     public Object JavaDoc getKey(int index) throws IndexOutOfBoundsException JavaDoc {
175
176         checkRange(index);
177
178         return objectKeyTable[index];
179     }
180
181     public int getIndex(Object JavaDoc key) {
182         return getLookup(key, key.hashCode());
183     }
184
185     private void checkRange(int i) {
186
187         if (i < 0 || i >= size()) {
188             throw new IndexOutOfBoundsException JavaDoc();
189         }
190     }
191 }
192
Popular Tags