X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=site%2Fswingjs%2Fj2s%2Fjava%2Futil%2Fconcurrent%2FConcurrentHashMap.js;fp=site%2Fswingjs%2Fj2s%2Fjava%2Futil%2Fconcurrent%2FConcurrentHashMap.js;h=28146f25acd77131d6b51918b71dd5961c0a5a07;hb=4231d40261fef32ce4570a6bbad0d35df44e8baf;hp=0000000000000000000000000000000000000000;hpb=c07eb2c5794833bd0241000d6844d783fe10cb82;p=jalview.git diff --git a/site/swingjs/j2s/java/util/concurrent/ConcurrentHashMap.js b/site/swingjs/j2s/java/util/concurrent/ConcurrentHashMap.js new file mode 100644 index 0000000..28146f2 --- /dev/null +++ b/site/swingjs/j2s/java/util/concurrent/ConcurrentHashMap.js @@ -0,0 +1,523 @@ +// BH concurrent version; copy of java.util.HashMap +Clazz.declarePackage ("java.util.concurrent"); +Clazz.load(["java.util.AbstractMap","$.AbstractSet","$.Iterator","$.Map","$.MapEntry"],"java.util.concurrent.ConcurrentHashMap",["java.lang.IllegalArgumentException","$.IllegalStateException","java.util.AbstractCollection","$.Arrays","$.ConcurrentModificationException","java.util.MapEntry.Type","java.util.NoSuchElementException"],function(){ +c$=Clazz.decorateAsClass(function(){ +this.elementCount=0; +this.elementData=null; +this.loadFactor=0; +this.threshold=0; +this.modCount=0; +Clazz.instantialize(this,arguments); +},java.util.concurrent,"ConcurrentHashMap",java.util.AbstractMap,[java.util.Map,Cloneable,java.io.Serializable]); +Clazz.defineMethod(c$,"newElementArray", +function(s){ +return new Array(s); +},"~N"); +//Clazz.makeConstructor(c$, +//function(){ +//this.construct(16); +//}); +//Clazz.makeConstructor(c$, +//function(capacity){ +//Clazz.superConstructor(this,java.util.ConcurrentHashMap,[]); +//if(capacity>=0){ +//this.elementCount=0; +//this.elementData=this.newElementArray(capacity==0?1:capacity); +//this.loadFactor=0.75; +//this.computeMaxSize(); +//}else{ +//throw new IllegalArgumentException(); +//}},"~N"); +Clazz.makeConstructor(c$, +function(){ +this.construct(16); +}); +Clazz.makeConstructor(c$, +function(capacity){ +Clazz.superConstructor(this,java.util.concurrent.ConcurrentHashMap,[]); +if(capacity>=0){ +this.elementCount=0; +this.elementData=this.newElementArray(capacity==0?1:capacity); +this.loadFactor=0.75; +this.computeMaxSize(); +}else{ +throw new IllegalArgumentException(); +}},"~N"); +Clazz.makeConstructor(c$, +function(capacity,loadFactor){ +Clazz.superConstructor(this,java.util.concurrent.ConcurrentHashMap,[]); +if(capacity>=0&&loadFactor>0){ +this.elementCount=0; +this.elementData=this.newElementArray(capacity==0?1:capacity); +this.loadFactor=loadFactor; +this.computeMaxSize(); +}else{ +throw new IllegalArgumentException(); +}},"~N,~N"); +Clazz.makeConstructor(c$, +function(map){ +this.construct(map.size()<6?11:map.size()*2); +Clazz.superCall(this,java.util.concurrent.ConcurrentHashMap,"putAll",[map]); +},"java.util.Map"); + + +/* +Clazz.makeConstructor(c$, +function(capacity,loadFactor){ +this.doConstruct(capacity,loadFactor); +},"~N,~N"); + +Clazz.defineMethod(c$, "doConstruct", +function(capacity,loadFactor) { +capacity || (capacity = 16); +loadFactor || (loadFactor = 0.75); +if (typeof capacity != "number") { + var map = capacity; + this.loadFactor=loadFactor; + this.elementData=this.newElementArray(map.size()<6?11:map.size()*2); + this.computeMaxSize(); + this.putAllHM(map); + return; +} + +//Clazz.superConstructor(this,java.util.ConcurrentHashMap,[]); +if(capacity>=0&&loadFactor>0){ +this.elementData=this.newElementArray(capacity==0?1:capacity); +this.loadFactor=loadFactor; +this.computeMaxSize(); +}else{ +throw new IllegalArgumentException(); +} +},"~N,~N"); + +//Clazz.makeConstructor(c$, +//function(map){ +//this.construct(map.size()<6?11:map.size()*2); +//Clazz.superCall(this,java.util.ConcurrentHashMap,"putAll",[map]); +//},"java.util.Map"); + +*/ +Clazz.overrideMethod(c$,"clear", +function(){ +if(this.elementCount>0){ +this.elementCount=0; +java.util.Arrays.fill(this.elementData,null); +this.modCount++; +}}); +Clazz.defineMethod(c$,"clone", +function(){ + return this.cloneHM(); +}); + +Clazz.defineMethod(c$,"cloneHM", +function(){ +try{ +var map=this.cloneAM();//Clazz.superCall(this,java.util.concurrent.ConcurrentHashMap,"clone",[]); +map.elementData=this.newElementArray(this.elementData.length); +var entry; +for(var i=0;i=0;){ +var entry=this.elementData[i]; +while(entry!=null){ +if(value.equals(entry.value)){ +return true; +}entry=entry.next; +} +} +}else{ +for(var i=this.elementData.length;--i>=0;){ +var entry=this.elementData[i]; +while(entry!=null){ +if(entry.value==null){ +return true; +}entry=entry.next; +} +} +}return false; +},"~O"); +Clazz.overrideMethod(c$,"entrySet", +function(){ +return new java.util.concurrent.ConcurrentHashMap.ConcurrentHashMapEntrySet(this); +}); +Clazz.overrideMethod(c$,"get", +function(key){ +var m=this.getEntry(key); +if(m!=null){ +return m.value; +}return null; +},"~O"); +Clazz.defineMethod(c$,"getEntry", +function(key){ +var index=this.getModuloHash(key); +return this.findEntry(key,index); +},"~O"); +Clazz.defineMethod(c$,"getModuloHash", +function(key){ +if(key==null){ +return 0; +}return(key.hashCode()&0x7FFFFFFF)%this.elementData.length; +},"~O"); +Clazz.defineMethod(c$,"findEntry", +function(key,index){ +var m; +m=this.elementData[index]; +if(key!=null){ +while(m!=null&&!this.keysEqual(key,m)){ +m=m.next; +} +}else{ +while(m!=null&&m.key!=null){ +m=m.next; +} +}return m; +},"~O,~N"); +Clazz.overrideMethod(c$,"isEmpty", +function(){ +return this.elementCount==0; +}); +Clazz.overrideMethod(c$,"keySet", +function(){ +if(this.$keySet==null){ +this.$keySet=((Clazz.isClassDefined("java.util.concurrent.ConcurrentHashMap$1")?0:java.util.concurrent.ConcurrentHashMap.$ConcurrentHashMap$1$()),Clazz.innerTypeInstance(java.util.concurrent.ConcurrentHashMap$1,this,null)); +}return this.$keySet; +}); +Clazz.overrideMethod(c$,"put", +function(key,value){ +var index=this.getModuloHash(key); +var entry=this.findEntry(key,index); +if(entry==null){ +this.modCount++; +if(++this.elementCount>this.threshold){ +this.rehash(); +index=key==null?0:(key.hashCode()&0x7FFFFFFF)%this.elementData.length; +}entry=this.createEntry(key,index,value); +return null; +}var result=entry.value; +entry.value=value; +return result; +},"~O,~O"); +Clazz.defineMethod(c$,"createEntry", +function(key,index,value){ +var entry=new java.util.concurrent.ConcurrentHashMap.Entry(key,value); +entry.next=this.elementData[index]; +this.elementData[index]=entry; +return entry; +},"~O,~N,~O"); +Clazz.defineMethod(c$,"putAll", +function(map){ +if(!map.isEmpty()){ +var capacity=this.elementCount+map.size(); +if(capacity>this.threshold){ +this.rehash(capacity); +} +this.putAllHM(map); + +}},"java.util.Map"); +Clazz.defineMethod(c$,"rehash", +function(capacity){ +var length=(capacity==0?1:capacity<<1); +var newData=this.newElementArray(length); +for(var i=0;i