com.landawn.abacus.util.stream.AbstractFloatStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-util-all Show documentation
Show all versions of abacus-util-all 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.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
import com.landawn.abacus.util.FloatIterator;
import com.landawn.abacus.util.FloatList;
import com.landawn.abacus.util.FloatMatrix;
import com.landawn.abacus.util.FloatSummaryStatistics;
import com.landawn.abacus.util.Fn;
import com.landawn.abacus.util.IndexedFloat;
import com.landawn.abacus.util.Joiner;
import com.landawn.abacus.util.Multiset;
import com.landawn.abacus.util.MutableFloat;
import com.landawn.abacus.util.MutableLong;
import com.landawn.abacus.util.N;
import com.landawn.abacus.util.Nth;
import com.landawn.abacus.util.Optional;
import com.landawn.abacus.util.OptionalDouble;
import com.landawn.abacus.util.OptionalFloat;
import com.landawn.abacus.util.Pair;
import com.landawn.abacus.util.Percentage;
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.FloatBiFunction;
import com.landawn.abacus.util.function.FloatBiPredicate;
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.FloatTriFunction;
import com.landawn.abacus.util.function.Function;
import com.landawn.abacus.util.function.ObjFloatConsumer;
import com.landawn.abacus.util.function.Predicate;
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
*/
abstract class AbstractFloatStream extends FloatStream {
AbstractFloatStream(final Collection closeHandlers, final boolean sorted) {
super(closeHandlers, sorted);
}
@Override
public FloatStream flatArray(final FloatFunction mapper) {
return flatMap(new FloatFunction() {
@Override
public FloatStream apply(float t) {
return FloatStream.of(mapper.apply(t));
}
});
}
@Override
public FloatStream remove(final long n, final FloatConsumer action) {
if (n < 0) {
throw new IllegalArgumentException("'n' can't be less than 0");
} else if (n == 0) {
return this;
}
if (this.isParallel()) {
final AtomicLong cnt = new AtomicLong(n);
return removeWhile(new FloatPredicate() {
@Override
public boolean test(float value) {
return cnt.getAndDecrement() > 0;
}
}, action);
} else {
final MutableLong cnt = MutableLong.of(n);
return removeWhile(new FloatPredicate() {
@Override
public boolean test(float value) {
return cnt.getAndDecrement() > 0;
}
}, action);
}
}
@Override
public FloatStream removeIf(final FloatPredicate predicate) {
N.requireNonNull(predicate);
return filter(new FloatPredicate() {
@Override
public boolean test(float value) {
return predicate.test(value) == false;
}
});
}
@Override
public FloatStream removeIf(final FloatPredicate predicate, final FloatConsumer action) {
N.requireNonNull(predicate);
N.requireNonNull(predicate);
return filter(new FloatPredicate() {
@Override
public boolean test(float value) {
if (predicate.test(value)) {
action.accept(value);
return false;
}
return true;
}
});
}
@Override
public FloatStream removeWhile(final FloatPredicate predicate, final FloatConsumer action) {
N.requireNonNull(predicate);
N.requireNonNull(action);
return dropWhile(new FloatPredicate() {
@Override
public boolean test(float value) {
if (predicate.test(value)) {
action.accept(value);
return true;
}
return false;
}
});
}
@Override
public FloatStream step(final long step) {
N.checkArgument(step > 0, "'step' can't be 0 or negative: %s", step);
if (step == 1) {
return this;
}
final long skip = step - 1;
final ExFloatIterator iter = this.exIterator();
final FloatIterator floatIterator = new ExFloatIterator() {
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public float nextFloat() {
final float next = iter.nextFloat();
iter.skip(skip);
return next;
}
};
return newStream(floatIterator, sorted);
}
@Override
public Stream split(final int size) {
return splitToList(size).map(new Function() {
@Override
public FloatStream apply(FloatList t) {
return new ArrayFloatStream(t.array(), 0, t.size(), null, sorted);
}
});
}
@Override
public Stream split(final FloatPredicate predicate) {
return splitToList(predicate).map(new Function() {
@Override
public FloatStream apply(FloatList t) {
return new ArrayFloatStream(t.array(), 0, t.size(), null, sorted);
}
});
}
@Override
public Stream splitToList(final FloatPredicate predicate) {
final BiFunction predicate2 = new BiFunction() {
@Override
public Boolean apply(Float t, Object u) {
return predicate.test(t);
}
};
return splitToList(null, predicate2, null);
}
@Override
public Stream split(final U identity, final BiFunction super Float, ? super U, Boolean> predicate,
final Consumer super U> identityUpdate) {
return splitToList(identity, predicate, identityUpdate).map(new Function() {
@Override
public FloatStream apply(FloatList t) {
return new ArrayFloatStream(t.array(), 0, t.size(), null, sorted);
}
});
}
@Override
public Stream sliding(final int windowSize, final int increment) {
return slidingToList(windowSize, increment).map(new Function() {
@Override
public FloatStream apply(FloatList t) {
return new ArrayFloatStream(t.array(), 0, t.size(), null, sorted);
}
});
}
@Override
public FloatStream collapse(final FloatBiPredicate collapsible, final FloatBiFunction mergeFunction) {
final ExFloatIterator iter = exIterator();
return this.newStream(new ExFloatIterator() {
private boolean hasNext = false;
private float next = 0;
@Override
public boolean hasNext() {
return hasNext || iter.hasNext();
}
@Override
public float nextFloat() {
float res = hasNext ? next : (next = iter.nextFloat());
while ((hasNext = iter.hasNext())) {
if (collapsible.test(next, (next = iter.nextFloat()))) {
res = mergeFunction.apply(res, next);
} else {
break;
}
}
return res;
}
}, false);
}
@Override
public FloatStream scan(final FloatBiFunction accumulator) {
final ExFloatIterator iter = exIterator();
return this.newStream(new ExFloatIterator() {
private float res = 0;
private boolean isFirst = true;
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public float nextFloat() {
if (isFirst) {
isFirst = false;
return (res = iter.nextFloat());
} else {
return (res = accumulator.apply(res, iter.nextFloat()));
}
}
}, false);
}
@Override
public FloatStream scan(final float seed, final FloatBiFunction accumulator) {
final ExFloatIterator iter = exIterator();
return this.newStream(new ExFloatIterator() {
private float res = seed;
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public float nextFloat() {
return (res = accumulator.apply(res, iter.nextFloat()));
}
}, false);
}
@Override
public FloatStream reverseSorted() {
return sorted().reversed();
}
@Override
public Map toMap(FloatFunction extends K> keyExtractor, FloatFunction extends U> valueMapper) {
final Supplier
© 2015 - 2025 Weber Informatics LLC | Privacy Policy