diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TArrayList.java b/classlib/src/main/java/org/teavm/classlib/java/util/TArrayList.java index 094801130..f5b31f338 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TArrayList.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TArrayList.java @@ -18,13 +18,9 @@ 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.classlib.java.util.function.TUnaryOperator; import org.teavm.interop.Rename; -/** - * - * @author Alexey Andreev - * @param - */ public class TArrayList extends TAbstractList implements TCloneable, TSerializable { private E[] array; private int size; @@ -184,4 +180,11 @@ public class TArrayList extends TAbstractList implements TCloneable, TSeri throw new TIndexOutOfBoundsException(); } } + + @Override + public void replaceAll(TUnaryOperator operator) { + for (int i = 0; i < size; ++i) { + array[i] = operator.apply(array[i]); + } + } } diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TArrays.java b/classlib/src/main/java/org/teavm/classlib/java/util/TArrays.java index 4a397b9ed..359ac5283 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TArrays.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TArrays.java @@ -18,6 +18,10 @@ package org.teavm.classlib.java.util; import java.lang.reflect.Array; import java.util.Objects; import java.util.RandomAccess; +import java.util.function.IntFunction; +import java.util.function.IntToDoubleFunction; +import java.util.function.IntToLongFunction; +import java.util.function.IntUnaryOperator; import org.teavm.classlib.java.lang.TClass; import org.teavm.classlib.java.lang.TComparable; import org.teavm.classlib.java.lang.TDouble; @@ -1600,4 +1604,28 @@ public class TArrays extends TObject { } return new TArrayDoubleStreamImpl(array, startInclusive, endExclusive); } + + public static void setAll(T[] array, IntFunction generator) { + for (int i = 0; i < array.length; ++i) { + array[i] = generator.apply(i); + } + } + + public static void setAll(int[] array, IntUnaryOperator generator) { + for (int i = 0; i < array.length; ++i) { + array[i] = generator.applyAsInt(i); + } + } + + public static void setAll(long[] array, IntToLongFunction generator) { + for (int i = 0; i < array.length; ++i) { + array[i] = generator.applyAsLong(i); + } + } + + public static void setAll(double[] array, IntToDoubleFunction generator) { + for (int i = 0; i < array.length; ++i) { + array[i] = generator.applyAsDouble(i); + } + } } 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 569a38aca..2fa25925f 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 @@ -89,9 +89,6 @@ public class TLinkedList extends TAbstractSequentialList implements TDeque @Override public boolean offer(E e) { - if (e == null) { - throw new IllegalArgumentException("Element can't be null"); - } Entry entry = new Entry<>(); entry.item = e; entry.next = firstEntry; @@ -108,11 +105,10 @@ public class TLinkedList extends TAbstractSequentialList implements TDeque @Override public E remove() { - E elem = poll(); - if (elem == null) { + if (isEmpty()) { throw new TNoSuchElementException(); } - return elem; + return poll(); } @Override @@ -152,9 +148,6 @@ public class TLinkedList extends TAbstractSequentialList implements TDeque @Override public void addLast(E e) { - if (e == null) { - throw new IllegalArgumentException("Element can't be null"); - } Entry entry = new Entry<>(); entry.item = e; entry.previous = lastEntry; @@ -187,11 +180,10 @@ public class TLinkedList extends TAbstractSequentialList implements TDeque @Override public E removeLast() { - E elem = pollLast(); - if (elem == null) { + if (isEmpty()) { throw new TNoSuchElementException(); } - return elem; + return pollLast(); } @Override diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TList.java b/classlib/src/main/java/org/teavm/classlib/java/util/TList.java index 0b8351d26..a8aae61ec 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TList.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TList.java @@ -15,6 +15,8 @@ */ package org.teavm.classlib.java.util; +import org.teavm.classlib.java.util.function.TUnaryOperator; + public interface TList extends TCollection { boolean addAll(int index, TCollection c); @@ -36,6 +38,13 @@ public interface TList extends TCollection { TList subList(int fromIndex, int toIndex); + default void replaceAll(TUnaryOperator operator) { + TListIterator iter = listIterator(); + while (iter.hasNext()) { + iter.set(operator.apply(iter.next())); + } + } + default void sort(TComparator c) { TCollections.sort(this, c); } diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TObjects.java b/classlib/src/main/java/org/teavm/classlib/java/util/TObjects.java index 7deab763b..fe405d927 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TObjects.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TObjects.java @@ -52,6 +52,10 @@ public final class TObjects extends TObject { return obj; } + public static boolean nonNull(Object obj) { + return obj != null; + } + public static boolean deepEquals(Object a, Object b) { if (a == b) { return true;