classlib: implement Cloneable for TreeSet

This commit is contained in:
Bernd Busse 2023-07-27 21:00:51 +02:00 committed by Alexey Andreev
parent 6a398c9b8d
commit 6de4560282

View File

@ -15,12 +15,17 @@
*/ */
package org.teavm.classlib.java.util; package org.teavm.classlib.java.util;
import org.teavm.classlib.java.lang.TCloneNotSupportedException;
import org.teavm.classlib.java.lang.TCloneable;
import org.teavm.classlib.java.lang.TObject;
import org.teavm.interop.Rename;
/** /**
* *
* @author Alexey Andreev * @author Alexey Andreev
* @param <E> * @param <E>
*/ */
public class TTreeSet<E> extends TAbstractSet<E> implements TNavigableSet<E> { public class TTreeSet<E> extends TAbstractSet<E> implements TCloneable, TNavigableSet<E> {
private static final Object VALUE = new Object(); private static final Object VALUE = new Object();
private TTreeMap<E, Object> map; private TTreeMap<E, Object> map;
@ -66,6 +71,25 @@ public class TTreeSet<E> extends TAbstractSet<E> implements TNavigableSet<E> {
return map.keySet().iterator(); return map.keySet().iterator();
} }
/**
* Returns a new {@code TreeSet} with the same elements and size as this
* {@code TreeSet}.
*
* @return a shallow copy of this {@code TreeSet}.
* @see java.lang.Cloneable
*/
@Rename("clone")
@SuppressWarnings("unchecked")
public TObject clone0() {
try {
TTreeSet<E> clone = (TTreeSet<E>) super.clone();
clone.map = (TTreeMap<E, Object>) map.clone();
return clone;
} catch (TCloneNotSupportedException e) {
return null;
}
}
@Override @Override
public void clear() { public void clear() {
map.clear(); map.clear();
@ -167,4 +191,9 @@ public class TTreeSet<E> extends TAbstractSet<E> implements TNavigableSet<E> {
public TNavigableSet<E> tailSet(E fromElement, boolean inclusive) { public TNavigableSet<E> tailSet(E fromElement, boolean inclusive) {
return map.tailMap(fromElement, inclusive).navigableKeySet(); return map.tailMap(fromElement, inclusive).navigableKeySet();
} }
@Override
public Object clone() {
return new TTreeSet<>(this);
}
} }