Menu

[r559]: / phprpc_3.0 / java / AssocArray.java  Maximize  Restore  History

Download this file

334 lines (299 with data), 10.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/**********************************************************\
| |
| The implementation of PHPRPC Protocol 3.0 |
| |
| AssocArray.java |
| |
| Release 3.0.2 |
| Copyright by Team-PHPRPC |
| |
| WebSite: http://www.phprpc.org/ |
| http://www.phprpc.net/ |
| http://www.phprpc.com/ |
| http://sourceforge.net/projects/php-rpc/ |
| |
| Authors: Ma Bingyao <andot@ujn.edu.cn> |
| |
| This file may be distributed and/or modified under the |
| terms of the GNU Lesser General Public License (LGPL) |
| version 3.0 as published by the Free Software Foundation |
| and appearing in the included file LICENSE. |
| |
\**********************************************************/
/* AssocArray class.
*
* Copyright: Ma Bingyao <andot@ujn.edu.cn>
* Version: 3.0.2
* LastModified: Feb 16, 2009
* This library is free. You can redistribute it and/or modify it.
*/
package org.phprpc.util;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class AssocArray
implements Cloneable, Serializable {
private ArrayList arrayList;
private LinkedHashMap hashMap;
private int arrayLength;
private int maxNumber;
public AssocArray() {
arrayList = new ArrayList();
hashMap = new LinkedHashMap();
arrayLength = 0;
maxNumber = -1;
}
public AssocArray(int initialCapacity) {
arrayList = new ArrayList(initialCapacity);
hashMap = new LinkedHashMap(initialCapacity);
arrayLength = 0;
maxNumber = -1;
}
public AssocArray(int initialCapacity, float loadFactor) {
arrayList = new ArrayList(initialCapacity);
hashMap = new LinkedHashMap(initialCapacity, loadFactor);
arrayLength = 0;
maxNumber = -1;
}
public AssocArray(Collection c) {
arrayList = new ArrayList(c);
arrayLength = arrayList.size();
maxNumber = arrayLength - 1;
hashMap = new LinkedHashMap(arrayLength);
for (int i = 0; i < arrayLength; i++) {
hashMap.put(new Integer(i), arrayList.get(i));
}
}
public AssocArray(Map m) {
int len = m.size();
arrayList = new ArrayList(len);
hashMap = new LinkedHashMap(len);
arrayLength = 0;
maxNumber = -1;
Iterator keys = m.keySet().iterator();
while (keys.hasNext()) {
Object key = keys.next();
if ((key instanceof Integer) ||
(key instanceof Short) ||
(key instanceof Byte)) {
int k = ((Number)key).intValue();
if (k > -1) {
arrayLength++;
if (maxNumber < k) {
maxNumber = k;
}
// assert (maxNumber + 1 >= arrayLength);
}
hashMap.put(new Integer(k), m.get(key));
}
else if (key instanceof String) {
hashMap.put(key, m.get(key));
}
}
setArrayList();
}
private void setArrayList() {
int len = arrayList.size();
// assert (len <= arrayLength);
if (len < arrayLength) {
if(maxNumber + 1 == arrayLength) {
for (int i = len; i < arrayLength; i++) {
arrayList.add(hashMap.get(new Integer(i)));
}
}
else {
Integer key = new Integer(len);
while (hashMap.containsKey(key)) {
arrayList.add(hashMap.get(key));
key = new Integer(++len);
}
}
}
}
public ArrayList toArrayList() {
setArrayList();
return arrayList;
}
public HashMap toHashMap() {
return hashMap;
}
public LinkedHashMap toLinkedHashMap() {
return hashMap;
}
public int size() {
return hashMap.size();
}
public boolean isEmpty() {
return hashMap.isEmpty();
}
public boolean add(Object element) {
int index = arrayList.size();
boolean result = arrayList.add(element);
if (result) {
Integer key = new Integer(index);
if (!hashMap.containsKey(key)) {
arrayLength++;
if (maxNumber < index) {
maxNumber = index;
}
// assert (maxNumber + 1 >= arrayLength);
}
hashMap.put(key, element);
}
return result;
}
public boolean addAll(Collection c) {
int len = c.size();
int index = arrayList.size() - 1;
boolean result = arrayList.addAll(c);
if (result) {
for (int i = 0; i < len; i++) {
Integer key = new Integer(++index);
if (!hashMap.containsKey(key)) {
arrayLength++;
}
hashMap.put(key, arrayList.get(index));
}
if (maxNumber < index) {
maxNumber = index;
}
// assert (maxNumber + 1 >= arrayLength);
}
return result;
}
public void putAll(Map m) {
Iterator keys = m.keySet().iterator();
while (keys.hasNext()) {
Object key = keys.next();
if ((key instanceof Integer) ||
(key instanceof Short) ||
(key instanceof Byte)) {
int k = ((Number)key).intValue();
key = new Integer(k);
if (k > -1 && !hashMap.containsKey(key)) {
arrayLength++;
if (maxNumber < k) {
maxNumber = k;
}
// assert (maxNumber + 1 >= arrayLength);
}
hashMap.put(key, m.get(key));
}
else if (key instanceof String) {
hashMap.put(key, m.get(key));
}
}
setArrayList();
}
public Object get(int index) {
if (index < arrayList.size()) {
return arrayList.get(index);
}
else {
return hashMap.get(new Integer(index));
}
}
public Object get(Byte key) {
return get(key.intValue());
}
public Object get(Short key) {
return get(key.intValue());
}
public Object get(Integer key) {
return get(key.intValue());
}
public Object get(String key) {
return hashMap.get(key);
}
public Object set(int index, Object element) {
Integer key = new Integer(index);
if (index > -1) {
int size = arrayList.size();
if (size > index) {
arrayList.set(index, element);
}
else {
if (size == index) {
arrayList.add(element);
}
if (!hashMap.containsKey(key)) {
arrayLength++;
if (maxNumber < index) {
maxNumber = index;
}
// assert (maxNumber + 1 >= arrayLength);
}
}
}
return hashMap.put(key, element);
}
public Object set(Byte key, Object element) {
return set(key.intValue(), element);
}
public Object set(Short key, Object element) {
return set(key.intValue(), element);
}
public Object set(Integer key, Object element) {
return set(key.intValue(), element);
}
public Object set(String key, Object element) {
return hashMap.put(key, element);
}
public Object remove(int index) {
Integer key = new Integer(index);
if (index > -1) {
if (hashMap.containsKey(key)) {
arrayLength--;
int lastIndex = arrayList.size() - 1;
if (index <= lastIndex) {
for (int i = lastIndex; i >= index; i--) {
arrayList.remove(i);
}
if (maxNumber == index) {
maxNumber--;
}
}
else if (maxNumber == index) {
while ((--index > lastIndex) && !hashMap.containsKey(new Integer(index)));
maxNumber = index;
}
// assert (maxNumber + 1 >= arrayLength);
}
else {
return null;
}
}
return hashMap.remove(key);
}
public Object remove(Byte key) {
return remove(key.intValue());
}
public Object remove(Short key) {
return remove(key.intValue());
}
public Object remove(Integer key) {
return remove(key.intValue());
}
public Object remove(String key) {
return hashMap.remove(key);
}
public void clear() {
arrayList.clear();
hashMap.clear();
arrayLength = 0;
maxNumber = -1;
}
public Object clone() throws CloneNotSupportedException {
AssocArray result = null;
result = (AssocArray)super.clone();
result.arrayList = (ArrayList)this.arrayList.clone();
result.hashMap = (LinkedHashMap)this.hashMap.clone();
result.arrayLength = this.arrayLength;
result.maxNumber = this.maxNumber;
return result;
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.