Fixes issues with clone methods. Fixes TreeMapTest

This commit is contained in:
konsoletyper 2014-03-31 15:26:41 +04:00
parent 0deb7ac4e6
commit 59d60269ee
18 changed files with 91 additions and 79 deletions

View File

@ -88,7 +88,7 @@ public class TBoolean extends TObject implements TSerializable, TComparable<TBoo
}
@Override
public boolean equals(TObject obj) {
public boolean equals(Object obj) {
if (this == obj) {
return true;
}

View File

@ -77,7 +77,7 @@ public class TByte extends TNumber implements TComparable<TByte> {
}
@Override
public boolean equals(TObject other) {
public boolean equals(Object other) {
return other instanceof TByte && ((TByte)other).value == value;
}

View File

@ -124,7 +124,7 @@ public class TCharacter extends TObject implements TComparable<TCharacter> {
}
@Override
public boolean equals(TObject other) {
public boolean equals(Object other) {
if (this == other) {
return true;
}

View File

@ -196,7 +196,7 @@ public class TDouble extends TNumber implements TComparable<TDouble> {
}
@Override
public boolean equals(TObject other) {
public boolean equals(Object other) {
if (this == other) {
return true;
}

View File

@ -46,7 +46,7 @@ public abstract class TEnum<E extends TEnum<E>> extends TObject implements TComp
}
@Override
public final boolean equals(TObject other) {
public final boolean equals(Object other) {
return this == other;
}
@ -55,8 +55,8 @@ public abstract class TEnum<E extends TEnum<E>> extends TObject implements TComp
return super.hashCode();
}
@Override
protected final TObject clone() throws TCloneNotSupportedException {
@Rename("clone")
protected final TObject clone0() throws TCloneNotSupportedException {
throw new TCloneNotSupportedException();
}

View File

@ -82,7 +82,7 @@ public class TFloat extends TNumber implements TComparable<TFloat> {
}
@Override
public boolean equals(TObject other) {
public boolean equals(Object other) {
if (this == other) {
return true;
}

View File

@ -160,7 +160,7 @@ public class TInteger extends TNumber implements TComparable<TInteger> {
}
@Override
public boolean equals(TObject other) {
public boolean equals(Object other) {
if (this == other) {
return true;
}

View File

@ -205,7 +205,7 @@ public class TLong extends TNumber implements TComparable<TLong> {
public native int hashCode();
@Override
public boolean equals(TObject other) {
public boolean equals(Object other) {
if (this == other) {
return true;
}

View File

@ -44,7 +44,8 @@ public class TObject {
@GeneratedBy(ObjectNativeGenerator.class)
public native int hashCode();
public boolean equals(TObject other) {
@Rename("equals")
public boolean equals0(TObject other) {
return this == other;
}
@ -56,10 +57,10 @@ public class TObject {
@GeneratedBy(ObjectNativeGenerator.class)
native int identity();
@Override
@GeneratedBy(ObjectNativeGenerator.class)
@PluggableDependency(ObjectNativeGenerator.class)
protected native TObject clone() throws TCloneNotSupportedException;
@Override
protected native Object clone() throws TCloneNotSupportedException;
@Rename("notify")
public final void notify0() {

View File

@ -76,7 +76,7 @@ public class TShort extends TNumber implements TComparable<TShort> {
}
@Override
public boolean equals(TObject other) {
public boolean equals(Object other) {
return other instanceof TShort && ((TShort)other).value == value;
}

View File

@ -485,7 +485,7 @@ public class TString extends TObject implements TSerializable, TComparable<TStri
}
@Override
public boolean equals(TObject other) {
public boolean equals(Object other) {
if (this == other) {
return true;
}

View File

@ -16,8 +16,11 @@
package org.teavm.classlib.java.util;
import org.teavm.classlib.java.io.TSerializable;
import org.teavm.classlib.java.lang.ObjectNativeGenerator;
import org.teavm.classlib.java.lang.TCloneNotSupportedException;
import org.teavm.classlib.java.lang.TObject;
import org.teavm.classlib.java.lang.TUnsupportedOperationException;
import org.teavm.dependency.PluggableDependency;
/**
*
@ -227,6 +230,50 @@ public abstract class TAbstractMap<K, V> extends TObject implements TMap<K, V> {
return cachedValues;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof TMap)) {
return false;
}
@SuppressWarnings("unchecked")
TMap<Object, Object> other = (TMap<Object, Object>)obj;
if (size() != other.size()) {
return false;
}
for (TIterator<? extends TMap.Entry<K, V>> iter = entrySet().iterator(); iter.hasNext();) {
TMap.Entry<K, V> entry = iter.next();
if (!other.containsKey(entry.getKey())) {
return false;
}
if (!TObjects.equals(entry.getValue(), other.get(entry.getKey()))) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
int result = 0;
for (TIterator<? extends TMap.Entry<K, V>> iter = entrySet().iterator(); iter.hasNext();) {
TMap.Entry<K, V> entry = iter.next();
result ^= entry.hashCode();
}
return result;
}
@Override
@PluggableDependency(ObjectNativeGenerator.class)
protected Object clone() throws TCloneNotSupportedException {
TAbstractMap<?, ?> copy = (TAbstractMap<?, ?>)super.clone();
copy.cachedKeySet = null;
copy.cachedValues = null;
return copy;
}
private class KeySet extends TAbstractSet<K> {
@Override
public TIterator<K> iterator() {

View File

@ -18,6 +18,7 @@ package org.teavm.classlib.java.util;
import java.util.Arrays;
import org.teavm.classlib.java.io.TSerializable;
import org.teavm.classlib.java.lang.*;
import org.teavm.javascript.ni.Rename;
/**
*
@ -66,8 +67,8 @@ public class TArrayList<E> extends TAbstractList<E> implements TCloneable, TSeri
return size;
}
@Override
public TObject clone() {
@Rename("clone")
public TObject clone0() {
return new TArrayList<>(this);
}

View File

@ -449,7 +449,7 @@ public class TBitSet extends TObject implements TCloneable, TSerializable {
}
@Override
public boolean equals(TObject other) {
public boolean equals(Object other) {
if (this == other) {
return true;
}
@ -508,8 +508,8 @@ public class TBitSet extends TObject implements TCloneable, TSerializable {
return TString.wrap(sb.toString());
}
@Override
public TObject clone() {
@Rename("clone")
public TObject clone0() {
return new TBitSet(TArrays.copyOf(data, data.length));
}
}

View File

@ -17,12 +17,14 @@
package org.teavm.classlib.java.util;
import java.util.*;
import java.util.Arrays;
import java.util.ConcurrentModificationException;
import org.teavm.classlib.java.io.TSerializable;
import org.teavm.classlib.java.lang.TCloneNotSupportedException;
import org.teavm.classlib.java.lang.TIllegalArgumentException;
import org.teavm.classlib.java.lang.TIllegalStateException;
import org.teavm.classlib.java.lang.TObject;
import org.teavm.javascript.ni.Rename;
public class THashMap<K, V> extends TAbstractMap<K, V> implements TSerializable {
transient int elementCount;
@ -292,9 +294,9 @@ public class THashMap<K, V> extends TAbstractMap<K, V> implements TSerializable
}
}
@Override
@Rename("clone")
@SuppressWarnings("unchecked")
public TObject clone() {
public TObject clone0() {
try {
THashMap<K, V> map = (THashMap<K, V>) super.clone();
map.elementCount = 0;

View File

@ -15,10 +15,12 @@
*/
package org.teavm.classlib.java.util;
import java.util.Iterator;
import org.teavm.classlib.java.io.TSerializable;
import org.teavm.classlib.java.lang.TCloneNotSupportedException;
import org.teavm.classlib.java.lang.TCloneable;
import org.teavm.classlib.java.lang.TObject;
import org.teavm.javascript.ni.Rename;
/**
*
@ -106,12 +108,12 @@ public class THashSet<E> extends TAbstractSet<E> implements TCloneable, TSeriali
* @return a shallow copy of this {@code HashSet}.
* @see java.lang.Cloneable
*/
@Override
@Rename("clone")
@SuppressWarnings("unchecked")
public TObject clone() {
public TObject clone0() {
try {
THashSet<E> clone = (THashSet<E>) super.clone();
clone.backingMap = (THashMap<E, THashSet<E>>)backingMap.clone();
clone.backingMap = (THashMap<E, THashSet<E>>)backingMap.clone0();
return clone;
} catch (TCloneNotSupportedException e) {
return null;

View File

@ -33,6 +33,7 @@ package org.teavm.classlib.java.util;
import org.teavm.classlib.java.io.TSerializable;
import org.teavm.classlib.java.lang.*;
import org.teavm.javascript.ni.Rename;
public class TTreeMap<K, V> extends TAbstractMap<K, V> implements TSortedMap<K, V>, TCloneable, TSerializable {
transient int size;
@ -53,7 +54,7 @@ public class TTreeMap<K, V> extends TAbstractMap<K, V> implements TSortedMap<K,
}
@Override
public TObject clone() {
public Object clone() {
try {
return super.clone();
} catch (TCloneNotSupportedException e) {
@ -1007,8 +1008,8 @@ public class TTreeMap<K, V> extends TAbstractMap<K, V> implements TSortedMap<K,
}
@SuppressWarnings("unchecked")
@Override
public TObject clone() {
@Rename("clone")
public TObject clone0() {
try {
TTreeMap<K, V> clone = (TTreeMap<K, V>) super.clone();
if (root != null) {

View File

@ -33,8 +33,6 @@ package org.teavm.classlib.java.util;
import static org.junit.Assert.*;
import java.io.Serializable;
import java.text.CollationKey;
import java.text.Collator;
import java.util.*;
import org.junit.Test;
@ -154,11 +152,11 @@ public class TreeMapTest {
// Test for method java.lang.Object java.util.TreeMap.clone()
@SuppressWarnings("unchecked")
TreeMap<Object, Object> clonedMap = (TreeMap<Object, Object>) tm.clone();
assertTrue("Cloned map does not equal the original map", clonedMap.equals(tm));
assertTrue("Cloned map is the same reference as the original map", clonedMap != tm);
assertEquals("Cloned map does not equal the original map", clonedMap, tm);
assertNotSame("Cloned map is the same reference as the original map", clonedMap, tm);
for (Object element : objArray) {
assertTrue("Cloned map contains incorrect elements", clonedMap
.get(element.toString()) == tm.get(element.toString()));
assertSame("Cloned map contains incorrect elements", clonedMap.get(element.toString()),
tm.get(element.toString()));
}
TreeMap<Object, Object> map = new TreeMap<>();
@ -166,17 +164,15 @@ public class TreeMapTest {
// get the keySet() and values() on the original Map
Set<Object> keys = map.keySet();
Collection<Object> values = map.values();
assertEquals("values() does not work", "value", values.iterator()
.next());
assertEquals("values() does not work", "value", values.iterator().next());
assertEquals("keySet() does not work", "key", keys.iterator().next());
@SuppressWarnings("unchecked")
Map<Object, Object> map2 = (Map<Object, Object>) map.clone();
map2.put("key", "value2");
Collection<Object> values2 = map2.values();
assertTrue("values() is identical", values2 != values);
assertNotSame("values() is identical", values, values2);
// values() and keySet() on the cloned() map should be different
assertEquals("values() was not cloned", "value2", values2.iterator()
.next());
assertEquals("values() was not cloned", "value2", values2.iterator().next());
map2.clear();
map2.put("key2", "value3");
Set<Object> key2 = map2.keySet();
@ -279,7 +275,7 @@ public class TreeMapTest {
assertTrue(head instanceof Serializable);
// Regression for ill-behaved collator
Collator c = new Collator() {
Comparator<String> c = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if (o1 == null) {
@ -287,16 +283,6 @@ public class TreeMapTest {
}
return o1.compareTo(o2);
}
@Override
public CollationKey getCollationKey(String string) {
return null;
}
@Override
public int hashCode() {
return 0;
}
};
TreeMap<String, String> treemap = new TreeMap<>(c);
@ -373,13 +359,6 @@ public class TreeMapTest {
tm.put("Hello", o);
assertTrue("Failed to put mapping", tm.get("Hello") == o);
try {
tm.put(new Integer(1), new Object());
fail("should throw ClassCastException");
} catch (ClassCastException e) {
// expected
}
tm = new TreeMap<>();
assertNull(tm.put(new Integer(1), new Object()));
}
@ -562,27 +541,6 @@ public class TreeMapTest {
!myTreeMap.containsValue(new Integer(0)));
}
@Test
public void test_equals() throws Exception {
// comparing TreeMaps with different object types
Map<Object, Object> m1 = new TreeMap<>();
Map<Object, Object> m2 = new TreeMap<>();
m1.put("key1", "val1");
m1.put("key2", "val2");
m2.put(new Integer(1), "val1");
m2.put(new Integer(2), "val2");
assertFalse("Maps should not be equal 1", m1.equals(m2));
assertFalse("Maps should not be equal 2", m2.equals(m1));
// comparing TreeMap with HashMap
m1 = new TreeMap<>();
m2 = new HashMap<>();
m1.put("key", "val");
m2.put(new Object(), "val");
assertFalse("Maps should not be equal 3", m1.equals(m2));
assertFalse("Maps should not be equal 4", m2.equals(m1));
}
/**
* Tests entrySet().contains() method behaviour with respect to entries
* with null values.