com.landawn.abacus.util.stream.AbstractStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-common Show documentation
Show all versions of abacus-common Show documentation
A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.
/*
* Copyright (C) 2016, 2017, 2018, 2019 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.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Deque;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.DoubleConsumer;
import java.util.function.Function;
import java.util.function.IntConsumer;
import java.util.function.IntFunction;
import java.util.function.LongConsumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.stream.Collector;
import com.landawn.abacus.exception.TooManyElementsException;
import com.landawn.abacus.parser.JSONParser;
import com.landawn.abacus.parser.JSONSerializationConfig;
import com.landawn.abacus.parser.JSONSerializationConfig.JSC;
import com.landawn.abacus.parser.ParserFactory;
import com.landawn.abacus.parser.ParserUtil;
import com.landawn.abacus.parser.ParserUtil.BeanInfo;
import com.landawn.abacus.parser.ParserUtil.PropInfo;
import com.landawn.abacus.type.Type;
import com.landawn.abacus.util.Array;
import com.landawn.abacus.util.BufferedJSONWriter;
import com.landawn.abacus.util.BufferedWriter;
import com.landawn.abacus.util.ClassUtil;
import com.landawn.abacus.util.Comparators;
import com.landawn.abacus.util.DataSet;
import com.landawn.abacus.util.DateTimeFormat;
import com.landawn.abacus.util.Fn;
import com.landawn.abacus.util.Fn.BiFunctions;
import com.landawn.abacus.util.Fn.Factory;
import com.landawn.abacus.util.Fn.Suppliers;
import com.landawn.abacus.util.IOUtil;
import com.landawn.abacus.util.Indexed;
import com.landawn.abacus.util.Iterables;
import com.landawn.abacus.util.Joiner;
import com.landawn.abacus.util.ListMultimap;
import com.landawn.abacus.util.MergeResult;
import com.landawn.abacus.util.Multimap;
import com.landawn.abacus.util.Multiset;
import com.landawn.abacus.util.MutableBoolean;
import com.landawn.abacus.util.MutableInt;
import com.landawn.abacus.util.MutableLong;
import com.landawn.abacus.util.N;
import com.landawn.abacus.util.Objectory;
import com.landawn.abacus.util.Percentage;
import com.landawn.abacus.util.PermutationIterator;
import com.landawn.abacus.util.Strings.StringUtil;
import com.landawn.abacus.util.Throwables;
import com.landawn.abacus.util.Tuple.Tuple3;
import com.landawn.abacus.util.u.Optional;
import com.landawn.abacus.util.u.OptionalDouble;
import com.landawn.abacus.util.u.OptionalInt;
import com.landawn.abacus.util.u.OptionalLong;
import com.landawn.abacus.util.function.TriFunction;
import com.landawn.abacus.util.function.TriPredicate;
/**
*
* @param
*/
@SuppressWarnings({ "java:S1192", "java:S1845", "java:S2445", "java:S3077" })
abstract class AbstractStream extends Stream {
AbstractStream(final boolean sorted, final Comparator cmp, final Collection closeHandlers) {
super(sorted, cmp, closeHandlers);
}
@Override
public Stream skipRange(final int startInclusive, final int endExclusive) {
assertNotClosed();
checkArgNotNegative(startInclusive, "startInclusive");
checkArgNotNegative(endExclusive, "endExclusive");
if (startInclusive == endExclusive) {
return skip(0);
}
return newStream(new ObjIteratorEx() { //NOSONAR
private final ObjIteratorEx iter = iteratorEx();
private final MutableLong idx = MutableLong.of(0);
private boolean skipped = false;
@Override
public boolean hasNext() {
if (!skipped && idx.value() >= startInclusive) {
skipped = true;
while (iter.hasNext() && idx.value() < endExclusive) {
iter.next();
idx.increment();
}
}
return iter.hasNext();
}
@Override
public T next() {
if (hasNext() == false) {
throw new NoSuchElementException();
}
idx.increment();
return iter.next();
}
}, sorted, cmp);
}
@Override
public Stream skip(final long n, final Consumer action) {
assertNotClosed();
checkArgNotNegative(n, "n");
checkArgNotNull(action, "action");
final Predicate filter = isParallel() ? new Predicate<>() {
final AtomicLong cnt = new AtomicLong(n);
@Override
public boolean test(T value) {
return cnt.getAndDecrement() > 0;
}
} : new Predicate<>() {
final MutableLong cnt = MutableLong.of(n);
@Override
public boolean test(T value) {
return cnt.getAndDecrement() > 0;
}
};
return dropWhile(filter, action);
}
@Override
public Stream peekFirst(final Consumer action) {
assertNotClosed();
final Function mapperForFirst = t -> {
action.accept(t);
return t;
};
return mapFirst(mapperForFirst);
}
@Override
public Stream peekLast(final Consumer action) {
assertNotClosed();
final Function mapperForLast = t -> {
action.accept(t);
return t;
};
return mapLast(mapperForLast);
}
// @Override
// public Stream removeIf(final Predicate predicate) {
// assertNotClosed();
//
// return filter(value -> !predicate.test(value));
// }
//
// @Override
// public Stream removeIf(final Predicate predicate, final Consumer actionOnDroppedItem) {
// assertNotClosed();
//
// return filter(value -> {
// if (predicate.test(value)) {
// actionOnDroppedItem.accept(value);
// return false;
// }
//
// return true;
// });
// }
@Override
public Stream filter(final Predicate predicate, final Consumer actionOnDroppedItem) {
assertNotClosed();
return filter(value -> {
if (!predicate.test(value)) {
actionOnDroppedItem.accept(value);
return false;
}
return true;
});
}
@Override
public Stream dropWhile(final Predicate predicate, final Consumer actionOnDroppedItem) {
assertNotClosed();
return dropWhile(value -> {
if (predicate.test(value)) {
actionOnDroppedItem.accept(value);
return true;
}
return false;
});
}
@Override
public Stream step(final long step) {
assertNotClosed();
checkArgPositive(step, "step");
if (step == 1) {
return skip(0);
}
final long skip = step - 1;
final ObjIteratorEx iter = iteratorEx();
final Iterator iterator = new ObjIteratorEx<>() {
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public T next() {
final T next = iter.next();
iter.advance(skip);
return next;
}
};
return newStream(iterator, sorted, cmp);
}
// @Override
// public Stream biMap(BiFunction mapper) {
// return biMap(mapper, false);
// }
//
// @Override
// public Stream triMap(TriFunction mapper) {
// return triMap(mapper, false);
// }
@Override
public Stream slidingMap(BiFunction mapper) {
assertNotClosed();
return slidingMap(mapper, 1);
}
@Override
public Stream slidingMap(BiFunction mapper, int increment) {
assertNotClosed();
return slidingMap(mapper, increment, false);
}
@Override
public Stream slidingMap(TriFunction mapper) {
assertNotClosed();
return slidingMap(mapper, 1);
}
@Override
public Stream slidingMap(TriFunction mapper, int increment) {
assertNotClosed();
return slidingMap(mapper, increment, false);
}
@Override
public EntryStream mapToEntry(final Function> mapper) {
assertNotClosed();
final Function mapper2 = Fn.identity();
if (mapper == mapper2) {
return EntryStream.of((Stream>) this);
}
return EntryStream.of(map(mapper));
}
@Override
public EntryStream mapToEntry(final Function keyMapper, final Function valueMapper) {
assertNotClosed();
final Function> mapper = t -> new SimpleImmutableEntry<>(keyMapper.apply(t), valueMapper.apply(t));
return mapToEntry(mapper);
}
// private static final Predicate> IS_PRESENT = new Predicate>() {
// @Override
// public boolean test(Optional t) {
// return t.isPresent();
// }
// };
//
// private static final Function, Object> OPTIONAL_GET = new Function, Object>() {
// @Override
// public Object apply(Optional