Skip to content

Commit

Permalink
classlib: implement Cloneable for TreeSet
Browse files Browse the repository at this point in the history
  • Loading branch information
tryone144 committed Jan 23, 2024
1 parent b3ad7af commit 5d3d39b
Showing 1 changed file with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@
*/
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
* @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 TTreeMap<E, Object> map;

Expand Down Expand Up @@ -66,6 +71,25 @@ public TIterator<E> 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
public void clear() {
map.clear();
Expand Down Expand Up @@ -167,4 +191,9 @@ public TNavigableSet<E> headSet(E toElement, boolean inclusive) {
public TNavigableSet<E> tailSet(E fromElement, boolean inclusive) {
return map.tailMap(fromElement, inclusive).navigableKeySet();
}

@Override
public Object clone() {
return new TTreeSet<>(this);
}
}

0 comments on commit 5d3d39b

Please sign in to comment.