com.landawn.abacus.util.Seq 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) 2017, 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;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import com.landawn.abacus.annotation.Beta;
import com.landawn.abacus.exception.DuplicatedResultException;
import com.landawn.abacus.util.Fn.BiFunctions;
import com.landawn.abacus.util.Fn.FN;
import com.landawn.abacus.util.Fn.Factory;
import com.landawn.abacus.util.Fn.Suppliers;
import com.landawn.abacus.util.u.Nullable;
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.function.BiConsumer;
import com.landawn.abacus.util.function.BiFunction;
import com.landawn.abacus.util.function.Function;
import com.landawn.abacus.util.function.IntFunction;
import com.landawn.abacus.util.function.Supplier;
import com.landawn.abacus.util.stream.Collector;
/**
* It's an read-only wrapper for Collection
to support more daily used/functional methods.
* All the operations are null safety. And an empty String
/Array
/Collection
/Optional
/Nullable
will be returned if possible, instead of null.
*
*
* Seq
should not be passed as a parameter or returned as a result because it's a pure utility class for the operations/calculation based on Collection/Array
*
* @since 0.8
*
* @author Haiyang Li
*/
@Beta
public final class Seq extends ImmutableCollection {
@SuppressWarnings("rawtypes")
private static final Seq EMPTY = new Seq<>(Collections.EMPTY_LIST);
/**
* The returned Seq
and the specified Collection
are backed by the same data.
* Any changes to one will appear in the other.
*
* @param c
*/
Seq(final Collection c) {
super(c == null ? Collections.EMPTY_LIST : c);
}
public static Seq empty() {
return EMPTY;
}
public static Seq just(T t) {
return of(t);
}
@SafeVarargs
public static Seq of(final T... a) {
if (N.isNullOrEmpty(a)) {
return EMPTY;
}
return of(Arrays.asList(a));
}
/**
* The returned Seq
and the specified Collection
are backed by the same data.
* Any changes to one will appear in the other.
*
* @param c
* @return
*/
public static Seq of(Collection c) {
if (N.isNullOrEmpty(c)) {
return EMPTY;
}
return new Seq<>(c);
}
/**
*
* @param map
* @return
*/
public static Seq> of(Map map) {
if (N.isNullOrEmpty(map)) {
return EMPTY;
}
return of(map.entrySet());
}
// /**
// * Returns the Collection
the Seq
is backed with recursively.
// *
// * @return
// */
// public Collection interior() {
// if (coll == null) {
// return coll;
// }
//
// Collection tmp = coll;
//
// if (tmp instanceof Seq) {
// while (tmp instanceof Seq) {
// tmp = ((Seq) tmp).coll;
// }
// }
//
// if (tmp instanceof SubCollection) {
// while (tmp instanceof SubCollection) {
// tmp = ((SubCollection) tmp).c;
// }
// }
//
// if (tmp instanceof Seq) {
// return ((Seq) tmp).interior();
// } else {
// return tmp;
// }
// }
@Override
public boolean contains(Object e) {
if (N.isNullOrEmpty(coll)) {
return false;
}
return coll.contains(e);
}
@Override
public boolean containsAll(Collection> c) {
if (N.isNullOrEmpty(c)) {
return true;
} else if (N.isNullOrEmpty(coll)) {
return false;
}
return coll.containsAll(c);
}
public boolean containsAll(Object[] a) {
if (N.isNullOrEmpty(a)) {
return true;
} else if (N.isNullOrEmpty(coll)) {
return false;
}
return containsAll(Arrays.asList(a));
}
public boolean containsAny(Collection> c) {
if (N.isNullOrEmpty(coll) || N.isNullOrEmpty(c)) {
return false;
}
return !disjoint(c);
}
public boolean containsAny(Object[] a) {
if (N.isNullOrEmpty(coll) || N.isNullOrEmpty(a)) {
return false;
}
return !disjoint(a);
}
public boolean disjoint(final Collection> c) {
return N.disjoint(this.coll, c);
}
public boolean disjoint(final Object[] a) {
if (N.isNullOrEmpty(coll) || N.isNullOrEmpty(a)) {
return true;
}
return disjoint(Arrays.asList(a));
}
/**
*
* @param b
* @return
* @see IntList#intersection(IntList)
*/
public List intersection(Collection> b) {
return N.intersection(coll, b);
}
public List intersection(final Object[] a) {
return N.intersection(coll, Array.asList(a));
}
/**
*
* @param b
* @return
* @see IntList#difference(IntList)
*/
public List difference(Collection> b) {
return N.difference(coll, b);
}
public List difference(final Object[] a) {
return N.difference(coll, Array.asList(a));
}
/**
*
* @param b
* @return this.difference(b).addAll(b.difference(this))
* @see IntList#symmetricDifference(IntList)
*/
public List symmetricDifference(Collection extends T> b) {
return N.symmetricDifference(coll, b);
}
public List symmetricDifference(final T[] a) {
return N.symmetricDifference(coll, Array.asList(a));
}
public int occurrencesOf(final Object objectToFind) {
return N.isNullOrEmpty(coll) ? 0 : N.occurrencesOf(coll, objectToFind);
}
@SuppressWarnings("rawtypes")
public Nullable min() {
return size() == 0 ? (Nullable) Nullable.empty() : Nullable.of((T) N.min((Collection) coll));
}
public Nullable min(Comparator super T> cmp) {
return size() == 0 ? (Nullable) Nullable.empty() : Nullable.of(N.min(coll, cmp));
}
@SuppressWarnings("rawtypes")
public Nullable minBy(final Function super T, ? extends Comparable> keyMapper) {
return min(Fn.comparingBy(keyMapper));
}
@SuppressWarnings("rawtypes")
public Nullable max() {
return size() == 0 ? (Nullable) Nullable.empty() : Nullable.of((T) N.max((Collection) coll));
}
public Nullable max(Comparator super T> cmp) {
return size() == 0 ? (Nullable) Nullable.empty() : Nullable.of(N.max(coll, cmp));
}
@SuppressWarnings("rawtypes")
public Nullable maxBy(final Function super T, ? extends Comparable> keyMapper) {
return max(Fn.comparingBy(keyMapper));
}
@SuppressWarnings("rawtypes")
public Nullable median() {
return size() == 0 ? (Nullable) Nullable.empty() : Nullable.of((T) N.median((Collection) coll));
}
public Nullable median(Comparator super T> cmp) {
return size() == 0 ? (Nullable) Nullable.empty() : Nullable.of(N.median(coll, cmp));
}
@SuppressWarnings("rawtypes")
public Nullable kthLargest(final int k) {
N.checkArgPositive(k, "k");
return size() < k ? (Nullable) Nullable.empty() : Nullable.of((T) N.kthLargest((Collection) coll, k));
}
public Nullable kthLargest(final int k, Comparator super T> cmp) {
N.checkArgPositive(k, "k");
return size() < k ? (Nullable) Nullable.empty() : Nullable.of(N.kthLargest(coll, k, cmp));
}
public int sumInt(final Try.ToIntFunction super T, E> mapper) throws E {
if (N.isNullOrEmpty(coll)) {
return 0;
}
return N.sumInt(coll, mapper);
}
public long sumLong(final Try.ToLongFunction super T, E> mapper) throws E {
if (N.isNullOrEmpty(coll)) {
return 0L;
}
return N.sumLong(coll, mapper);
}
public double sumDouble(final Try.ToDoubleFunction super T, E> mapper) throws E {
if (N.isNullOrEmpty(coll)) {
return 0L;
}
return N.sumDouble(coll, mapper);
}
public OptionalDouble averageInt(final Try.ToIntFunction super T, E> mapper) throws E {
return N.averageInt(coll, mapper);
}
public OptionalDouble averageLong(final Try.ToLongFunction super T, E> mapper) throws E {
return N.averageLong(coll, mapper);
}
public OptionalDouble averageDouble(final Try.ToDoubleFunction super T, E> mapper) throws E {
return N.averageDouble(coll, mapper);
}
public void foreach(final Try.Consumer super T, E> action) throws E {
N.forEach(coll, action);
}
public void forEach(final Try.Consumer super T, E> action, Try.Runnable onComplete) throws E, E2 {
N.forEach(coll, action);
onComplete.run();
}
// public void forEach(int fromIndex, final int toIndex, final Consumer super T> action) throws E {
// N.forEach(coll, fromIndex, toIndex, action);
// }
public void forEach(final Try.IndexedConsumer super T, E> action) throws E {
N.forEach(coll, action);
}
public void forEach(final Try.Function super T, ? extends Collection, E> flatMapper,
final Try.BiConsumer super T, ? super U, E2> action) throws E, E2 {
N.forEach(coll, flatMapper, action);
}
public void forEach(
final Try.Function super T, ? extends Collection, E> flatMapper, final Try.Function super T2, ? extends Collection, E2> flatMapper2,
final Try.TriConsumer super T, ? super T2, ? super T3, E3> action) throws E, E2, E3 {
N.forEach(coll, flatMapper, flatMapper2, action);
}
public void forEachNonNull(final Try.Consumer super T, E> action) throws E {
N.forEachNonNull(coll, action);
}
public void forEachNonNull(final Try.Function super T, ? extends Collection, E> flatMapper,
final Try.BiConsumer super T, ? super U, E2> action) throws E, E2 {
N.forEachNonNull(coll, flatMapper, action);
}
public void forEachNonNull(
final Try.Function super T, ? extends Collection, E> flatMapper, final Try.Function super T2, ? extends Collection, E2> flatMapper2,
final Try.TriConsumer super T, ? super T2, ? super T3, E3> action) throws E, E2, E3 {
N.forEachNonNull(coll, flatMapper, flatMapper2, action);
}
public void forEachPair(final Try.BiConsumer super T, ? super T, E> action) throws E {
forEachPair(action, 1);
}
public void forEachPair(final Try.BiConsumer super T, ? super T, E> action, final int increment) throws E {
N.checkArgNotNull(action);
final int windowSize = 2;
N.checkArgument(windowSize > 0 && increment > 0, "windowSize=%s and increment=%s must be bigger than 0", windowSize, increment);
if (N.isNullOrEmpty(coll)) {
return;
}
final Iterator iter = coll.iterator();
Iterators.forEachPair(iter, action, increment);
}
public void forEachTriple(final Try.TriConsumer super T, ? super T, ? super T, E> action) throws E {
forEachTriple(action, 1);
}
public void forEachTriple(final Try.TriConsumer super T, ? super T, ? super T, E> action, final int increment) throws E {
N.checkArgNotNull(action);
final int windowSize = 3;
N.checkArgument(windowSize > 0 && increment > 0, "windowSize=%s and increment=%s must be bigger than 0", windowSize, increment);
if (N.isNullOrEmpty(coll)) {
return;
}
final Iterator iter = coll.iterator();
Iterators.forEachTriple(iter, action, increment);
}
public Nullable first() {
return N.first(coll);
}
public Optional firstNonNull() {
return N.firstNonNull(coll);
}
/**
* Return at most first n
elements.
*
* @param n
* @return
*/
public List first(final int n) {
N.checkArgument(n >= 0, "'n' can't be negative: " + n);
if (N.isNullOrEmpty(coll) || n == 0) {
return new ArrayList<>();
} else if (coll.size() <= n) {
return new ArrayList<>(coll);
} else if (coll instanceof List) {
return new ArrayList<>(((List) coll).subList(0, n));
} else {
final List result = new ArrayList<>(N.min(n, coll.size()));
int cnt = 0;
for (T e : coll) {
result.add(e);
if (++cnt == n) {
break;
}
}
return result;
}
}
public Nullable last() {
return N.last(coll);
}
public Optional lastNonNull() {
return N.lastNonNull(coll);
}
/**
* Return at most last n
elements.
*
* @param n
* @return
*/
public List last(final int n) {
N.checkArgument(n >= 0, "'n' can't be negative: " + n);
if (N.isNullOrEmpty(coll) || n == 0) {
return new ArrayList<>();
} else if (coll.size() <= n) {
return new ArrayList<>(coll);
} else if (coll instanceof List) {
return new ArrayList<>(((List) coll).subList(coll.size() - n, coll.size()));
} else {
final List result = new ArrayList<>(N.min(n, coll.size()));
final Iterator iter = coll.iterator();
int offset = coll.size() - n;
while (offset-- > 0) {
iter.next();
}
while (iter.hasNext()) {
result.add(iter.next());
}
return result;
}
}
public Nullable findFirst(Try.Predicate super T, E> predicate) throws E {
return Iterables.findFirst(coll, predicate);
}
public Nullable findLast(Try.Predicate super T, E> predicate) throws E {
return Iterables.findLast(coll, predicate);
}
public OptionalInt findFirstIndex(Try.Predicate super T, E> predicate) throws E {
return Iterables.findFirstIndex(coll, predicate);
}
public OptionalInt findLastIndex(Try.Predicate super T, E> predicate) throws E {
return Iterables.findLastIndex(coll, predicate);
}
public Nullable findFirstOrLast(final Try.Predicate super T, E> predicateForFirst,
final Try.Predicate super T, E2> predicateForLast) throws E, E2 {
if (N.isNullOrEmpty(coll)) {
return Nullable. empty();
}
final Nullable res = findFirst(predicateForFirst);
return res.isPresent() ? res : findLast(predicateForLast);
}
public OptionalInt findFirstOrLastIndex(final Try.Predicate super T, E> predicateForFirst,
final Try.Predicate super T, E2> predicateForLast) throws E, E2 {
if (N.isNullOrEmpty(coll)) {
return OptionalInt.empty();
}
final OptionalInt res = findFirstIndex(predicateForFirst);
return res.isPresent() ? res : findLastIndex(predicateForLast);
}
public Pair, Nullable> findFirstAndLast(final Try.Predicate super T, E> predicate) throws E {
return findFirstAndLast(predicate, predicate);
}
public Pair, Nullable> findFirstAndLast(final Try.Predicate super T, E> predicateForFirst,
final Try.Predicate super T, E2> predicateForLast) throws E, E2 {
if (N.isNullOrEmpty(coll)) {
return Pair.of(Nullable. empty(), Nullable. empty());
}
return Pair.of(findFirst(predicateForFirst), findLast(predicateForLast));
}
public Pair findFirstAndLastIndex(final Try.Predicate super T, E> predicate) throws E {
return findFirstAndLastIndex(predicate, predicate);
}
public Pair findFirstAndLastIndex(final Try.Predicate super T, E> predicateForFirst,
final Try.Predicate super T, E2> predicateForLast) throws E, E2 {
if (N.isNullOrEmpty(coll)) {
return Pair.of(OptionalInt.empty(), OptionalInt.empty());
}
return Pair.of(findFirstIndex(predicateForFirst), findLastIndex(predicateForLast));
}
public boolean allMatch(Try.Predicate super T, E> filter) throws E {
if (N.isNullOrEmpty(coll)) {
return true;
}
for (T e : coll) {
if (filter.test(e) == false) {
return false;
}
}
return true;
}
public boolean anyMatch(Try.Predicate super T, E> filter) throws E {
if (N.isNullOrEmpty(coll)) {
return false;
}
for (T e : coll) {
if (filter.test(e)) {
return true;
}
}
return false;
}
public boolean noneMatch(Try.Predicate super T, E> filter) throws E {
if (N.isNullOrEmpty(coll)) {
return true;
}
for (T e : coll) {
if (filter.test(e)) {
return false;
}
}
return true;
}
public boolean nMatch(final int atLeast, final int atMost, final Try.Predicate super T, E> filter) throws E {
N.checkArgNotNegative(atLeast, "atLeast");
N.checkArgNotNegative(atMost, "atMost");
N.checkArgument(atLeast <= atMost, "'atLeast' must be <= 'atMost'");
long cnt = 0;
for (T e : coll) {
if (filter.test(e)) {
if (++cnt > atMost) {
return false;
}
}
}
return cnt >= atLeast && cnt <= atMost;
}
public boolean hasDuplicates() {
return N.hasDuplicates(coll, false);
}
public int count(Try.Predicate super T, E> filter) throws E {
return N.count(coll, filter);
}
public List filter(Try.Predicate super T, E> filter) throws E {
return N.filter(coll, filter);
}
public List filter(Try.Predicate super T, E> filter, final int max) throws E {
return N.filter(coll, filter, max);
}
public , E extends Exception> C filter(Try.Predicate super T, E> filter, IntFunction extends C> supplier) throws E {
return N.filter(coll, filter, supplier);
}
public , E extends Exception> C filter(Try.Predicate super T, E> filter, final int max, IntFunction extends C> supplier)
throws E {
return N.filter(coll, filter, max, supplier);
}
public List takeWhile(Try.Predicate super T, E> filter) throws E {
N.checkArgNotNull(filter);
final List result = new ArrayList<>(N.min(9, size()));
if (N.isNullOrEmpty(coll)) {
return result;
}
for (T e : coll) {
if (filter.test(e)) {
result.add(e);
} else {
break;
}
}
return result;
}
public List takeWhileInclusive(Try.Predicate super T, E> filter) throws E {
N.checkArgNotNull(filter);
final List result = new ArrayList<>(N.min(9, size()));
if (N.isNullOrEmpty(coll)) {
return result;
}
for (T e : coll) {
result.add(e);
if (filter.test(e) == false) {
break;
}
}
return result;
}
public List dropWhile(Try.Predicate super T, E> filter) throws E {
N.checkArgNotNull(filter);
final List result = new ArrayList<>(N.min(9, size()));
if (N.isNullOrEmpty(coll)) {
return result;
}
final Iterator iter = iterator();
T e = null;
while (iter.hasNext()) {
e = iter.next();
if (filter.test(e) == false) {
result.add(e);
break;
}
}
while (iter.hasNext()) {
result.add(iter.next());
}
return result;
}
public List skipUntil(final Try.Predicate super T, E> filter) throws E {
N.checkArgNotNull(filter);
final List result = new ArrayList<>(N.min(9, size()));
if (N.isNullOrEmpty(coll)) {
return result;
}
final Iterator iter = iterator();
T e = null;
while (iter.hasNext()) {
e = iter.next();
if (filter.test(e)) {
result.add(e);
break;
}
}
while (iter.hasNext()) {
result.add(iter.next());
}
return result;
}
public List map(final Try.Function super T, ? extends R, E> func) throws E {
return N.map(coll, func);
}
public BooleanList mapToBoolean(final Try.ToBooleanFunction super T, E> func) throws E {
return N.mapToBoolean(coll, func);
}
public CharList mapToChar(final Try.ToCharFunction super T, E> func) throws E {
return N.mapToChar(coll, func);
}
public ByteList mapToByte(final Try.ToByteFunction super T, E> func) throws E {
return N.mapToByte(coll, func);
}
public ShortList mapToShort(final Try.ToShortFunction super T, E> func) throws E {
return N.mapToShort(coll, func);
}
public IntList mapToInt(final Try.ToIntFunction super T, E> func) throws E {
return N.mapToInt(coll, func);
}
public LongList mapToLong(final Try.ToLongFunction super T, E> func) throws E {
return N.mapToLong(coll, func);
}
public FloatList mapToFloat(final Try.ToFloatFunction super T, E> func) throws E {
return N.mapToFloat(coll, func);
}
public DoubleList mapToDouble(final Try.ToDoubleFunction super T, E> func) throws E {
return N.mapToDouble(coll, func);
}
public List flatMap(final Try.Function super T, ? extends Collection extends R>, E> func) throws E {
N.checkArgNotNull(func);
final List result = new ArrayList<>(size() > N.MAX_ARRAY_SIZE / 2 ? N.MAX_ARRAY_SIZE : size() * 2);
if (N.isNullOrEmpty(coll)) {
return result;
}
for (T e : coll) {
result.addAll(func.apply(e));
}
return result;
}
public List flattMap(final Try.Function super T, ? extends R[], E> func) throws E {
N.checkArgNotNull(func);
final List result = new ArrayList<>(size() > N.MAX_ARRAY_SIZE / 2 ? N.MAX_ARRAY_SIZE : size() * 2);
if (N.isNullOrEmpty(coll)) {
return result;
}
R[] a = null;
for (T e : coll) {
a = func.apply(e);
if (N.notNullOrEmpty(a)) {
if (a.length < 9) {
for (R r : a) {
result.add(r);
}
} else {
result.addAll(Arrays.asList(a));
}
}
}
return result;
}
public BooleanList flatMapToBoolean(final Try.Function super T, ? extends Collection, E> func) throws E {
N.checkArgNotNull(func);
final BooleanList result = new BooleanList(size() > N.MAX_ARRAY_SIZE / 2 ? N.MAX_ARRAY_SIZE : size() * 2);
if (N.isNullOrEmpty(coll)) {
return result;
}
for (T e : coll) {
for (boolean b : func.apply(e)) {
result.add(b);
}
}
return result;
}
public BooleanList flattMapToBoolean(final Try.Function super T, boolean[], E> func) throws E {
N.checkArgNotNull(func);
final BooleanList result = new BooleanList(size() > N.MAX_ARRAY_SIZE / 2 ? N.MAX_ARRAY_SIZE : size() * 2);
if (N.isNullOrEmpty(coll)) {
return result;
}
for (T e : coll) {
result.addAll(func.apply(e));
}
return result;
}
public CharList flatMapToChar(final Try.Function super T, ? extends Collection, E> func) throws E {
N.checkArgNotNull(func);
final CharList result = new CharList(size() > N.MAX_ARRAY_SIZE / 2 ? N.MAX_ARRAY_SIZE : size() * 2);
if (N.isNullOrEmpty(coll)) {
return result;
}
for (T e : coll) {
for (char b : func.apply(e)) {
result.add(b);
}
}
return result;
}
public CharList flattMapToChar(final Try.Function super T, char[], E> func) throws E {
N.checkArgNotNull(func);
final CharList result = new CharList(size() > N.MAX_ARRAY_SIZE / 2 ? N.MAX_ARRAY_SIZE : size() * 2);
if (N.isNullOrEmpty(coll)) {
return result;
}
for (T e : coll) {
result.addAll(func.apply(e));
}
return result;
}
public ByteList flatMapToByte(final Try.Function super T, ? extends Collection, E> func) throws E {
N.checkArgNotNull(func);
final ByteList result = new ByteList(size() > N.MAX_ARRAY_SIZE / 2 ? N.MAX_ARRAY_SIZE : size() * 2);
if (N.isNullOrEmpty(coll)) {
return result;
}
for (T e : coll) {
for (byte b : func.apply(e)) {
result.add(b);
}
}
return result;
}
public ByteList flattMapToByte(final Try.Function super T, byte[], E> func) throws E {
N.checkArgNotNull(func);
final ByteList result = new ByteList(size() > N.MAX_ARRAY_SIZE / 2 ? N.MAX_ARRAY_SIZE : size() * 2);
if (N.isNullOrEmpty(coll)) {
return result;
}
for (T e : coll) {
result.addAll(func.apply(e));
}
return result;
}
public ShortList flatMapToShort(final Try.Function super T, ? extends Collection, E> func) throws E {
N.checkArgNotNull(func);
final ShortList result = new ShortList(size() > N.MAX_ARRAY_SIZE / 2 ? N.MAX_ARRAY_SIZE : size() * 2);
if (N.isNullOrEmpty(coll)) {
return result;
}
for (T e : coll) {
for (short b : func.apply(e)) {
result.add(b);
}
}
return result;
}
public ShortList flattMapToShort(final Try.Function super T, short[], E> func) throws E {
N.checkArgNotNull(func);
final ShortList result = new ShortList(size() > N.MAX_ARRAY_SIZE / 2 ? N.MAX_ARRAY_SIZE : size() * 2);
if (N.isNullOrEmpty(coll)) {
return result;
}
for (T e : coll) {
result.addAll(func.apply(e));
}
return result;
}
public IntList flatMapToInt(final Try.Function super T, ? extends Collection, E> func) throws E {
N.checkArgNotNull(func);
final IntList result = new IntList(size() > N.MAX_ARRAY_SIZE / 2 ? N.MAX_ARRAY_SIZE : size() * 2);
if (N.isNullOrEmpty(coll)) {
return result;
}
for (T e : coll) {
for (int b : func.apply(e)) {
result.add(b);
}
}
return result;
}
public IntList flattMapToInt(final Try.Function super T, int[], E> func) throws E {
N.checkArgNotNull(func);
final IntList result = new IntList(size() > N.MAX_ARRAY_SIZE / 2 ? N.MAX_ARRAY_SIZE : size() * 2);
if (N.isNullOrEmpty(coll)) {
return result;
}
for (T e : coll) {
result.addAll(func.apply(e));
}
return result;
}
public LongList flatMapToLong(final Try.Function super T, ? extends Collection, E> func) throws E {
N.checkArgNotNull(func);
final LongList result = new LongList(size() > N.MAX_ARRAY_SIZE / 2 ? N.MAX_ARRAY_SIZE : size() * 2);
if (N.isNullOrEmpty(coll)) {
return result;
}
for (T e : coll) {
for (long b : func.apply(e)) {
result.add(b);
}
}
return result;
}
public LongList flattMapToLong(final Try.Function super T, long[], E> func) throws E {
N.checkArgNotNull(func);
final LongList result = new LongList(size() > N.MAX_ARRAY_SIZE / 2 ? N.MAX_ARRAY_SIZE : size() * 2);
if (N.isNullOrEmpty(coll)) {
return result;
}
for (T e : coll) {
result.addAll(func.apply(e));
}
return result;
}
public FloatList flatMapToFloat(final Try.Function super T, ? extends Collection, E> func) throws E {
N.checkArgNotNull(func);
final FloatList result = new FloatList(size() > N.MAX_ARRAY_SIZE / 2 ? N.MAX_ARRAY_SIZE : size() * 2);
if (N.isNullOrEmpty(coll)) {
return result;
}
for (T e : coll) {
for (float b : func.apply(e)) {
result.add(b);
}
}
return result;
}
public FloatList flattMapToFloat(final Try.Function super T, float[], E> func) throws E {
N.checkArgNotNull(func);
final FloatList result = new FloatList(size() > N.MAX_ARRAY_SIZE / 2 ? N.MAX_ARRAY_SIZE : size() * 2);
if (N.isNullOrEmpty(coll)) {
return result;
}
for (T e : coll) {
result.addAll(func.apply(e));
}
return result;
}
public DoubleList flatMapToDouble(final Try.Function super T, ? extends Collection, E> func) throws E {
N.checkArgNotNull(func);
final DoubleList result = new DoubleList(size() > N.MAX_ARRAY_SIZE / 2 ? N.MAX_ARRAY_SIZE : size() * 2);
if (N.isNullOrEmpty(coll)) {
return result;
}
for (T e : coll) {
for (double b : func.apply(e)) {
result.add(b);
}
}
return result;
}
public DoubleList flattMapToDouble(final Try.Function super T, double[], E> func) throws E {
N.checkArgNotNull(func);
final DoubleList result = new DoubleList(size() > N.MAX_ARRAY_SIZE / 2 ? N.MAX_ARRAY_SIZE : size() * 2);
if (N.isNullOrEmpty(coll)) {
return result;
}
for (T e : coll) {
result.addAll(func.apply(e));
}
return result;
}
public List flatMap(final Try.Function super T, ? extends Collection, E> mapper,
final Try.BiFunction super T, ? super U, ? extends R, E2> func) throws E, E2 {
N.checkArgNotNull(mapper);
N.checkArgNotNull(func);
final List result = new ArrayList(N.max(9, coll.size()));
for (T e : coll) {
final Collection c = mapper.apply(e);
if (N.notNullOrEmpty(c)) {
for (U u : c) {
result.add(func.apply(e, u));
}
}
}
return result;
}
public List flatMap(
final Try.Function super T, ? extends Collection, E> mapper2, final Try.Function super T2, ? extends Collection, E2> mapper3,
final Try.TriFunction super T, ? super T2, ? super T3, R, E3> func) throws E, E2, E3 {
N.checkArgNotNull(mapper2);
N.checkArgNotNull(mapper3);
N.checkArgNotNull(func);
if (N.isNullOrEmpty(coll)) {
return new ArrayList();
}
final List result = new ArrayList(N.max(9, coll.size()));
for (T e : coll) {
final Collection c2 = mapper2.apply(e);
if (N.notNullOrEmpty(c2)) {
for (T2 t2 : c2) {
final Collection c3 = mapper3.apply(t2);
if (N.notNullOrEmpty(c3)) {
for (T3 t3 : c3) {
result.add(func.apply(e, t2, t3));
}
}
}
}
}
return result;
}
/**
* For better performance, comparing to {@code Stream}.
*
* @param filter
* @param mapper
* @return
* @throws E
* @throws E2
*/
@Beta
public List filterThenMap(final Try.Predicate super T, E> filter,
final Try.Function super T, ? extends R, E2> mapper) throws E, E2 {
N.checkArgNotNull(filter);
N.checkArgNotNull(mapper);
final List result = new ArrayList<>();
for (T e : coll) {
if (filter.test(e)) {
result.add(mapper.apply(e));
}
}
return result;
}
/**
* For better performance, comparing to {@code Stream}.
*
* @param filter
* @param mapper
* @return
* @throws E
* @throws E2
*/
@Beta
public List filterThenFlatMap(final Try.Predicate super T, E> filter,
final Try.Function super T, ? extends Collection extends R>, E2> mapper) throws E, E2 {
N.checkArgNotNull(filter);
N.checkArgNotNull(mapper);
final List result = new ArrayList<>();
Collection extends R> c = null;
for (T e : coll) {
if (filter.test(e)) {
c = mapper.apply(e);
if (N.notNullOrEmpty(c)) {
result.addAll(c);
}
}
}
return result;
}
/**
* For better performance, comparing to {@code Stream}.
*
* @param mapper
* @param filter
* @return
* @throws E
* @throws E2
*/
@Beta
public List mapThenFilter(final Try.Function super T, ? extends R, E> mapper,
final Try.Predicate super R, E2> filter) throws E, E2 {
N.checkArgNotNull(mapper);
N.checkArgNotNull(filter);
final List result = new ArrayList<>();
R r = null;
for (T e : coll) {
r = mapper.apply(e);
if (filter.test(r)) {
result.add(r);
}
}
return result;
}
/**
* For better performance, comparing to {@code Stream}.
*
* @param mapper
* @param filter
* @return
* @throws E
* @throws E2
*/
@Beta
public List flatMapThenFilter(final Try.Function super T, ? extends Collection extends R>, E> mapper,
final Try.Predicate super R, E2> filter) throws E, E2 {
N.checkArgNotNull(mapper);
N.checkArgNotNull(filter);
final List result = new ArrayList<>();
Collection extends R> c = null;
for (T e : coll) {
c = mapper.apply(e);
if (N.notNullOrEmpty(c)) {
for (R r : c) {
if (filter.test(r)) {
result.add(r);
}
}
}
}
return result;
}
/**
* For better performance, comparing to {@code Stream}.
*
* @param filter
* @param action
* @return
* @throws E
* @throws E2
*/
@Beta
public void filterThenForEach(final Try.Predicate super T, E> filter,
final Try.Consumer super T, E2> action) throws E, E2 {
N.checkArgNotNull(filter);
N.checkArgNotNull(action);
for (T e : coll) {
if (filter.test(e)) {
action.accept(e);
}
}
}
/**
* For better performance, comparing to {@code Stream}.
*
* @param mapper
* @param action
* @return
* @throws E
* @throws E2
*/
@Beta
public void mapThenForEach(final Try.Function super T, ? extends R, E> mapper,
final Try.Consumer super R, E2> action) throws E, E2 {
N.checkArgNotNull(mapper);
N.checkArgNotNull(action);
for (T e : coll) {
action.accept(mapper.apply(e));
}
}
/**
* For better performance, comparing to {@code Stream}.
*
* @param mapper
* @param action
* @return
* @throws E
* @throws E2
*/
@Beta
public void flatMapThenForEach(final Try.Function super T, ? extends Collection extends R>, E> mapper,
final Try.Consumer super R, E2> action) throws E, E2 {
N.checkArgNotNull(mapper);
N.checkArgNotNull(action);
Collection extends R> c = null;
for (T e : coll) {
c = mapper.apply(e);
if (N.notNullOrEmpty(c)) {
for (R r : c) {
action.accept(r);
}
}
}
}
/**
* Merge series of adjacent elements which satisfy the given predicate using the merger function.
*
* Example:
*
*
* Seq.of(new Integer[0]).collapse((a, b) -> a < b, (a, b) -> a + b) => []
* Seq.of(1).collapse((a, b) -> a < b, (a, b) -> a + b) => [1]
* Seq.of(1, 2).collapse((a, b) -> a < b, (a, b) -> a + b) => [3]
* Seq.of(1, 2, 3).collapse((a, b) -> a < b, (a, b) -> a + b) => [6]
* Seq.of(1, 2, 3, 3, 2, 1).collapse((a, b) -> a < b, (a, b) -> a + b) => [6, 3, 2, 1]
*
*
*
* @param collapsible
* @param mergeFunction
* @return
*/
public List collapse(final Try.BiPredicate super T, ? super T, E> collapsible,
final Try.BiFunction super T, ? super T, T, E2> mergeFunction) throws E, E2 {
N.checkArgNotNull(collapsible);
N.checkArgNotNull(mergeFunction);
final List result = new ArrayList<>();
final Iterator iter = iterator();
boolean hasNext = false;
T next = null;
while (hasNext || iter.hasNext()) {
T res = hasNext ? next : (next = iter.next());
while ((hasNext = iter.hasNext())) {
if (collapsible.test(next, (next = iter.next()))) {
res = mergeFunction.apply(res, next);
} else {
break;
}
}
result.add(res);
}
return result;
}
public List collapse(final Try.BiPredicate super T, ? super T, E> collapsible, final U init,
final Try.BiFunction super U, ? super T, U, E2> op) throws E, E2 {
N.checkArgNotNull(collapsible);
N.checkArgNotNull(op);
final List result = new ArrayList<>();
final Iterator iter = iterator();
boolean hasNext = false;
T next = null;
while (hasNext || iter.hasNext()) {
U res = op.apply(init, hasNext ? next : (next = iter.next()));
while ((hasNext = iter.hasNext())) {
if (collapsible.test(next, (next = iter.next()))) {
res = op.apply(res, next);
} else {
break;
}
}
result.add(res);
}
return result;
}
public List> collapse(final Try.BiPredicate super T, ? super T, E> collapsible) throws E {
return collapse(collapsible, Suppliers. ofList());
}
public , E extends Exception> List collapse(final Try.BiPredicate super T, ? super T, E> collapsible,
final Supplier extends C> collectionSupplier) throws E {
N.checkArgNotNull(collapsible);
N.checkArgNotNull(collectionSupplier);
final List result = new ArrayList<>();
final Iterator iter = iterator();
boolean hasNext = false;
T next = null;
while (hasNext || iter.hasNext()) {
final C c = collectionSupplier.get();
c.add(hasNext ? next : (next = iter.next()));
while ((hasNext = iter.hasNext())) {
if (collapsible.test(next, (next = iter.next()))) {
c.add(next);
} else {
break;
}
}
result.add(c);
}
return result;
}
/**
* Merge series of adjacent elements which satisfy the given predicate using the merger function.
*
* Example:
*
*
* Seq.of(new Integer[0]).collapse((a, b) -> a < b, Collectors.summingInt(Fn.unboxI())) => []
* Seq.of(1).collapse((a, b) -> a < b, Collectors.summingInt(Fn.unboxI())) => [1]
* Seq.of(1, 2).collapse((a, b) -> a < b, Collectors.summingInt(Fn.unboxI())) => [3]
* Seq.of(1, 2, 3).collapse((a, b) -> a < b, Collectors.summingInt(Fn.unboxI())) => [6]
* Seq.of(1, 2, 3, 3, 2, 1).collapse((a, b) -> a < b, Collectors.summingInt(Fn.unboxI())) => [6, 3, 2, 1]
*
*
*
* @param collapsible
* @param collector
* @return
*/
public List collapse(final Try.BiPredicate super T, ? super T, E> collapsible, final Collector super T, A, R> collector)
throws E {
N.checkArgNotNull(collapsible);
N.checkArgNotNull(collector);
final List result = new ArrayList<>();
final Supplier supplier = collector.supplier();
final BiConsumer accumulator = collector.accumulator();
final Function finisher = collector.finisher();
final Iterator iter = iterator();
boolean hasNext = false;
T next = null;
while (hasNext || iter.hasNext()) {
final A c = supplier.get();
accumulator.accept(c, hasNext ? next : (next = iter.next()));
while ((hasNext = iter.hasNext())) {
if (collapsible.test(next, (next = iter.next()))) {
accumulator.accept(c, next);
} else {
break;
}
}
result.add(finisher.apply(c));
}
return result;
}
/**
* Returns a {@code Stream} produced by iterative application of a accumulation function
* to an initial element {@code identity} and next element of the current stream.
* Produces a {@code Stream} consisting of {@code identity}, {@code acc(identity, value1)},
* {@code acc(acc(identity, value1), value2)}, etc.
*
* Example:
*
*
* Seq.of(new Integer[0]).scan((a, b) -> a + b) => []
* Seq.of(1).scan((a, b) -> a + b) => [1]
* Seq.of(1, 2).scan((a, b) -> a + b) => [1, 3]
* Seq.of(1, 2, 3).scan((a, b) -> a + b) => [1, 3, 6]
* Seq.of(1, 2, 3, 3, 2, 1).scan((a, b) -> a + b) => [1, 3, 6, 9, 11, 12]
*
*
*
* @param accumulator the accumulation function
* @return
*/
public List scan(final Try.BiFunction super T, ? super T, T, E> accumulator) throws E {
N.checkArgNotNull(accumulator);
final List result = new ArrayList<>();
final Iterator iter = iterator();
T next = null;
if (iter.hasNext()) {
result.add((next = iter.next()));
}
while (iter.hasNext()) {
result.add((next = accumulator.apply(next, iter.next())));
}
return result;
}
/**
* Returns a {@code Stream} produced by iterative application of a accumulation function
* to an initial element {@code identity} and next element of the current stream.
* Produces a {@code Stream} consisting of {@code identity}, {@code acc(identity, value1)},
* {@code acc(acc(identity, value1), value2)}, etc.
*
* Example:
*
*
* Seq.of(new Integer[0]).scan(10, (a, b) -> a + b) => []
* Seq.of(1).scan(10, (a, b) -> a + b) => [11]
* Seq.of(1, 2).scan(10, (a, b) -> a + b) => [11, 13]
* Seq.of(1, 2, 3).scan(10, (a, b) -> a + b) => [11, 13, 16]
* Seq.of(1, 2, 3, 3, 2, 1).scan(10, (a, b) -> a + b) => [11, 13, 16, 19, 21, 22]
*
*
*
* @param init the initial value. it's only used once by accumulator
to calculate the fist element in the returned stream.
* It will be ignored if this stream is empty and won't be the first element of the returned stream.
*
* @param accumulator the accumulation function
* @return
*/
public List scan(final U init, final Try.BiFunction super U, ? super T, U, E> accumulator) throws E {
return scan(init, accumulator, false);
}
/**
*
* @param init
* @param accumulator
* @param initIncluded
* @return
* @throws E
*/
public List scan(final U init, final Try.BiFunction super U, ? super T, U, E> accumulator, boolean initIncluded) throws E {
N.checkArgNotNull(accumulator);
final List result = new ArrayList<>();
if (initIncluded) {
result.add(init);
}
final Iterator iter = iterator();
U next = init;
while (iter.hasNext()) {
result.add((next = accumulator.apply(next, iter.next())));
}
return result;
}
/**
* This is equivalent to:
*
*
* if (isEmpty()) {
* return Nullable.empty();
* }
*
* final Iterator iter = iterator();
* T result = iter.next();
*
* while (iter.hasNext()) {
* result = accumulator.apply(result, iter.next());
* }
*
* return Nullable.of(result);
*
*
*
* @param accumulator
* @return
*/
public Nullable reduce(Try.BinaryOperator accumulator) throws E {
N.checkArgNotNull(accumulator);
if (isEmpty()) {
return Nullable.empty();
}
final Iterator iter = iterator();
T result = iter.next();
while (iter.hasNext()) {
result = accumulator.apply(result, iter.next());
}
return Nullable.of(result);
}
/**
* This is equivalent to:
*
*
* if (isEmpty()) {
* return identity;
* }
*
* final Iterator iter = iterator();
* U result = identity;
*
* while (iter.hasNext()) {
* result = accumulator.apply(result, iter.next());
* }
*
* return result;
*
*
*
* @param identity
* @param accumulator
* @return
*/
public U reduce(final U identity, final Try.BiFunction accumulator) throws E {
N.checkArgNotNull(accumulator);
if (isEmpty()) {
return identity;
}
final Iterator iter = iterator();
U result = identity;
while (iter.hasNext()) {
result = accumulator.apply(result, iter.next());
}
return result;
}
public R collect(final Supplier supplier, final Try.BiConsumer super R, ? super T, E> accumulator) throws E {
N.checkArgNotNull(supplier);
N.checkArgNotNull(accumulator);
final R result = supplier.get();
for (T e : coll) {
accumulator.accept(result, e);
}
return result;
}
public R collect(final Supplier supplier, final Try.BiConsumer super A, ? super T, E> accumulator,
final Try.Function finisher) throws E, E2 {
N.checkArgNotNull(supplier);
N.checkArgNotNull(accumulator);
N.checkArgNotNull(finisher);
final A result = supplier.get();
for (T e : coll) {
accumulator.accept(result, e);
}
return finisher.apply(result);
}
public R collect(final Collector super T, A, R> collector) {
N.checkArgNotNull(collector);
final BiConsumer accumulator = collector.accumulator();
final A result = collector.supplier().get();
for (T e : coll) {
accumulator.accept(result, e);
}
return collector.finisher().apply(result);
}
public R collect(final java.util.stream.Collector super T, A, R> collector) {
N.checkArgNotNull(collector);
final java.util.function.BiConsumer accumulator = collector.accumulator();
final A result = collector.supplier().get();
for (T e : coll) {
accumulator.accept(result, e);
}
return collector.finisher().apply(result);
}
public RR collectThenApply(final Collector downstream, final Try.Function super R, ? extends RR, E> mapper)
throws E {
return mapper.apply(collect(downstream));
}
public RR collectThenApply(final java.util.stream.Collector downstream,
final Try.Function super R, ? extends RR, E> mapper) throws E {
return mapper.apply(collect(downstream));
}
public void collectThenAccept(final Collector downstream, final Try.Consumer super R, E> consumer) throws E {
consumer.accept(collect(downstream));
}
public void collectThenAccept(final java.util.stream.Collector downstream, final Try.Consumer super R, E> consumer)
throws E {
consumer.accept(collect(downstream));
}
@SafeVarargs
public final List append(T... a) {
if (N.isNullOrEmpty(a)) {
return toList();
}
return append(Arrays.asList(a));
}
public List append(final Collection extends T> c) {
return N.concat(this, c);
}
@SafeVarargs
public final List prepend(T... a) {
if (N.isNullOrEmpty(a)) {
return toList();
}
return prepend(Arrays.asList(a));
}
public List