Skip to content

Commit

Permalink
Solve ambiguity of delete(int) and delete(Object) for various collect…
Browse files Browse the repository at this point in the history
…ions in javascript.
  • Loading branch information
rgraf committed Apr 2, 2020
1 parent 19bf4f4 commit b684888
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@
<fileset dir="${basedir}/build/package"/>
<manifest>
<attribute name="Main-Class" value="com.greentube.javaconverter.JavaConverter"/>
<attribute name="Implementation-Version" value="2.13.2"/>
<attribute name="Implementation-Version" value="2.13.3"/>
<attribute name="Version-Date" value="${timestamp}"/>
</manifest>
</jar>
Expand Down
9 changes: 8 additions & 1 deletion runtimejs/java/util/AbstractCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,14 @@ _defclass(java_util_AbstractCollection, java_lang_Object, [java_util_Collection]

//iterator abstract - must be implemented by subclass

remove_1: function(o)
// disambiguity
remove_1: function(o)
{
if (typeof o === 'number') { return this.remove_1I(o); }
else { return this.remove_1Ljava_lang_Object$(o); }
},

remove_1Ljava_lang_Object$: function(o)
{
var i = this.iterator_0();
var didremove = false;
Expand Down
2 changes: 1 addition & 1 deletion runtimejs/java/util/AbstractList.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ _defclass(java_util_AbstractList, java_util_AbstractCollection, [java_util_List]
},

// OPTIMIZATION
remove_1: function(obj)
remove_1Ljava_lang_Object$: function(obj)
{
var idx = this.indexOf_1(obj);
if (idx>=0)
Expand Down
4 changes: 2 additions & 2 deletions runtimejs/java/util/HashSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ _defclass(java_util_HashSet, java_util_AbstractCollection, [java_util_Set],
},

// OPTIMIZATION
remove_1: function(key)
remove_1Ljava_lang_Object$: function(key)
{ return this._map.remove_1(key)!==null;
},

Expand All @@ -64,7 +64,7 @@ _defclass(java_util_HashSet, java_util_AbstractCollection, [java_util_Set],
{ var i = collection.iterator_0();
var didremove = false;
while (i.hasNext_0())
{ if (this.remove_1(i.next_0()))
{ if (this.remove_1Ljava_lang_Object$(i.next_0()))
{ didremove = true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/com/greentube/javaconverter/LibraryList.java
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ public class LibraryList
"boolean isEmpty()",
"void forEach(java.util.function.Consumer)",
"java.util.Iterator iterator()",
"java.lang.Object remove(java.lang.Object)",
"boolean remove(java.lang.Object)",
"boolean removeAll(java.util.Collection)",
"boolean retainAll(java.util.Collection)",
"int size()",
Expand Down
38 changes: 35 additions & 3 deletions test/src/com/greentube/convertertest/TestJava4.java
Original file line number Diff line number Diff line change
Expand Up @@ -2311,9 +2311,20 @@ public static void vectortest() {

c = (Vector) v.clone();
c.removeElementAt(2);
c.removeElementAt(1);
c.remove(1);
assertO(c.toString(), "[homer, lisa, meggy]");

c.remove("lisa");
assertO(c.toString(), "[homer, meggy]");
{ AbstractList al= c;
al.remove(0);
}
assertO(c.toString(), "[meggy]");
{
AbstractCollection ac= c;
ac.remove("meggy");
}
assertO(c.toString(), "[]");

c = (Vector) v.clone();
c.set(3, "LISA");
c.setElementAt("MARGE",1);
Expand Down Expand Up @@ -2430,6 +2441,21 @@ public static void arraylisttest() {
assertO(l.toString(),"[alice, bob, corbin, doris, xavier, carl]");
l.remove(l.indexOf("doris"));
assertO(l.toString(),"[alice, bob, corbin, xavier, carl]");

l2 = new ArrayList(Arrays.asList("alice", "bob", "corbin", "xavier", "carl"));
assertO(l2.toString(), "[alice, bob, corbin, xavier, carl]");
l2.remove("corbin");
assertO(l2.toString(), "[alice, bob, xavier, carl]");
{ AbstractList al= l2;
al.remove(0);
}
assertO(l2.toString(), "[bob, xavier, carl]");
{
AbstractCollection ac = l2;
ac.remove("bob");
}
assertO(l2.toString(), "[xavier, carl]");


l2.clear();
l2.add("bob");
Expand Down Expand Up @@ -2493,6 +2519,7 @@ public static void arraylisttest() {
assertO(itt.toString(), "[8, 14]");
itt.remove(Integer.valueOf(8));
assertO(itt.toString(), "[14]");

}


Expand Down Expand Up @@ -2740,6 +2767,8 @@ public static void hashsettest() {
assertB(s.contains(new Integer(44)));
assertB(s.contains(Integer.valueOf(43)));
assertI(s.hashCode(), 91);
s.remove(Byte.valueOf((byte)4));
assertB(!s.contains(Byte.valueOf((byte)4)));
}

public static void linkedlisttest() {
Expand Down Expand Up @@ -2831,7 +2860,10 @@ public static void linkedlisttest() {
Integer e = (Integer) iti.next();
if (e.intValue()%2==1) iti.remove();
}
assertO(itt.toString(), "[8, 14]");
assertO(itt.toString(), "[8, 14]");

itt.remove(Integer.valueOf(8));
assertO(itt.toString(), "[14]");
}

public static void collectionremovaltest()
Expand Down

0 comments on commit b684888

Please sign in to comment.