From 6de45602825b1fa4a0eefc6f1f9504cd8626394d Mon Sep 17 00:00:00 2001 From: Bernd Busse Date: Thu, 27 Jul 2023 21:00:51 +0200 Subject: [PATCH] classlib: implement Cloneable for TreeSet --- .../teavm/classlib/java/util/TTreeSet.java | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TTreeSet.java b/classlib/src/main/java/org/teavm/classlib/java/util/TTreeSet.java index de6f6cb68..30605ac50 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TTreeSet.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TTreeSet.java @@ -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 */ -public class TTreeSet extends TAbstractSet implements TNavigableSet { +public class TTreeSet extends TAbstractSet implements TCloneable, TNavigableSet { private static final Object VALUE = new Object(); private TTreeMap map; @@ -66,6 +71,25 @@ public class TTreeSet extends TAbstractSet implements TNavigableSet { 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 clone = (TTreeSet) super.clone(); + clone.map = (TTreeMap) map.clone(); + return clone; + } catch (TCloneNotSupportedException e) { + return null; + } + } + @Override public void clear() { map.clear(); @@ -167,4 +191,9 @@ public class TTreeSet extends TAbstractSet implements TNavigableSet { public TNavigableSet tailSet(E fromElement, boolean inclusive) { return map.tailMap(fromElement, inclusive).navigableKeySet(); } + + @Override + public Object clone() { + return new TTreeSet<>(this); + } }