Skip to content

Commit 180fa74

Browse files
committed
hashtable, HashMap and HashSet
- default is to use JavaScript Map for String keys. - reverts to Java hash table use for the XX(initialCapacity, limitingFactor) constructor specifically. - j2sClazz returns standard JavaScript string for all Java new String(x) except when x is "..." or new String() itself.
1 parent e776862 commit 180fa74

File tree

5 files changed

+72
-14
lines changed

5 files changed

+72
-14
lines changed

sources/net.sf.j2s.java.core/src/java/util/HashMap.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,9 @@ public final V setValue(V newValue) {
300300

301301
@Override
302302
public final boolean equals(Object o) {
303-
return (o == this || o instanceof Map.Entry
303+
return (o != null && (o == this || o instanceof Map.Entry
304304
&& Objects.equals(key, ((Map.Entry<?, ?>) o).getKey())
305-
&& Objects.equals(value, ((Map.Entry<?, ?>) o).getValue()));
305+
&& Objects.equals(value, ((Map.Entry<?, ?>) o).getValue())));
306306
}
307307
}
308308

@@ -424,6 +424,8 @@ static final int tableSizeFor(int cap) {
424424
/* ---------------- Public operations -------------- */
425425

426426
/**
427+
* SwingJS note: This constructor DOES NOT allow JavaScript Map object for HashMap<String,?>.
428+
*
427429
* Constructs an empty <tt>HashMap</tt> with the specified initial capacity and
428430
* load factor.
429431
*
@@ -433,7 +435,6 @@ static final int tableSizeFor(int cap) {
433435
* load factor is nonpositive
434436
*/
435437
public HashMap(int initialCapacity, float loadFactor) {
436-
秘setJS();
437438
if (initialCapacity < 0)
438439
throw new IllegalArgumentException("Illegal initial capacity: " + initialCapacity);
439440
if (initialCapacity > MAXIMUM_CAPACITY)
@@ -445,6 +446,8 @@ public HashMap(int initialCapacity, float loadFactor) {
445446
}
446447

447448
/**
449+
* SwingJS note: This constructor allows JavaScript Map object for HashMap<String,?>.
450+
*
448451
* Constructs an empty <tt>HashMap</tt> with the specified initial capacity and
449452
* the default load factor (0.75).
450453
*
@@ -453,9 +456,12 @@ public HashMap(int initialCapacity, float loadFactor) {
453456
*/
454457
public HashMap(int initialCapacity) {
455458
this(initialCapacity, DEFAULT_LOAD_FACTOR);
459+
秘setJS();
456460
}
457461

458462
/**
463+
* SwingJS note: This constructor allows JavaScript Map object for HashMap<String,?>.
464+
*
459465
* Constructs an empty <tt>HashMap</tt> with the default initial capacity (16)
460466
* and the default load factor (0.75).
461467
*/

sources/net.sf.j2s.java.core/src/java/util/HashSet.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,18 @@ public class HashSet<E>
9898
private static final Object PRESENT = new Object();
9999

100100
/**
101+
* SwingJS note: This constructor allow JavaScript Map object for HashSet<String>.
102+
*
101103
* Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
102104
* default initial capacity (16) and load factor (0.75).
103105
*/
104106
public HashSet() {
105107
map = new HashMap<>();
106-
map.秘m = null;
107108
}
108109

109110
/**
111+
* SwingJS note: This constructor allow JavaScript Map object for HashSet<String>.
112+
*
110113
* Constructs a new set containing the elements in the specified collection. The
111114
* <tt>HashMap</tt> is created with default load factor (0.75) and an initial
112115
* capacity sufficient to contain the elements in the specified collection.
@@ -116,11 +119,12 @@ public HashSet() {
116119
*/
117120
public HashSet(Collection<? extends E> c) {
118121
map = new HashMap<>(Math.max((int) (c.size() / .75f) + 1, 16));
119-
map.秘m = null;
120122
addAll(c);
121123
}
122124

123125
/**
126+
* SwingJS note: This constructor DOES NOT allow JavaScript Map object for HashSet<String>.
127+
*
124128
* Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
125129
* the specified initial capacity and the specified load factor.
126130
*
@@ -131,10 +135,11 @@ public HashSet(Collection<? extends E> c) {
131135
*/
132136
public HashSet(int initialCapacity, float loadFactor) {
133137
map = new HashMap<>(initialCapacity, loadFactor);
134-
map.秘m = null;
135138
}
136139

137140
/**
141+
* SwingJS note: This constructor allows JavaScript Map object for HashSet<String>.
142+
*
138143
* Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
139144
* the specified initial capacity and default load factor (0.75).
140145
*
@@ -147,6 +152,8 @@ public HashSet(int initialCapacity) {
147152
}
148153

149154
/**
155+
* SwingJS note: This constructor DOES NOT allow JavaScript Map object for HashSet<String>.
156+
*
150157
* Constructs a new, empty linked hash set. (This package private
151158
* constructor is only used by LinkedHashSet.) The backing
152159
* HashMap instance is a LinkedHashMap with the specified initial

sources/net.sf.j2s.java.core/src/java/util/Hashtable.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
package java.util;
2727

28-
import java.util.HashMap.Node;
2928
import java.util.function.BiConsumer;
3029
import java.util.function.BiFunction;
3130
import java.util.function.Function;
@@ -170,6 +169,8 @@ public class Hashtable<K,V>
170169
private static final long serialVersionUID = 1421746759512286392L;
171170

172171
/**
172+
* SwingJS note: This constructor DOES NOT allow JavaScript Map object for Hashtable<String,?>.
173+
*
173174
* Constructs a new, empty hashtable with the specified initial
174175
* capacity and the specified load factor.
175176
*
@@ -184,8 +185,6 @@ public Hashtable(int initialCapacity, float loadFactor) {
184185
initialCapacity);
185186
if (loadFactor <= 0 || Float.isNaN(loadFactor))
186187
throw new IllegalArgumentException("Illegal Load: "+loadFactor);
187-
秘setJS();
188-
189188
if (initialCapacity==0)
190189
initialCapacity = 1;
191190
this.loadFactor = loadFactor;
@@ -194,6 +193,8 @@ public Hashtable(int initialCapacity, float loadFactor) {
194193
}
195194

196195
/**
196+
* SwingJS note: This constructor allows JavaScript Map object for Hashtable<String,?>.
197+
*
197198
* Constructs a new, empty hashtable with the specified initial capacity
198199
* and default load factor (0.75).
199200
*
@@ -203,14 +204,18 @@ public Hashtable(int initialCapacity, float loadFactor) {
203204
*/
204205
public Hashtable(int initialCapacity) {
205206
this(initialCapacity, 0.75f);
207+
秘setJS();
206208
}
207209

208210
/**
211+
* SwingJS note: This constructor allows JavaScript Map object for Hashtable<String,?>.
212+
*
209213
* Constructs a new, empty hashtable with a default initial capacity (11)
210214
* and load factor (0.75).
211215
*/
212216
public Hashtable() {
213217
this(11, 0.75f);
218+
秘setJS();
214219
}
215220

216221
/**

sources/net.sf.j2s.java.core/src/test/Test_Map.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ public boolean equals(Object o) {
114114
}
115115
}
116116

117+
static final int hash(Object key) {
118+
int h;
119+
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
120+
}
121+
117122
private static void testSets() {
118123
Set<Object> hs = new HashSet<>();
119124
System.out.println("HashSet should have 6 members:");
@@ -154,6 +159,35 @@ private static void testSets() {
154159
}
155160
System.out.println(hs.size());
156161

162+
String test;
163+
164+
System.out.println("Testing order for new HashSet() -- this will be different than Java for JavaScript in this particular case");
165+
hs = new HashSet<Object>();
166+
hs.add("testing");
167+
hs.add("two");
168+
hs.add("one");
169+
hs.add("three");
170+
test = "";
171+
it = hs.iterator();
172+
while (it.hasNext()) {
173+
test += it.next();
174+
}
175+
System.out.println(test);
176+
assert(test.equals(/** @j2sNative 1 ? "testingtwoonethree":*/"testingonetwothree"));
177+
System.out.println("Testing order for new HashSet(16, 0.75f) -- this will be the same");
178+
hs = new HashSet<Object>(16, 0.75f);
179+
hs.add("testing");
180+
hs.add("two");
181+
hs.add("one");
182+
hs.add("three");
183+
test = "";
184+
it = hs.iterator();
185+
while (it.hasNext()) {
186+
test += it.next();
187+
}
188+
System.out.println(test);
189+
assert(test.equals("testingonetwothree"));
190+
157191
System.out.println("testSets OK");
158192
}
159193

sources/net.sf.j2s.java.core/srcjs/js/j2sClazz.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -358,12 +358,14 @@ Clazz.getClass = function(cl, methodList) {
358358
*/
359359
/* public */
360360
Clazz.instanceOf = function (obj, clazz) {
361+
if (obj == null)
362+
return false;
361363
// allows obj to be a class already, from arrayX.getClass().isInstance(y)
362364
// unwrap java.lang.Class to JavaScript clazz using $clazz$
363365
if (typeof clazz == "string") {
364366
clazz = Clazz._getDeclared(clazz);
365367
}
366-
if (obj == null || !clazz)
368+
if (!clazz)
367369
return false;
368370
if (obj == clazz)
369371
return true;
@@ -4922,6 +4924,9 @@ String(byte[] ascii, int hibyte, int offset, int count)
49224924

49234925
var textDecoder = null;
49244926

4927+
// Note that of all these constructors, only new String("xxx") and new String(new String())
4928+
// return actual JavaScript String objects (as of 3.2.9.v1)
4929+
49254930
String.instantialize=function(){
49264931
var x=arguments[0];
49274932
switch (arguments.length) {
@@ -4934,9 +4939,10 @@ case 1:
49344939
// String(StringBuilder builder)
49354940
// String(String original)
49364941
if (x.__BYTESIZE || x instanceof Array){
4937-
return new String(x.length == 0 ? "" : typeof x[0]=="number" ? Encoding.readUTF8Array(x) : x.join(''));
4942+
return x.length == 0 ? "" : typeof x[0]=="number" ? Encoding.readUTF8Array(x).toString() : x.join('');
49384943
}
4939-
return new String(x.toString());
4944+
// raw JavaScript string unless new String(string)
4945+
return (typeof x == "string" || x instanceof String ? new String(x) : x.toString());
49404946
case 2:
49414947
// String(char[] value, boolean share)
49424948
// String(byte[] ascii, int hibyte)
@@ -4946,7 +4952,7 @@ case 2:
49464952
var hibyte=arguments[1];
49474953
return (typeof hibyte=="number" ? String.instantialize(x,hibyte,0,x.length)
49484954
: typeof hibyte == "boolean" ? x.join('') : self.TextDecoder && (textDecoder || (textDecoder = new TextDecoder())) && arguments[1].toString().toUpperCase() == "UTF-8" ? textDecoder.decode(arguments[0])
4949-
: String.instantialize(x,0,x.length,hibyte));
4955+
: String.instantialize(x,0,x.length,hibyte)).toString();
49504956
case 3:
49514957
// String(byte[] bytes, int offset, int length)
49524958
// String(char[] value, int offset, int count)
@@ -4990,7 +4996,7 @@ case 4:
49904996
var length=arguments[2];
49914997
if (typeof cs == "string") {
49924998
if (",utf8,utf-8,utf_8,".indexOf("," + cs + ",") >= 0)
4993-
return Encoding.readUTF8Array(bytes,offset,length);
4999+
return Encoding.readUTF8Array(bytes,offset,length).toString();
49945000
cs = Clazz.loadClass("java.nio.charset.Charset").forName$S(cs);
49955001
if (!cs)
49965002
throw new java.io.UnsupportedEncodingException();

0 commit comments

Comments
 (0)