com.landawn.abacus.util.stream.AbstractCharStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-android-se-jdk7 Show documentation
Show all versions of abacus-android-se-jdk7 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.CharIterator;
import com.landawn.abacus.util.CharList;
import com.landawn.abacus.util.CharMatrix;
import com.landawn.abacus.util.CharSummaryStatistics;
import com.landawn.abacus.util.Fn;
import com.landawn.abacus.util.IndexedChar;
import com.landawn.abacus.util.Joiner;
import com.landawn.abacus.util.Multiset;
import com.landawn.abacus.util.MutableChar;
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.OptionalChar;
import com.landawn.abacus.util.Pair;
import com.landawn.abacus.util.Percentage;
import com.landawn.abacus.util.Try;
import com.landawn.abacus.util.function.BiConsumer;
import com.landawn.abacus.util.function.BiPredicate;
import com.landawn.abacus.util.function.BinaryOperator;
import com.landawn.abacus.util.function.CharBiFunction;
import com.landawn.abacus.util.function.CharBiPredicate;
import com.landawn.abacus.util.function.CharConsumer;
import com.landawn.abacus.util.function.CharFunction;
import com.landawn.abacus.util.function.CharPredicate;
import com.landawn.abacus.util.function.CharTriFunction;
import com.landawn.abacus.util.function.Consumer;
import com.landawn.abacus.util.function.Function;
import com.landawn.abacus.util.function.ObjCharConsumer;
import com.landawn.abacus.util.function.Predicate;
import com.landawn.abacus.util.function.Supplier;
import com.landawn.abacus.util.function.ToCharFunction;
/**
* This class is a sequential, stateful and immutable stream implementation.
*
* @since 0.8
*
* @author Haiyang Li
*/
abstract class AbstractCharStream extends CharStream {
AbstractCharStream(final boolean sorted, final Collection closeHandlers) {
super(sorted, closeHandlers);
}
@Override
public CharStream flattMap(final CharFunction mapper) {
return flatMap(new CharFunction() {
@Override
public CharStream apply(char t) {
return CharStream.of(mapper.apply(t));
}
});
}
@Override
public Stream flattMapToObj(final CharFunction extends Collection> mapper) {
return flatMapToObj(new CharFunction>() {
@Override
public Stream apply(char t) {
return Stream.of(mapper.apply(t));
}
});
}
@Override
public CharStream skip(final long n, final CharConsumer action) {
N.checkArgNotNegative(n, "n");
if (n == 0) {
return this;
}
final CharPredicate filter = isParallel() ? new CharPredicate() {
final AtomicLong cnt = new AtomicLong(n);
@Override
public boolean test(char value) {
return cnt.getAndDecrement() > 0;
}
} : new CharPredicate() {
final MutableLong cnt = MutableLong.of(n);
@Override
public boolean test(char value) {
return cnt.getAndDecrement() > 0;
}
};
return dropWhile(filter, action);
}
@Override
public CharStream removeIf(final CharPredicate predicate) {
N.checkArgNotNull(predicate);
return filter(new CharPredicate() {
@Override
public boolean test(char value) {
return predicate.test(value) == false;
}
});
}
@Override
public CharStream removeIf(final CharPredicate predicate, final CharConsumer action) {
N.checkArgNotNull(predicate);
N.checkArgNotNull(predicate);
return filter(new CharPredicate() {
@Override
public boolean test(char value) {
if (predicate.test(value)) {
action.accept(value);
return false;
}
return true;
}
});
}
@Override
public CharStream dropWhile(final CharPredicate predicate, final CharConsumer action) {
N.checkArgNotNull(predicate);
N.checkArgNotNull(action);
return dropWhile(new CharPredicate() {
@Override
public boolean test(char value) {
if (predicate.test(value)) {
action.accept(value);
return true;
}
return false;
}
});
}
@Override
public CharStream step(final long step) {
N.checkArgPositive(step, "step");
if (step == 1) {
return this;
}
final long skip = step - 1;
final CharIteratorEx iter = this.iteratorEx();
final CharIterator charIterator = new CharIteratorEx() {
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public char nextChar() {
final char next = iter.nextChar();
iter.skip(skip);
return next;
}
};
return newStream(charIterator, sorted);
}
@Override
public Stream split(final int size) {
return splitToList(size).map(new Function() {
@Override
public CharStream apply(CharList t) {
return new ArrayCharStream(t.array(), 0, t.size(), sorted, null);
}
});
}
@Override
public Stream split(final CharPredicate predicate) {
return splitToList(predicate).map(new Function() {
@Override
public CharStream apply(CharList t) {
return new ArrayCharStream(t.array(), 0, t.size(), sorted, null);
}
});
}
@Override
public Stream splitToList(final CharPredicate predicate) {
final BiPredicate predicate2 = new BiPredicate() {
@Override
public boolean test(Character t, Object u) {
return predicate.test(t);
}
};
return splitToList(null, predicate2, null);
}
@Override
public Stream split(final U seed, final BiPredicate super Character, ? super U> predicate, final Consumer super U> seedUpdate) {
return splitToList(seed, predicate, seedUpdate).map(new Function() {
@Override
public CharStream apply(CharList t) {
return new ArrayCharStream(t.array(), 0, t.size(), sorted, null);
}
});
}
@Override
public Stream sliding(final int windowSize, final int increment) {
return slidingToList(windowSize, increment).map(new Function() {
@Override
public CharStream apply(CharList t) {
return new ArrayCharStream(t.array(), 0, t.size(), sorted, null);
}
});
}
@Override
public CharStream collapse(final CharBiPredicate collapsible, final CharBiFunction mergeFunction) {
final CharIteratorEx iter = iteratorEx();
return newStream(new CharIteratorEx() {
private boolean hasNext = false;
private char next = 0;
@Override
public boolean hasNext() {
return hasNext || iter.hasNext();
}
@Override
public char nextChar() {
char res = hasNext ? next : (next = iter.nextChar());
while ((hasNext = iter.hasNext())) {
if (collapsible.test(next, (next = iter.nextChar()))) {
res = mergeFunction.apply(res, next);
} else {
break;
}
}
return res;
}
}, false);
}
@Override
public CharStream scan(final CharBiFunction accumulator) {
final CharIteratorEx iter = iteratorEx();
return newStream(new CharIteratorEx() {
private char res = 0;
private boolean isFirst = true;
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public char nextChar() {
if (isFirst) {
isFirst = false;
return (res = iter.nextChar());
} else {
return (res = accumulator.apply(res, iter.nextChar()));
}
}
}, false);
}
@Override
public CharStream scan(final char seed, final CharBiFunction accumulator) {
final CharIteratorEx iter = iteratorEx();
return newStream(new CharIteratorEx() {
private char res = seed;
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public char nextChar() {
return (res = accumulator.apply(res, iter.nextChar()));
}
}, false);
}
@Override
public Map toMap(CharFunction extends K> keyExtractor, CharFunction extends V> valueMapper) {
final Supplier
© 2015 - 2025 Weber Informatics LLC | Privacy Policy