From c8585426053696668c0728313946740682b6f70a Mon Sep 17 00:00:00 2001 From: ihromant Date: Mon, 25 Sep 2023 15:58:45 +0300 Subject: [PATCH] classlib: implement general reversed ListIterator --- .../teavm/classlib/java/util/TLinkedList.java | 96 +------------------ .../classlib/java/util/TReversedList.java | 65 +++++++++++++ 2 files changed, 67 insertions(+), 94 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TLinkedList.java b/classlib/src/main/java/org/teavm/classlib/java/util/TLinkedList.java index 0d13bd1b7..27ab9d7d6 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TLinkedList.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TLinkedList.java @@ -529,53 +529,7 @@ public class TLinkedList extends TAbstractSequentialList implements TDeque @Override public TListIterator listIterator() { - TListIterator lit = list.listIterator(list.size()); - return new TListIterator<>() { - @Override - public boolean hasPrevious() { - return lit.hasNext(); - } - - @Override - public E previous() { - return lit.next(); - } - - @Override - public int nextIndex() { - return list.size() - lit.previousIndex() - 1; - } - - @Override - public int previousIndex() { - return list.size() - lit.nextIndex() - 1; - } - - @Override - public void set(E e) { - lit.set(e); - } - - @Override - public void add(E e) { - lit.add(e); - } - - @Override - public boolean hasNext() { - return lit.hasPrevious(); - } - - @Override - public E next() { - return lit.previous(); - } - - @Override - public void remove() { - lit.remove(); - } - }; + return reversed.listIterator(); } @Override @@ -615,53 +569,7 @@ public class TLinkedList extends TAbstractSequentialList implements TDeque @Override public TListIterator listIterator(int index) { - TListIterator lit = list.listIterator(list.size() - index); - return new TListIterator<>() { - @Override - public boolean hasPrevious() { - return lit.hasNext(); - } - - @Override - public E previous() { - return lit.next(); - } - - @Override - public int nextIndex() { - return list.size() - lit.previousIndex() - 1; - } - - @Override - public int previousIndex() { - return list.size() - lit.nextIndex() - 1; - } - - @Override - public void set(E e) { - lit.set(e); - } - - @Override - public void add(E e) { - lit.add(e); - } - - @Override - public boolean hasNext() { - return lit.hasPrevious(); - } - - @Override - public E next() { - return lit.previous(); - } - - @Override - public void remove() { - lit.remove(); - } - }; + return reversed.listIterator(index); } @Override diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TReversedList.java b/classlib/src/main/java/org/teavm/classlib/java/util/TReversedList.java index 1f7364a20..67f83235d 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TReversedList.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TReversedList.java @@ -98,6 +98,16 @@ class TReversedList extends TAbstractList { }; } + @Override + public TListIterator listIterator() { + return new ReversedListIterator<>(this, base.listIterator(base.size())); + } + + @Override + public TListIterator listIterator(int idx) { + return new ReversedListIterator<>(this, base.listIterator(base.size() - idx)); + } + @Override public void clear() { base.clear(); @@ -118,4 +128,59 @@ class TReversedList extends TAbstractList { super(base); } } + + private static class ReversedListIterator implements TListIterator { + private final TList list; + private final TListIterator lit; + + private ReversedListIterator(TList list, TListIterator lit) { + this.list = list; + this.lit = lit; + } + + @Override + public boolean hasPrevious() { + return lit.hasNext(); + } + + @Override + public E previous() { + return lit.next(); + } + + @Override + public int nextIndex() { + return list.size() - lit.previousIndex() - 1; + } + + @Override + public int previousIndex() { + return list.size() - lit.nextIndex() - 1; + } + + @Override + public void set(E e) { + lit.set(e); + } + + @Override + public void add(E e) { + lit.add(e); + } + + @Override + public boolean hasNext() { + return lit.hasPrevious(); + } + + @Override + public E next() { + return lit.previous(); + } + + @Override + public void remove() { + lit.remove(); + } + } }