Add several unimplemented collection methods

This commit is contained in:
Alexey Andreev 2017-11-19 23:25:40 +03:00
parent 608e62ae3b
commit 5fa48c089a
5 changed files with 53 additions and 17 deletions

View File

@ -18,13 +18,9 @@ package org.teavm.classlib.java.util;
import java.util.Arrays; import java.util.Arrays;
import org.teavm.classlib.java.io.TSerializable; import org.teavm.classlib.java.io.TSerializable;
import org.teavm.classlib.java.lang.*; import org.teavm.classlib.java.lang.*;
import org.teavm.classlib.java.util.function.TUnaryOperator;
import org.teavm.interop.Rename; import org.teavm.interop.Rename;
/**
*
* @author Alexey Andreev
* @param <E>
*/
public class TArrayList<E> extends TAbstractList<E> implements TCloneable, TSerializable { public class TArrayList<E> extends TAbstractList<E> implements TCloneable, TSerializable {
private E[] array; private E[] array;
private int size; private int size;
@ -184,4 +180,11 @@ public class TArrayList<E> extends TAbstractList<E> implements TCloneable, TSeri
throw new TIndexOutOfBoundsException(); throw new TIndexOutOfBoundsException();
} }
} }
@Override
public void replaceAll(TUnaryOperator<E> operator) {
for (int i = 0; i < size; ++i) {
array[i] = operator.apply(array[i]);
}
}
} }

View File

@ -18,6 +18,10 @@ package org.teavm.classlib.java.util;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.util.Objects; import java.util.Objects;
import java.util.RandomAccess; 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.TClass;
import org.teavm.classlib.java.lang.TComparable; import org.teavm.classlib.java.lang.TComparable;
import org.teavm.classlib.java.lang.TDouble; import org.teavm.classlib.java.lang.TDouble;
@ -1600,4 +1604,28 @@ public class TArrays extends TObject {
} }
return new TArrayDoubleStreamImpl(array, startInclusive, endExclusive); return new TArrayDoubleStreamImpl(array, startInclusive, endExclusive);
} }
public static <T> void setAll(T[] array, IntFunction<? extends T> 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);
}
}
} }

View File

@ -89,9 +89,6 @@ public class TLinkedList<E> extends TAbstractSequentialList<E> implements TDeque
@Override @Override
public boolean offer(E e) { public boolean offer(E e) {
if (e == null) {
throw new IllegalArgumentException("Element can't be null");
}
Entry<E> entry = new Entry<>(); Entry<E> entry = new Entry<>();
entry.item = e; entry.item = e;
entry.next = firstEntry; entry.next = firstEntry;
@ -108,11 +105,10 @@ public class TLinkedList<E> extends TAbstractSequentialList<E> implements TDeque
@Override @Override
public E remove() { public E remove() {
E elem = poll(); if (isEmpty()) {
if (elem == null) {
throw new TNoSuchElementException(); throw new TNoSuchElementException();
} }
return elem; return poll();
} }
@Override @Override
@ -152,9 +148,6 @@ public class TLinkedList<E> extends TAbstractSequentialList<E> implements TDeque
@Override @Override
public void addLast(E e) { public void addLast(E e) {
if (e == null) {
throw new IllegalArgumentException("Element can't be null");
}
Entry<E> entry = new Entry<>(); Entry<E> entry = new Entry<>();
entry.item = e; entry.item = e;
entry.previous = lastEntry; entry.previous = lastEntry;
@ -187,11 +180,10 @@ public class TLinkedList<E> extends TAbstractSequentialList<E> implements TDeque
@Override @Override
public E removeLast() { public E removeLast() {
E elem = pollLast(); if (isEmpty()) {
if (elem == null) {
throw new TNoSuchElementException(); throw new TNoSuchElementException();
} }
return elem; return pollLast();
} }
@Override @Override

View File

@ -15,6 +15,8 @@
*/ */
package org.teavm.classlib.java.util; package org.teavm.classlib.java.util;
import org.teavm.classlib.java.util.function.TUnaryOperator;
public interface TList<E> extends TCollection<E> { public interface TList<E> extends TCollection<E> {
boolean addAll(int index, TCollection<? extends E> c); boolean addAll(int index, TCollection<? extends E> c);
@ -36,6 +38,13 @@ public interface TList<E> extends TCollection<E> {
TList<E> subList(int fromIndex, int toIndex); TList<E> subList(int fromIndex, int toIndex);
default void replaceAll(TUnaryOperator<E> operator) {
TListIterator<E> iter = listIterator();
while (iter.hasNext()) {
iter.set(operator.apply(iter.next()));
}
}
default void sort(TComparator<? super E> c) { default void sort(TComparator<? super E> c) {
TCollections.sort(this, c); TCollections.sort(this, c);
} }

View File

@ -52,6 +52,10 @@ public final class TObjects extends TObject {
return obj; return obj;
} }
public static boolean nonNull(Object obj) {
return obj != null;
}
public static boolean deepEquals(Object a, Object b) { public static boolean deepEquals(Object a, Object b) {
if (a == b) { if (a == b) {
return true; return true;