mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 08:14:09 -08:00
Extend the class library with the part of Spliterators that is used by Google Guava (#443)
Add class library support for the part of Spliterators that is used by Google Guava.
This commit is contained in:
parent
9d0a53175a
commit
dd255f9da0
|
@ -0,0 +1,120 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2017 Alexey Andreev.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.teavm.classlib.java.util;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.IntConsumer;
|
||||||
|
|
||||||
|
public class TSpliterators {
|
||||||
|
|
||||||
|
private TSpliterators() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> TSpliterator<T> spliterator(Object[] array, int additionalCharacteristics) {
|
||||||
|
return new TSpliterator<T>() {
|
||||||
|
private int index;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean tryAdvance(Consumer<? super T> action) {
|
||||||
|
if (index >= array.length) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
action.accept((T) array[index]);
|
||||||
|
index++;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TSpliterator<T> trySplit() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long estimateSize() {
|
||||||
|
return array.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int characteristics() {
|
||||||
|
return additionalCharacteristics | TSpliterator.SIZED;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> TSpliterator<T> spliterator(Collection<? extends T> c, int characteristics) {
|
||||||
|
return ((TCollection) c).spliterator();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TSpliterator.OfInt spliterator(int[] array, int additionalCharacteristics) {
|
||||||
|
return spliterator(array, 0, array.length, additionalCharacteristics);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TSpliterator.OfInt spliterator(int[] array, int fromIndex, int toIndex,
|
||||||
|
int additionalCharacteristics) {
|
||||||
|
return new TSpliterator.OfInt() {
|
||||||
|
int index = fromIndex;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TSpliterator.OfInt trySplit() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean tryAdvance(IntConsumer action) {
|
||||||
|
if (index < toIndex) {
|
||||||
|
action.accept(array[index]);
|
||||||
|
index++;
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> TSpliterator<T> spliterator(Iterator<? extends T> iterator,
|
||||||
|
long size, int characteristics) {
|
||||||
|
return new TSpliterator<T>() {
|
||||||
|
@Override
|
||||||
|
public boolean tryAdvance(Consumer<? super T> action) {
|
||||||
|
if (iterator.hasNext()) {
|
||||||
|
action.accept(iterator.next());
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TSpliterator<T> trySplit() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long estimateSize() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int characteristics() {
|
||||||
|
return characteristics;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,98 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2015 Alexey Andreev.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.teavm.classlib.java.util;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.IntConsumer;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class SpliteratorsTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void intSpliterator() {
|
||||||
|
int[] values = {1, 2, 3, 4, 5};
|
||||||
|
|
||||||
|
TSpliterator.OfInt spliterator = TSpliterators.spliterator(values, 0, 5, 0);
|
||||||
|
|
||||||
|
List<Integer> collected = new ArrayList<>();
|
||||||
|
IntConsumer collector = value -> collected.add((Integer) value);
|
||||||
|
|
||||||
|
assertTrue(spliterator.tryAdvance(collector));
|
||||||
|
assertTrue(spliterator.tryAdvance(collector));
|
||||||
|
assertTrue(spliterator.tryAdvance(collector));
|
||||||
|
assertTrue(spliterator.tryAdvance(collector));
|
||||||
|
assertTrue(spliterator.tryAdvance(collector));
|
||||||
|
assertFalse(spliterator.tryAdvance(collector));
|
||||||
|
|
||||||
|
assertEquals(Arrays.asList(1, 2, 3, 4, 5), collected);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void intSpliteratorWithSubRange() {
|
||||||
|
int[] values = {1, 2, 3, 4, 5};
|
||||||
|
|
||||||
|
TSpliterator.OfInt spliterator = TSpliterators.spliterator(values, 1, 4, 0);
|
||||||
|
|
||||||
|
List<Integer> collected = new ArrayList<>();
|
||||||
|
IntConsumer collector = value -> collected.add((Integer) value);
|
||||||
|
|
||||||
|
assertTrue(spliterator.tryAdvance(collector));
|
||||||
|
assertTrue(spliterator.tryAdvance(collector));
|
||||||
|
assertTrue(spliterator.tryAdvance(collector));
|
||||||
|
assertFalse(spliterator.tryAdvance(collector));
|
||||||
|
|
||||||
|
assertEquals(Arrays.asList(2, 3, 4), collected);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void spliteratorFromIterator() {
|
||||||
|
List<Integer> values = Arrays.asList(1, 2, 3);
|
||||||
|
|
||||||
|
TSpliterator<Integer> spliterator = TSpliterators.spliterator(values.iterator(),
|
||||||
|
values.size(), 0);
|
||||||
|
|
||||||
|
assertEquals(3L, spliterator.estimateSize());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void spliteratorFromObjectArray() {
|
||||||
|
Object[] array = {1, 2, 3, 4};
|
||||||
|
|
||||||
|
TSpliterator<Integer> spliterator = TSpliterators.spliterator(array, 0);
|
||||||
|
|
||||||
|
List<Object> collected = new ArrayList<>();
|
||||||
|
Consumer<Object> collector = value -> collected.add(value);
|
||||||
|
|
||||||
|
spliterator.tryAdvance(collector);
|
||||||
|
spliterator.tryAdvance(collector);
|
||||||
|
array[2] = 9;
|
||||||
|
spliterator.tryAdvance(collector);
|
||||||
|
spliterator.tryAdvance(collector);
|
||||||
|
spliterator.tryAdvance(collector);
|
||||||
|
|
||||||
|
assertArrayEquals(new Object[] {1, 2, 9, 4}, array);
|
||||||
|
assertEquals(4L, spliterator.estimateSize());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user