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-android Show documentation
Show all versions of abacus-android Show documentation
A general and simple library for Android
/*
* 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.FileWriter;
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.Collection;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashSet;
import java.util.IdentityHashMap;
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.AtomicLong;
import com.landawn.abacus.DataSet;
import com.landawn.abacus.exception.DuplicatedResultException;
import com.landawn.abacus.type.Type;
import com.landawn.abacus.util.Array;
import com.landawn.abacus.util.BufferedWriter;
import com.landawn.abacus.util.Comparators;
import com.landawn.abacus.util.Duration;
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.Iterators;
import com.landawn.abacus.util.JdbcUtil;
import com.landawn.abacus.util.Joiner;
import com.landawn.abacus.util.ListMultimap;
import com.landawn.abacus.util.Matrix;
import com.landawn.abacus.util.Multimap;
import com.landawn.abacus.util.Multiset;
import com.landawn.abacus.util.MutableBoolean;
import com.landawn.abacus.util.MutableLong;
import com.landawn.abacus.util.N;
import com.landawn.abacus.util.NoCachingNoUpdating.DisposableEntry;
import com.landawn.abacus.util.Nth;
import com.landawn.abacus.util.ObjIterator;
import com.landawn.abacus.util.Objectory;
import com.landawn.abacus.util.Pair;
import com.landawn.abacus.util.Percentage;
import com.landawn.abacus.util.PermutationIterator;
import com.landawn.abacus.util.StringUtil.Strings;
import com.landawn.abacus.util.Timed;
import com.landawn.abacus.util.Try;
import com.landawn.abacus.util.u.Optional;
import com.landawn.abacus.util.u.OptionalDouble;
import com.landawn.abacus.util.function.BiConsumer;
import com.landawn.abacus.util.function.BiFunction;
import com.landawn.abacus.util.function.BiPredicate;
import com.landawn.abacus.util.function.BinaryOperator;
import com.landawn.abacus.util.function.BooleanSupplier;
import com.landawn.abacus.util.function.Consumer;
import com.landawn.abacus.util.function.Function;
import com.landawn.abacus.util.function.IntFunction;
import com.landawn.abacus.util.function.LongSupplier;
import com.landawn.abacus.util.function.Predicate;
import com.landawn.abacus.util.function.Supplier;
import com.landawn.abacus.util.function.ToDoubleFunction;
import com.landawn.abacus.util.function.ToIntFunction;
import com.landawn.abacus.util.function.ToLongFunction;
import com.landawn.abacus.util.function.TriFunction;
/**
*
* @param
*/
abstract class AbstractStream extends Stream {
AbstractStream(final boolean sorted, final Comparator super T> cmp, final Collection closeHandlers) {
super(sorted, cmp, closeHandlers);
}
@Override
public Stream skip(final long n, final Consumer super T> 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 super T> action) {
final Function super T, ? extends T> mapperForFirst = new Function() {
@Override
public T apply(T t) {
action.accept(t);
return t;
}
};
return mapFirst(mapperForFirst);
}
@Override
public Stream peekLast(final Consumer super T> action) {
final Function super T, ? extends T> mapperForFirst = new Function() {
@Override
public T apply(T t) {
action.accept(t);
return t;
}
};
return mapLast(mapperForFirst);
}
@Override
public Stream removeIf(final Predicate super T> predicate) {
checkArgNotNull(predicate);
return filter(new Predicate() {
@Override
public boolean test(T value) {
return predicate.test(value) == false;
}
});
}
@Override
public Stream removeIf(final Predicate super T> predicate, final Consumer super T> action) {
checkArgNotNull(predicate);
checkArgNotNull(action);
return filter(new Predicate() {
@Override
public boolean test(T value) {
if (predicate.test(value)) {
action.accept(value);
return false;
}
return true;
}
});
}
@Override
public Stream dropWhile(final Predicate super T> predicate, final Consumer super T> action) {
checkArgNotNull(predicate);
checkArgNotNull(action);
return dropWhile(new Predicate() {
@Override
public boolean test(T value) {
if (predicate.test(value)) {
action.accept(value);
return true;
}
return false;
}
});
}
@Override
public Stream step(final long step) {
checkArgPositive(step, "step");
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.skip(skip);
return next;
}
};
return newStream(iterator, sorted, cmp);
}
// @Override
// public Stream biMap(BiFunction super T, ? super T, ? extends R> mapper) {
// return biMap(mapper, false);
// }
//
// @Override
// public Stream triMap(TriFunction super T, ? super T, ? super T, ? extends R> mapper) {
// return triMap(mapper, false);
// }
@Override
public Stream slidingMap(BiFunction super T, ? super T, R> mapper) {
return slidingMap(mapper, 1);
}
@Override
public Stream slidingMap(BiFunction super T, ? super T, R> mapper, int increment) {
return slidingMap(mapper, increment, false);
}
@Override
public Stream slidingMap(TriFunction super T, ? super T, ? super T, R> mapper) {
return slidingMap(mapper, 1);
}
@Override
public Stream slidingMap(TriFunction super T, ? super T, ? super T, R> mapper, int increment) {
return slidingMap(mapper, increment, false);
}
@Override
public EntryStream mapToEntry(final Function super T, ? extends Map.Entry extends K, ? extends V>> mapper) {
final Function mapper2 = Fn.identity();
if (mapper == mapper2) {
return EntryStream.of((Stream>) this);
}
return EntryStream.of(map(mapper));
}
@Override
public EntryStream mapToEntry(final Function super T, ? extends K> keyMapper, final Function super T, ? extends V> valueMapper) {
final Function> mapper = new Function>() {
@Override
public Entry apply(T t) {
return 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