0c06402324cc463866e24436eb410532991db27a
[jalviewjs.git] / site / j2s / java / util / Hashtable.js
1
2 // modified by Bob Hanson 3/21/2014 6:44:21 AM  to reduce this.b$[....] phrases to simply this.h$
3 // BH added ability to use a non-Java key for HTML elements, for example.
4 // BH 8/24/2014 8:48:58 PM all synchronization and inner classes removed
5
6
7 Clazz.load([],"java.util.HashtableIterator",[],function(){
8 c$=Clazz.decorateAsClass(function(){
9 this.position=0;
10 this.expectedModCount=0;
11 this.type=null;
12 this.lastEntry=null;
13 this.lastPosition=0;
14 this.canRemove=false;
15 Clazz.instantialize(this,arguments);
16 },java.util,"HashtableIterator",null,java.util.Iterator);
17 Clazz.makeConstructor(c$,
18 function(a){
19 this.type=a;
20 this.h$ = a.h$;
21 this.position=this.h$.lastSlot;
22 this.expectedModCount=this.h$.modCount;
23 },"java.util.AbstractSet");
24 Clazz.overrideMethod(c$,"hasNext",
25 function(){
26 if(this.lastEntry&&this.lastEntry.next){
27 return true;
28 }while(this.position>=this.h$.firstSlot){
29 if(this.h$.elementData[this.position]==null){
30 this.position--;
31 }else{
32 return true;
33 }}
34 return false;
35 });
36 Clazz.overrideMethod(c$,"next",
37 function(){
38 if(this.expectedModCount==this.h$.modCount){
39 if(this.lastEntry){
40 this.lastEntry=this.lastEntry.next;
41 }if(this.lastEntry==null){
42 while(this.position>=this.h$.firstSlot&&(this.lastEntry=this.h$.elementData[this.position])==null){
43 this.position--;
44 }
45 if(this.lastEntry){
46 this.lastPosition=this.position;
47 this.position--;
48 }}if(this.lastEntry){
49 this.canRemove=true;
50 return this.type.get(this.lastEntry);
51 }throw new java.util.NoSuchElementException();
52 }throw new java.util.ConcurrentModificationException();
53 });
54 Clazz.overrideMethod(c$,"remove",
55 function(){
56 if(this.expectedModCount==this.h$.modCount){
57 if(this.canRemove){
58 this.canRemove=false;
59 {
60 var a=false;
61 var b=this.h$.elementData[this.lastPosition];
62 if(b===this.lastEntry){
63 this.h$.elementData[this.lastPosition]=b.next;
64 a=true;
65 }else{
66 while(b&&b.next!==this.lastEntry){
67 b=b.next;
68 }
69 if(b){
70 b.next=this.lastEntry.next;
71 a=true;
72 }}if(a){
73 this.h$.modCount++;
74 this.h$.elementCount--;
75 this.expectedModCount++;
76 return;
77 }}}else{
78 throw new IllegalStateException();
79 }}throw new java.util.ConcurrentModificationException();
80 });
81 });
82
83
84
85 ////////////////////////////
86
87
88 Clazz.load([],"java.util.HashtableEnumerator",[],function(){
89 c$=Clazz.decorateAsClass(function(){
90 this.key=false;
91 this.start=0;
92 this.entry=null;
93 Clazz.instantialize(this,arguments);
94 },java.util,"HashtableEnumerator",null,java.util.Enumeration);
95
96 Clazz.makeConstructor(c$,
97 function(a, b){
98 this.key = a;
99 this.h$ = b;
100 if (this.h$)this.start=this.h$.lastSlot+1;
101 },"~B,java.util.Hashtable");
102 Clazz.overrideMethod(c$,"hasMoreElements",
103 function(){
104 if (!this.h$)return false;
105 if(this.entry)return true;
106
107 while(--this.start>=this.h$.firstSlot){
108 if(this.h$.elementData[this.start]){
109 this.entry=this.h$.elementData[this.start];
110 return true;
111 }}
112 return false;
113 });
114 Clazz.overrideMethod(c$,"nextElement",
115 function(){
116 if(this.hasMoreElements()){
117 var a=this.key?this.entry.key:this.entry.value;
118 this.entry=this.entry.next;
119 return a;
120 }
121 throw new java.util.NoSuchElementException();
122 });
123 });
124
125 ////////////////////////////
126
127 Clazz.load([],"java.util.HashtableEntrySet",[],function(){
128 c$=Clazz.decorateAsClass(function(){
129 Clazz.instantialize(this,arguments);
130 },java.util,"HashtableEntrySet",java.util.AbstractSet);
131
132 Clazz.makeConstructor(c$,
133 function(a){
134 this.h$ = a;
135 },"java.util.Hashtable");
136 Clazz.overrideMethod(c$,"size",
137 function(){
138 return this.h$.elementCount;
139 });
140 Clazz.overrideMethod(c$,"clear",
141 function(){
142 this.h$.clear();
143 });
144 Clazz.overrideMethod(c$,"remove",
145 function(object){
146 if(this.contains(object)){
147 this.h$.remove((object).getKey());
148 return true;
149 }return false;
150 },"~O");
151 Clazz.defineMethod(c$,"contains",
152 function(object){
153 var entry=this.h$.getEntry((object).getKey());
154 return object.equals(entry);
155 },"~O");
156
157 Clazz.overrideMethod(c$,"get",
158 function(entry){
159 return entry;
160 },"java.util.MapEntry");
161
162 Clazz.defineMethod(c$,"iterator",
163 function(){
164 return new java.util.HashtableIterator(this);
165 });
166 });
167
168
169 ////////////////////////////
170
171 Clazz.load([],"java.util.HashtableKeySet",[],function(){
172 c$=Clazz.decorateAsClass(function(){
173 Clazz.instantialize(this,arguments);
174 },java.util,"HashtableKeySet",java.util.AbstractSet);
175
176 Clazz.makeConstructor(c$,
177 function(a){
178 this.h$ = a;
179 },"java.util.Hashtable");
180
181 Clazz.overrideMethod(c$,"contains",
182 function(object){
183 return this.h$.containsKey(object);
184 },"~O");
185 Clazz.overrideMethod(c$,"size",
186 function(){
187 return this.h$.elementCount;
188 });
189 Clazz.overrideMethod(c$,"clear",
190 function(){
191 this.h$.clear();
192 });
193 Clazz.overrideMethod(c$,"remove",
194 function(key){
195 if(this.h$.containsKey(key)){
196 this.h$.remove(key);
197 return true;
198 }return false;
199 },"~O");
200
201 Clazz.overrideMethod(c$,"get",
202 function(entry){
203 return entry.key;
204 },"java.util.MapEntry");
205
206 Clazz.overrideMethod(c$,"iterator",
207 function(){
208 return new java.util.HashtableIterator(this);
209 });
210 });
211
212 ////////////////////////////
213
214 Clazz.load([],"java.util.HashtableValueCollection",[],function(){
215 c$=Clazz.decorateAsClass(function(){
216 Clazz.instantialize(this,arguments);
217 },java.util,"HashtableValueCollection",null,java.util.AbstractCollection);
218
219 Clazz.makeConstructor(c$,
220 function(a){
221 this.h$ = a;
222 },"java.util.Hashtable");
223 Clazz.overrideMethod(c$,"contains",
224 function(object){
225 return this.h$.contains(object);
226 },"~O");
227 Clazz.overrideMethod(c$,"size",
228 function(){
229 return this.h$.elementCount;
230 });
231 Clazz.overrideMethod(c$,"clear",
232 function(){
233 this.h$.clear();
234 });
235
236 Clazz.overrideMethod(c$,"get",
237 function(entry){
238 return entry.value;
239 },"java.util.MapEntry");
240
241 Clazz.overrideMethod(c$,"iterator",
242 function(){
243 return new java.util.HashtableIterator(this);
244 });
245 });
246 ////////////////////////////
247
248
249 Clazz.load(["java.util.MapEntry"],"java.util.HashtableEntry",[],function(){
250 c$=Clazz.decorateAsClass(function(){
251 this.next=null;
252 this.hashcode=0;
253 Clazz.instantialize(this,arguments);
254 },java.util,"HashtableEntry",java.util.MapEntry);
255 Clazz.overrideConstructor(c$,
256 function(a,b){
257 this.key = a;
258 this.value = b;
259 this.hashcode=a.hashCode();
260 });
261 Clazz.defineMethod(c$,"clone",
262 function(){
263 var a=Clazz.superCall(this,java.util.HashtableEntry,"clone",[]);
264 if(this.next!=null){
265 a.next=this.next.clone();
266 }
267 return a;
268 });
269 Clazz.overrideMethod(c$,"setValue",
270 function(a){
271 if(a==null){
272 throw new NullPointerException();
273 }var b=this.value;
274 this.value=a;
275 return b;
276 },"~O");
277 Clazz.defineMethod(c$,"getKeyHash",
278 function(){
279 return this.key.hashCode();
280 });
281 Clazz.defineMethod(c$,"equalsKey",
282 function(a,b){
283 return this.hashcode==(!a.hashCode || a.hashCode())&&this.key.equals(a);
284 },"~O,~N");
285 Clazz.overrideMethod(c$,"toString",
286 function(){
287 return this.key+"="+this.value;
288 });
289 });
290
291
292
293 ////////////////////////////
294
295
296 Clazz.load(["java.util.Dictionary","$.Enumeration","$.HashtableEnumerator","$.Iterator","$.Map","$.MapEntry","$.NoSuchElementException"],"java.util.Hashtable",["java.lang.IllegalArgumentException","$.IllegalStateException","$.NullPointerException","$.StringBuilder","java.util.AbstractCollection","$.AbstractSet","$.Arrays","$.Collections","$.ConcurrentModificationException","java.util.MapEntry.Type","java.util.HashtableEntry"],function(){
297 c$=Clazz.decorateAsClass(function(){
298 this.elementCount=0;
299 this.elementData=null;
300 this.loadFactor=0;
301 this.threshold=0;
302 this.firstSlot=0;
303 this.lastSlot=-1;
304 this.modCount=0;
305 Clazz.instantialize(this,arguments);
306 },java.util,"Hashtable",java.util.Dictionary,[java.util.Map,Cloneable,java.io.Serializable]);   
307 c$.newEntry=Clazz.defineMethod(c$,"newEntry",
308 ($fz=function(key,value,hash){
309 return new java.util.HashtableEntry(key,value);
310 },$fz.isPrivate=true,$fz),"~O,~O,~N");
311 Clazz.overrideConstructor(c$,
312 function(){
313 this.elementCount=0;
314 this.elementData=this.newElementArray(11);
315 this.firstSlot=this.elementData.length;
316 this.loadFactor=0.75;
317 this.computeMaxSize();
318 });
319 Clazz.defineMethod(c$,"newElementArray",
320 ($fz=function(size){
321 return new Array(size);
322 },$fz.isPrivate=true,$fz),"~N");
323 Clazz.overrideMethod(c$,"clear",
324 function(){
325 this.elementCount=0;
326 for (var i = this.elementData.length; --i >= 0;)
327           this.elementData[i] = null;
328 this.modCount++;
329 });
330 Clazz.defineMethod(c$,"clone",
331 function(){
332 try{
333 var hashtable=Clazz.superCall(this,java.util.Hashtable,"clone",[]);
334 hashtable.elementData=this.elementData.clone();
335 var entry;
336 for(var i=this.elementData.length;--i>=0;){
337 if((entry=this.elementData[i])){
338 hashtable.elementData[i]=entry.clone();
339 }}
340 return hashtable;
341 }catch(e){
342 if(Clazz.instanceOf(e,CloneNotSupportedException)){
343 return null;
344 }else{
345 throw e;
346 }
347 }
348 });
349 Clazz.defineMethod(c$,"computeMaxSize",
350 ($fz=function(){
351 this.threshold=Math.round((this.elementData.length*this.loadFactor));
352 },$fz.isPrivate=true,$fz));
353 Clazz.defineMethod(c$,"contains",
354 function(value){
355 if(value==null){
356 throw new NullPointerException();
357 }for(var i=this.elementData.length;--i>=0;){
358 var entry=this.elementData[i];
359 while(entry){
360 if(value.equals(entry.value)){
361 return true;
362 }entry=entry.next;
363 }
364 }
365 return false;
366 },"~O");
367 Clazz.overrideMethod(c$,"containsKey",
368 function(key){
369         if(!key.hashCode)  {
370           key.hashCode = function(){return 1};
371           if (!key.equals)
372                 key.equals = function(a) {return this == a};
373         }
374 return this.getEntry(key)!=null ;
375 },"~O");
376 Clazz.overrideMethod(c$,"containsValue",
377 function(value){
378 return this.contains(value);
379 },"~O");
380 Clazz.overrideMethod(c$,"elements",
381 function(){
382 if(this.elementCount==0){
383 return java.util.Hashtable.EMPTY_ENUMERATION;
384 }
385 return new java.util.HashtableEnumerator(false, this);
386 });
387 Clazz.overrideMethod(c$,"entrySet",
388 function(){
389 return new java.util.HashtableEntrySet(this);
390 });
391 Clazz.overrideMethod(c$,"equals",
392 function(object){
393 if(this===object){
394 return true;
395 }if(Clazz.instanceOf(object,java.util.Map)){
396 var map=object;
397 if(this.size()!=map.size()){
398 return false;
399 }var entries=this.entrySet();
400 for(var e,$e=map.entrySet().iterator();$e.hasNext()&&((e=$e.next())||true);){
401 if(!entries.contains(e)){
402 return false;
403 }}
404 return true;
405 }return false;
406 },"~O");
407 Clazz.overrideMethod(c$,"get",
408 function(key){
409         if(!key.hashCode) { 
410           key.hashCode = function(){return 1};
411         if (!key.equals)
412                 key.equals = function(a) {return this == a};
413         }
414 var hash=key.hashCode();
415 var index=(hash&0x7FFFFFFF)%this.elementData.length;
416 var entry=this.elementData[index];
417 while(entry){
418 if(entry.equalsKey(key,hash)){
419 return entry.value;
420 }entry=entry.next;
421 }
422 return null;
423 },"~O");
424 Clazz.defineMethod(c$,"getEntry",
425 function(key){
426 var hash=key.hashCode();
427 var index=(hash&0x7FFFFFFF)%this.elementData.length;
428 var entry=this.elementData[index];
429 while(entry){
430 if(entry.equalsKey(key,hash)){
431 return entry;
432 }entry=entry.next;
433 }
434 return null;
435 },"~O");
436 Clazz.overrideMethod(c$,"hashCode",
437 function(){
438 var result=0;
439 var it=this.entrySet().iterator();
440 while(it.hasNext()){
441 var entry=it.next();
442 var key=entry.getKey();
443 var value=entry.getValue();
444 var hash=(key!==this?key.hashCode():0)^(value!==this?(value!=null?value.hashCode():0):0);
445 result+=hash;
446 }
447 return result;
448 });
449 Clazz.overrideMethod(c$,"isEmpty",
450 function(){
451 return this.elementCount==0;
452 });
453 Clazz.overrideMethod(c$,"keys",
454 function(){
455 if(this.elementCount==0){
456 return java.util.Hashtable.EMPTY_ENUMERATION;
457 }
458 return new java.util.HashtableEnumerator(true, this); 
459 });
460 Clazz.overrideMethod(c$,"keySet",
461 function(){
462 return new java.util.HashtableKeySet(this);
463 });
464 Clazz.overrideMethod(c$,"put",
465 function(key,value){
466 if(key!=null&&value!=null){
467         if(!key.hashCode)  {
468           key.hashCode = function(){return 1};
469           if (!key.equals)
470                 key.equals = function(a) {return this == a};
471         }
472         var hash=key.hashCode();
473         var index=(hash&0x7FFFFFFF)%this.elementData.length;
474         var entry=this.elementData[index];
475         while(entry!=null&&!entry.equalsKey(key,hash)){
476         entry=entry.next;
477 }
478 if(entry==null){
479 this.modCount++;
480 if(++this.elementCount>this.threshold){
481 this.rehash();
482 index=(hash&0x7FFFFFFF)%this.elementData.length;
483 }if(index<this.firstSlot){
484 this.firstSlot=index;
485 }if(index>this.lastSlot){
486 this.lastSlot=index;
487 }
488
489 entry=java.util.Hashtable.newEntry(key,value,hash);
490 entry.next=this.elementData[index];
491 this.elementData[index]=entry;
492 return null;
493 }var result=entry.value;
494 entry.value=value;
495 return result;
496 }throw new NullPointerException();
497 },"~O,~O");
498 Clazz.overrideMethod(c$,"putAll",
499 function(map){
500 for(var entry,$entry=map.entrySet().iterator();$entry.hasNext()&&((entry=$entry.next())||true);){
501 this.put(entry.getKey(),entry.getValue());
502 }
503 },"java.util.Map");
504
505 Clazz.defineMethod(c$,"rehash",
506 function(){
507 var length=(this.elementData.length<<1)+1;
508 if(length==0){
509 length=1;
510 }var newFirst=length;
511 var newLast=-1;
512 var newData=this.newElementArray(length);
513 for(var i=this.lastSlot+1;--i>=this.firstSlot;){
514 var entry=this.elementData[i];
515 while(entry!=null){
516 var index=(entry.getKeyHash()&0x7FFFFFFF)%length;
517 if(index<newFirst){
518 newFirst=index;
519 }if(index>newLast){
520 newLast=index;
521 }var next=entry.next;
522 entry.next=newData[index];
523 newData[index]=entry;
524 entry=next;
525 }
526 }
527 this.firstSlot=newFirst;
528 this.lastSlot=newLast;
529 this.elementData=newData;
530 this.computeMaxSize();
531 });
532 Clazz.overrideMethod(c$,"remove",
533 function(key){
534 var hash=key.hashCode();
535 var index=(hash&0x7FFFFFFF)%this.elementData.length;
536 var last=null;
537 var entry=this.elementData[index];
538 while(entry!=null&&!entry.equalsKey(key,hash)){
539 last=entry;
540 entry=entry.next;
541 }
542 if(entry!=null){
543 this.modCount++;
544 if(last==null){
545 this.elementData[index]=entry.next;
546 }else{
547 last.next=entry.next;
548 }this.elementCount--;
549 var result=entry.value;
550 entry.value=null;
551 return result;
552 }return null;
553 },"~O");
554 Clazz.overrideMethod(c$,"size",
555 function(){
556 return this.elementCount;
557 });
558 Clazz.overrideMethod(c$,"toString",
559 function(){
560 if(this.isEmpty()){
561 return"{}";
562 }var buffer=new StringBuilder(this.size()*28);
563 buffer.append('{');
564 for(var i=this.lastSlot;i>=this.firstSlot;i--){
565 var entry=this.elementData[i];
566 while(entry!=null){
567 if(entry.key!==this){
568 buffer.append(entry.key);
569 }else{
570 buffer.append("(this Map)");
571 }buffer.append('=');
572 if(entry.value!==this){
573 buffer.append(entry.value);
574 }else{
575 buffer.append("(this Map)");
576 }buffer.append(", ");
577 entry=entry.next;
578 }
579 }
580 if(this.elementCount>0){
581 buffer.setLength(buffer.length()-2);
582 }buffer.append('}');
583 return buffer.toString();
584 });
585 Clazz.overrideMethod(c$,"values",
586 function(){
587 return new java.util.HashtableValueCollection(this);
588 });
589 java.util.Hashtable.EMPTY_ENUMERATION = new java.util.HashtableEnumerator();
590 });