com.landawn.abacus.util.stream.IteratorFloatStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-android Show documentation
Show all versions of abacus-android Show documentation
A general programming library in Java
/*
* Copyright (C) 2016 HaiYang Li
*
* 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 com.landawn.abacus.util.stream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import com.landawn.abacus.util.DoubleIterator;
import com.landawn.abacus.util.FloatIterator;
import com.landawn.abacus.util.FloatList;
import com.landawn.abacus.util.FloatSummaryStatistics;
import com.landawn.abacus.util.IntIterator;
import com.landawn.abacus.util.LongIterator;
import com.landawn.abacus.util.LongMultiset;
import com.landawn.abacus.util.Multimap;
import com.landawn.abacus.util.Multiset;
import com.landawn.abacus.util.N;
import com.landawn.abacus.util.NullabLe;
import com.landawn.abacus.util.OptionalFloat;
import com.landawn.abacus.util.function.BiConsumer;
import com.landawn.abacus.util.function.BiFunction;
import com.landawn.abacus.util.function.BinaryOperator;
import com.landawn.abacus.util.function.Consumer;
import com.landawn.abacus.util.function.FloatBinaryOperator;
import com.landawn.abacus.util.function.FloatConsumer;
import com.landawn.abacus.util.function.FloatFunction;
import com.landawn.abacus.util.function.FloatPredicate;
import com.landawn.abacus.util.function.FloatToDoubleFunction;
import com.landawn.abacus.util.function.FloatToIntFunction;
import com.landawn.abacus.util.function.FloatToLongFunction;
import com.landawn.abacus.util.function.FloatUnaryOperator;
import com.landawn.abacus.util.function.ObjFloatConsumer;
import com.landawn.abacus.util.function.Supplier;
import com.landawn.abacus.util.function.ToFloatFunction;
/**
* This class is a sequential, stateful and immutable stream implementation.
*
* @since 0.8
*
* @author Haiyang Li
*/
class IteratorFloatStream extends AbstractFloatStream {
final ExFloatIterator elements;
OptionalFloat head;
FloatStream tail;
FloatStream head2;
OptionalFloat tail2;
IteratorFloatStream(final FloatIterator values) {
this(values, null);
}
IteratorFloatStream(final FloatIterator values, final Collection closeHandlers) {
this(values, closeHandlers, false);
}
IteratorFloatStream(final FloatIterator values, final Collection closeHandlers, final boolean sorted) {
super(closeHandlers, sorted);
ExFloatIterator tmp = null;
if (values instanceof ExFloatIterator) {
tmp = (ExFloatIterator) values;
} else if (values instanceof SkippableIterator) {
tmp = new ExFloatIterator() {
@Override
public boolean hasNext() {
return values.hasNext();
}
@Override
public float nextFloat() {
return values.nextFloat();
}
@Override
public void skip(long n) {
((SkippableIterator) values).skip(n);
}
@Override
public long count() {
return ((SkippableIterator) values).count();
}
};
} else {
tmp = new ExFloatIterator() {
@Override
public boolean hasNext() {
return values.hasNext();
}
@Override
public float nextFloat() {
return values.nextFloat();
}
};
}
this.elements = tmp;
}
@Override
public FloatStream filter(final FloatPredicate predicate) {
return new IteratorFloatStream(new ExFloatIterator() {
private boolean hasNext = false;
private float next = 0;
@Override
public boolean hasNext() {
if (hasNext == false) {
while (elements.hasNext()) {
next = elements.nextFloat();
if (predicate.test(next)) {
hasNext = true;
break;
}
}
}
return hasNext;
}
@Override
public float nextFloat() {
if (hasNext == false && hasNext() == false) {
throw new NoSuchElementException();
}
hasNext = false;
return next;
}
}, closeHandlers, sorted);
}
@Override
public FloatStream takeWhile(final FloatPredicate predicate) {
return new IteratorFloatStream(new ExFloatIterator() {
private boolean hasMore = true;
private boolean hasNext = false;
private float next = 0;
@Override
public boolean hasNext() {
if (hasNext == false && hasMore && elements.hasNext()) {
next = elements.nextFloat();
if (predicate.test(next)) {
hasNext = true;
} else {
hasMore = false;
}
}
return hasNext;
}
@Override
public float nextFloat() {
if (hasNext == false && hasNext() == false) {
throw new NoSuchElementException();
}
hasNext = false;
return next;
}
}, closeHandlers, sorted);
}
@Override
public FloatStream dropWhile(final FloatPredicate predicate) {
return new IteratorFloatStream(new ExFloatIterator() {
private boolean hasNext = false;
private float next = 0;
private boolean dropped = false;
@Override
public boolean hasNext() {
if (hasNext == false) {
if (dropped == false) {
while (elements.hasNext()) {
next = elements.nextFloat();
if (predicate.test(next) == false) {
hasNext = true;
break;
}
}
dropped = true;
} else if (elements.hasNext()) {
next = elements.nextFloat();
hasNext = true;
}
}
return hasNext;
}
@Override
public float nextFloat() {
if (hasNext == false && hasNext() == false) {
throw new NoSuchElementException();
}
hasNext = false;
return next;
}
}, closeHandlers, sorted);
}
@Override
public FloatStream map(final FloatUnaryOperator mapper) {
return new IteratorFloatStream(new ExFloatIterator() {
@Override
public boolean hasNext() {
return elements.hasNext();
}
@Override
public float nextFloat() {
return mapper.applyAsFloat(elements.nextFloat());
}
@Override
public long count() {
return elements.count();
}
@Override
public void skip(long n) {
elements.skip(n);
}
}, closeHandlers);
}
@Override
public IntStream mapToInt(final FloatToIntFunction mapper) {
return new IteratorIntStream(new ExIntIterator() {
@Override
public boolean hasNext() {
return elements.hasNext();
}
@Override
public int nextInt() {
return mapper.applyAsInt(elements.nextFloat());
}
@Override
public long count() {
return elements.count();
}
@Override
public void skip(long n) {
elements.skip(n);
}
}, closeHandlers);
}
@Override
public LongStream mapToLong(final FloatToLongFunction mapper) {
return new IteratorLongStream(new ExLongIterator() {
@Override
public boolean hasNext() {
return elements.hasNext();
}
@Override
public long nextLong() {
return mapper.applyAsLong(elements.nextFloat());
}
@Override
public long count() {
return elements.count();
}
@Override
public void skip(long n) {
elements.skip(n);
}
}, closeHandlers);
}
@Override
public DoubleStream mapToDouble(final FloatToDoubleFunction mapper) {
return new IteratorDoubleStream(new ExDoubleIterator() {
@Override
public boolean hasNext() {
return elements.hasNext();
}
@Override
public double nextDouble() {
return mapper.applyAsDouble(elements.nextFloat());
}
@Override
public long count() {
return elements.count();
}
@Override
public void skip(long n) {
elements.skip(n);
}
}, closeHandlers);
}
@Override
public Stream mapToObj(final FloatFunction extends U> mapper) {
return new IteratorStream(new ExIterator() {
@Override
public boolean hasNext() {
return elements.hasNext();
}
@Override
public U next() {
return mapper.apply(elements.nextFloat());
}
@Override
public long count() {
return elements.count();
}
@Override
public void skip(long n) {
elements.skip(n);
}
}, closeHandlers);
}
@Override
public FloatStream flatMap(final FloatFunction extends FloatStream> mapper) {
return new IteratorFloatStream(new ExFloatIterator() {
private FloatIterator cur = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && elements.hasNext()) {
cur = mapper.apply(elements.nextFloat()).exIterator();
}
return cur != null && cur.hasNext();
}
@Override
public float nextFloat() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.nextFloat();
}
}, closeHandlers);
}
@Override
public IntStream flatMapToInt(final FloatFunction extends IntStream> mapper) {
return new IteratorIntStream(new ExIntIterator() {
private IntIterator cur = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && elements.hasNext()) {
cur = mapper.apply(elements.nextFloat()).exIterator();
}
return cur != null && cur.hasNext();
}
@Override
public int nextInt() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.nextInt();
}
}, closeHandlers);
}
@Override
public LongStream flatMapToLong(final FloatFunction extends LongStream> mapper) {
return new IteratorLongStream(new ExLongIterator() {
private LongIterator cur = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && elements.hasNext()) {
cur = mapper.apply(elements.nextFloat()).exIterator();
}
return cur != null && cur.hasNext();
}
@Override
public long nextLong() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.nextLong();
}
}, closeHandlers);
}
@Override
public DoubleStream flatMapToDouble(final FloatFunction extends DoubleStream> mapper) {
return new IteratorDoubleStream(new ExDoubleIterator() {
private DoubleIterator cur = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && elements.hasNext()) {
cur = mapper.apply(elements.nextFloat()).exIterator();
}
return cur != null && cur.hasNext();
}
@Override
public double nextDouble() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.nextDouble();
}
}, closeHandlers);
}
@Override
public Stream flatMapToObj(final FloatFunction extends Stream> mapper) {
return new IteratorStream(new ExIterator() {
private Iterator extends T> cur = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && elements.hasNext()) {
cur = mapper.apply(elements.nextFloat()).iterator();
}
return cur != null && cur.hasNext();
}
@Override
public T next() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.next();
}
}, closeHandlers);
}
@Override
public Stream splitToList(final int size) {
N.checkArgument(size > 0, "'size' must be bigger than 0");
return new IteratorStream(new ExIterator() {
@Override
public boolean hasNext() {
return elements.hasNext();
}
@Override
public FloatList next() {
if (hasNext() == false) {
throw new NoSuchElementException();
}
final FloatList result = new FloatList(size);
while (result.size() < size && elements.hasNext()) {
result.add(elements.nextFloat());
}
return result;
}
}, closeHandlers);
}
@Override
public Stream splitToList(final FloatPredicate predicate) {
return new IteratorStream(new ExIterator() {
private float next;
private boolean hasNext = false;
private boolean preCondition = false;
@Override
public boolean hasNext() {
return hasNext == true || elements.hasNext();
}
@Override
public FloatList next() {
if (hasNext() == false) {
throw new NoSuchElementException();
}
final FloatList result = new FloatList();
if (hasNext == false) {
next = elements.nextFloat();
hasNext = true;
}
while (hasNext) {
if (result.size() == 0) {
result.add(next);
preCondition = predicate.test(next);
next = (hasNext = elements.hasNext()) ? elements.nextFloat() : 0;
} else if (predicate.test(next) == preCondition) {
result.add(next);
next = (hasNext = elements.hasNext()) ? elements.nextFloat() : 0;
} else {
break;
}
}
return result;
}
}, closeHandlers);
}
@Override
public Stream splitToList(final U identity, final BiFunction super Float, ? super U, Boolean> predicate,
final Consumer super U> identityUpdate) {
return new IteratorStream(new ExIterator() {
private float next;
private boolean hasNext = false;
private boolean preCondition = false;
@Override
public boolean hasNext() {
return hasNext == true || elements.hasNext();
}
@Override
public FloatList next() {
if (hasNext() == false) {
throw new NoSuchElementException();
}
final FloatList result = new FloatList();
if (hasNext == false) {
next = elements.nextFloat();
hasNext = true;
}
while (hasNext) {
if (result.size() == 0) {
result.add(next);
preCondition = predicate.apply(next, identity);
next = (hasNext = elements.hasNext()) ? elements.nextFloat() : 0;
} else if (predicate.apply(next, identity) == preCondition) {
result.add(next);
next = (hasNext = elements.hasNext()) ? elements.nextFloat() : 0;
} else {
if (identityUpdate != null) {
identityUpdate.accept(identity);
}
break;
}
}
return result;
}
}, closeHandlers);
}
@Override
public Stream slidingToList(final int windowSize, final int increment) {
N.checkArgument(windowSize > 0 && increment > 0, "'windowSize'=%s and 'increment'=%s must not be less than 1", windowSize, increment);
return new IteratorStream(new ExIterator() {
private FloatList prev = null;
@Override
public boolean hasNext() {
if (prev != null && increment > windowSize) {
int skipNum = increment - windowSize;
while (skipNum-- > 0 && elements.hasNext()) {
elements.nextFloat();
}
prev = null;
}
return elements.hasNext();
}
@Override
public FloatList next() {
if (hasNext() == false) {
throw new NoSuchElementException();
}
FloatList result = null;
int cnt = 0;
if (prev != null && increment < windowSize) {
cnt = windowSize - increment;
if (cnt <= 8) {
result = new FloatList(windowSize);
for (int i = windowSize - cnt; i < windowSize; i++) {
result.add(prev.get(i));
}
} else {
final float[] dest = new float[windowSize];
N.copy(prev.trimToSize().array(), windowSize - cnt, dest, 0, cnt);
result = FloatList.of(dest, cnt);
}
} else {
result = new FloatList(windowSize);
}
while (cnt++ < windowSize && elements.hasNext()) {
result.add(elements.nextFloat());
}
return prev = result;
}
}, closeHandlers);
}
@Override
public FloatStream top(int n) {
return top(n, FLOAT_COMPARATOR);
}
@Override
public FloatStream top(int n, Comparator super Float> comparator) {
return boxed().top(n, comparator).mapToFloat(ToFloatFunction.UNBOX);
}
@Override
public FloatStream sorted() {
if (sorted) {
return this;
}
return new IteratorFloatStream(new ExFloatIterator() {
float[] a = null;
int toIndex = 0;
int cursor = 0;
@Override
public boolean hasNext() {
if (a == null) {
sort();
}
return cursor < toIndex;
}
@Override
public float nextFloat() {
if (a == null) {
sort();
}
if (cursor >= toIndex) {
throw new NoSuchElementException();
}
return a[cursor++];
}
@Override
public long count() {
if (a == null) {
sort();
}
return toIndex - cursor;
}
@Override
public void skip(long n) {
if (a == null) {
sort();
}
cursor = n < toIndex - cursor ? cursor + (int) n : toIndex;
}
@Override
public float[] toArray() {
if (a == null) {
sort();
}
if (cursor == 0) {
return a;
} else {
return N.copyOfRange(a, cursor, toIndex);
}
}
private void sort() {
a = elements.toArray();
toIndex = a.length;
N.sort(a);
}
}, closeHandlers, true);
}
@Override
public FloatStream peek(final FloatConsumer action) {
return new IteratorFloatStream(new ExFloatIterator() {
@Override
public boolean hasNext() {
return elements.hasNext();
}
@Override
public float nextFloat() {
final float next = elements.nextFloat();
action.accept(next);
return next;
}
}, closeHandlers, sorted);
}
@Override
public FloatStream limit(final long maxSize) {
if (maxSize < 0) {
throw new IllegalArgumentException("'maxSize' can't be negative: " + maxSize);
}
return new IteratorFloatStream(new ExFloatIterator() {
private long cnt = 0;
@Override
public boolean hasNext() {
return cnt < maxSize && elements.hasNext();
}
@Override
public float nextFloat() {
if (cnt >= maxSize) {
throw new NoSuchElementException();
}
cnt++;
return elements.nextFloat();
}
@Override
public void skip(long n) {
elements.skip(n);
}
}, closeHandlers, sorted);
}
@Override
public FloatStream skip(final long n) {
if (n < 0) {
throw new IllegalArgumentException("The skipped number can't be negative: " + n);
} else if (n == 0) {
return this;
}
return new IteratorFloatStream(new ExFloatIterator() {
private boolean skipped = false;
@Override
public boolean hasNext() {
if (skipped == false) {
elements.skip(n);
skipped = true;
}
return elements.hasNext();
}
@Override
public float nextFloat() {
if (skipped == false) {
elements.skip(n);
skipped = true;
}
return elements.nextFloat();
}
@Override
public long count() {
if (skipped == false) {
elements.skip(n);
skipped = true;
}
return elements.count();
}
@Override
public void skip(long n2) {
if (skipped == false) {
elements.skip(n);
skipped = true;
}
elements.skip(n2);
}
@Override
public float[] toArray() {
if (skipped == false) {
elements.skip(n);
skipped = true;
}
return elements.toArray();
}
}, closeHandlers, sorted);
}
@Override
public void forEach(FloatConsumer action) {
while (elements.hasNext()) {
action.accept(elements.nextFloat());
}
}
@Override
public float[] toArray() {
return elements.toArray();
}
@Override
public FloatList toFloatList() {
return FloatList.of(toArray());
}
@Override
public List toList() {
final List result = new ArrayList<>();
while (elements.hasNext()) {
result.add(elements.nextFloat());
}
return result;
}
@Override
public > R toList(Supplier supplier) {
final R result = supplier.get();
while (elements.hasNext()) {
result.add(elements.nextFloat());
}
return result;
}
@Override
public Set toSet() {
final Set result = new HashSet<>();
while (elements.hasNext()) {
result.add(elements.nextFloat());
}
return result;
}
@Override
public > R toSet(Supplier supplier) {
final R result = supplier.get();
while (elements.hasNext()) {
result.add(elements.nextFloat());
}
return result;
}
@Override
public Multiset toMultiset() {
final Multiset result = new Multiset<>();
while (elements.hasNext()) {
result.add(elements.nextFloat());
}
return result;
}
@Override
public Multiset toMultiset(Supplier extends Multiset> supplier) {
final Multiset result = supplier.get();
while (elements.hasNext()) {
result.add(elements.nextFloat());
}
return result;
}
@Override
public LongMultiset toLongMultiset() {
final LongMultiset result = new LongMultiset<>();
while (elements.hasNext()) {
result.add(elements.nextFloat());
}
return result;
}
@Override
public LongMultiset toLongMultiset(Supplier extends LongMultiset> supplier) {
final LongMultiset result = supplier.get();
while (elements.hasNext()) {
result.add(elements.nextFloat());
}
return result;
}
@Override
public > M toMap(FloatFunction extends K> keyExtractor, FloatFunction extends U> valueMapper, BinaryOperator mergeFunction,
Supplier mapFactory) {
final M result = mapFactory.get();
float element = 0;
while (elements.hasNext()) {
element = elements.nextFloat();
Collectors.merge(result, keyExtractor.apply(element), valueMapper.apply(element), mergeFunction);
}
return result;
}
@Override
public > M toMap(final FloatFunction extends K> classifier, final Collector downstream,
final Supplier mapFactory) {
final M result = mapFactory.get();
final Supplier downstreamSupplier = downstream.supplier();
final BiConsumer downstreamAccumulator = downstream.accumulator();
final Map intermediate = (Map) result;
K key = null;
A v = null;
float element = 0;
while (elements.hasNext()) {
element = elements.nextFloat();
key = N.requireNonNull(classifier.apply(element), "element cannot be mapped to a null key");
if ((v = intermediate.get(key)) == null) {
if ((v = downstreamSupplier.get()) != null) {
intermediate.put(key, v);
}
}
downstreamAccumulator.accept(v, element);
}
final BiFunction super K, ? super A, ? extends A> function = new BiFunction() {
@Override
public A apply(K k, A v) {
return (A) downstream.finisher().apply(v);
}
};
Collectors.replaceAll(intermediate, function);
return result;
}
@Override
public > Multimap toMultimap(FloatFunction extends K> keyExtractor, FloatFunction extends U> valueMapper,
Supplier> mapFactory) {
final Multimap result = mapFactory.get();
float element = 0;
while (elements.hasNext()) {
element = elements.nextFloat();
result.put(keyExtractor.apply(element), valueMapper.apply(element));
}
return result;
}
@Override
public float reduce(float identity, FloatBinaryOperator op) {
float result = identity;
while (elements.hasNext()) {
result = op.applyAsFloat(result, elements.nextFloat());
}
return result;
}
@Override
public OptionalFloat reduce(FloatBinaryOperator op) {
if (elements.hasNext() == false) {
return OptionalFloat.empty();
}
float result = elements.nextFloat();
while (elements.hasNext()) {
result = op.applyAsFloat(result, elements.nextFloat());
}
return OptionalFloat.of(result);
}
@Override
public R collect(Supplier supplier, ObjFloatConsumer accumulator, BiConsumer combiner) {
final R result = supplier.get();
while (elements.hasNext()) {
accumulator.accept(result, elements.nextFloat());
}
return result;
}
@Override
public OptionalFloat head() {
if (head == null) {
head = elements.hasNext() ? OptionalFloat.of(elements.nextFloat()) : OptionalFloat.empty();
tail = new IteratorFloatStream(elements, closeHandlers, sorted);
}
return head;
}
@Override
public FloatStream tail() {
if (tail == null) {
head = elements.hasNext() ? OptionalFloat.of(elements.nextFloat()) : OptionalFloat.empty();
tail = new IteratorFloatStream(elements, closeHandlers, sorted);
}
return tail;
}
@Override
public FloatStream head2() {
if (head2 == null) {
final float[] a = elements.toArray();
head2 = new ArrayFloatStream(a, 0, a.length == 0 ? 0 : a.length - 1, closeHandlers, sorted);
tail2 = a.length == 0 ? OptionalFloat.empty() : OptionalFloat.of(a[a.length - 1]);
}
return head2;
}
@Override
public OptionalFloat tail2() {
if (tail2 == null) {
final float[] a = elements.toArray();
head2 = new ArrayFloatStream(a, 0, a.length == 0 ? 0 : a.length - 1, closeHandlers, sorted);
tail2 = a.length == 0 ? OptionalFloat.empty() : OptionalFloat.of(a[a.length - 1]);
}
return tail2;
}
@Override
public OptionalFloat min() {
if (elements.hasNext() == false) {
return OptionalFloat.empty();
} else if (sorted) {
return OptionalFloat.of(elements.nextFloat());
}
float candidate = elements.nextFloat();
float next = 0;
while (elements.hasNext()) {
next = elements.nextFloat();
if (N.compare(next, candidate) < 0) {
candidate = next;
}
}
return OptionalFloat.of(candidate);
}
@Override
public OptionalFloat max() {
if (elements.hasNext() == false) {
return OptionalFloat.empty();
} else if (sorted) {
float next = 0;
while (elements.hasNext()) {
next = elements.nextFloat();
}
return OptionalFloat.of(next);
}
float candidate = elements.nextFloat();
float next = 0;
while (elements.hasNext()) {
next = elements.nextFloat();
if (N.compare(next, candidate) > 0) {
candidate = next;
}
}
return OptionalFloat.of(candidate);
}
@Override
public OptionalFloat kthLargest(int k) {
N.checkArgument(k > 0, "'k' must be bigger than 0");
if (elements.hasNext() == false) {
return OptionalFloat.empty();
}
final NullabLe optional = boxed().kthLargest(k, FLOAT_COMPARATOR);
return optional.isPresent() ? OptionalFloat.of(optional.get()) : OptionalFloat.empty();
}
@Override
public long count() {
return elements.count();
}
@Override
public FloatSummaryStatistics summarize() {
final FloatSummaryStatistics result = new FloatSummaryStatistics();
while (elements.hasNext()) {
result.accept(elements.nextFloat());
}
return result;
}
@Override
public boolean anyMatch(FloatPredicate predicate) {
while (elements.hasNext()) {
if (predicate.test(elements.nextFloat())) {
return true;
}
}
return false;
}
@Override
public boolean allMatch(FloatPredicate predicate) {
while (elements.hasNext()) {
if (predicate.test(elements.nextFloat()) == false) {
return false;
}
}
return true;
}
@Override
public boolean noneMatch(FloatPredicate predicate) {
while (elements.hasNext()) {
if (predicate.test(elements.nextFloat())) {
return false;
}
}
return true;
}
@Override
public OptionalFloat findFirst(FloatPredicate predicate) {
while (elements.hasNext()) {
float e = elements.nextFloat();
if (predicate.test(e)) {
return OptionalFloat.of(e);
}
}
return OptionalFloat.empty();
}
@Override
public OptionalFloat findLast(FloatPredicate predicate) {
if (elements.hasNext() == false) {
return OptionalFloat.empty();
}
boolean hasResult = false;
float e = 0;
float result = 0;
while (elements.hasNext()) {
e = elements.nextFloat();
if (predicate.test(e)) {
result = e;
hasResult = true;
}
}
return hasResult ? OptionalFloat.of(result) : OptionalFloat.empty();
}
@Override
public DoubleStream asDoubleStream() {
return new IteratorDoubleStream(new ExDoubleIterator() {
@Override
public boolean hasNext() {
return elements.hasNext();
}
@Override
public double nextDouble() {
return elements.nextFloat();
}
@Override
public long count() {
return elements.count();
}
@Override
public void skip(long n) {
elements.skip(n);
}
}, closeHandlers, sorted);
}
@Override
public Stream boxed() {
return new IteratorStream(iterator(), closeHandlers, sorted, sorted ? FLOAT_COMPARATOR : null);
}
@Override
ExFloatIterator exIterator() {
return elements;
}
@Override
public FloatStream parallel(int maxThreadNum, Splitor splitor) {
if (maxThreadNum < 1 || maxThreadNum > MAX_THREAD_NUM_PER_OPERATION) {
throw new IllegalArgumentException("'maxThreadNum' must not less than 1 or exceeded: " + MAX_THREAD_NUM_PER_OPERATION);
}
return new ParallelIteratorFloatStream(elements, closeHandlers, sorted, maxThreadNum, splitor);
}
@Override
public FloatStream onClose(Runnable closeHandler) {
final Set newCloseHandlers = new AbstractStream.LocalLinkedHashSet<>(N.isNullOrEmpty(this.closeHandlers) ? 1 : this.closeHandlers.size() + 1);
if (N.notNullOrEmpty(this.closeHandlers)) {
newCloseHandlers.addAll(this.closeHandlers);
}
newCloseHandlers.add(closeHandler);
return new IteratorFloatStream(elements, newCloseHandlers, sorted);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy