Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2018, 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.Collection;
import java.util.Comparator;
import java.util.Deque;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import java.util.RandomAccess;
import com.landawn.abacus.util.u.Nullable;
import com.landawn.abacus.util.u.Optional;
import com.landawn.abacus.util.u.OptionalByte;
import com.landawn.abacus.util.u.OptionalChar;
import com.landawn.abacus.util.u.OptionalDouble;
import com.landawn.abacus.util.u.OptionalFloat;
import com.landawn.abacus.util.u.OptionalInt;
import com.landawn.abacus.util.u.OptionalLong;
import com.landawn.abacus.util.u.OptionalShort;
import com.landawn.abacus.util.function.Function;
/**
* The methods in this class should only read the input {@code Collections/Arrays}, not modify any of them.
*
* @author Haiyang Li
* @since 1.2.7
*/
public class Iterables {
Iterables() {
// singleton.
}
public static OptionalChar min(final char... a) {
return a == null || a.length == 0 ? OptionalChar.empty() : OptionalChar.of(N.min(a));
}
public static OptionalByte min(final byte... a) {
return a == null || a.length == 0 ? OptionalByte.empty() : OptionalByte.of(N.min(a));
}
public static OptionalShort min(final short... a) {
return a == null || a.length == 0 ? OptionalShort.empty() : OptionalShort.of(N.min(a));
}
public static OptionalInt min(final int... a) {
return a == null || a.length == 0 ? OptionalInt.empty() : OptionalInt.of(N.min(a));
}
public static OptionalLong min(final long... a) {
return a == null || a.length == 0 ? OptionalLong.empty() : OptionalLong.of(N.min(a));
}
public static OptionalFloat min(final float... a) {
return a == null || a.length == 0 ? OptionalFloat.empty() : OptionalFloat.of(N.min(a));
}
public static OptionalDouble min(final double... a) {
return a == null || a.length == 0 ? OptionalDouble.empty() : OptionalDouble.of(N.min(a));
}
public static OptionalChar max(final char... a) {
return a == null || a.length == 0 ? OptionalChar.empty() : OptionalChar.of(N.max(a));
}
public static OptionalByte max(final byte... a) {
return a == null || a.length == 0 ? OptionalByte.empty() : OptionalByte.of(N.max(a));
}
public static OptionalShort max(final short... a) {
return a == null || a.length == 0 ? OptionalShort.empty() : OptionalShort.of(N.max(a));
}
public static OptionalInt max(final int... a) {
return a == null || a.length == 0 ? OptionalInt.empty() : OptionalInt.of(N.max(a));
}
public static OptionalLong max(final long... a) {
return a == null || a.length == 0 ? OptionalLong.empty() : OptionalLong.of(N.max(a));
}
public static OptionalFloat max(final float... a) {
return a == null || a.length == 0 ? OptionalFloat.empty() : OptionalFloat.of(N.max(a));
}
public static OptionalDouble max(final double... a) {
return a == null || a.length == 0 ? OptionalDouble.empty() : OptionalDouble.of(N.max(a));
}
/**
* Min.
*
* @param the generic type
* @param c the c
* @return the nullable
*/
public static > Nullable min(final Collection extends T> c) {
return N.isNullOrEmpty(c) ? Nullable. empty() : Nullable.of(N.min(c));
}
/**
* Min.
*
* @param the generic type
* @param a the a
* @return the nullable
*/
public static > Nullable min(final T[] a) {
return N.isNullOrEmpty(a) ? Nullable. empty() : Nullable.of(N.min(a));
}
/**
* Min.
*
* @param the generic type
* @param c the c
* @param cmp the cmp
* @return the nullable
*/
public static Nullable min(final Collection extends T> c, final Comparator super T> cmp) {
return N.isNullOrEmpty(c) ? Nullable. empty() : Nullable.of(N.min(c, cmp));
}
/**
* Min.
*
* @param the generic type
* @param a the a
* @param cmp the cmp
* @return the nullable
*/
public static Nullable min(final T[] a, final Comparator super T> cmp) {
return N.isNullOrEmpty(a) ? Nullable. empty() : Nullable.of(N.min(a, cmp));
}
/**
* Min by.
*
* @param the generic type
* @param c the c
* @param keyMapper the key mapper
* @return the nullable
*/
@SuppressWarnings("rawtypes")
public static Nullable minBy(final Collection extends T> c, final Function super T, ? extends Comparable> keyMapper) {
return min(c, Fn.comparingBy(keyMapper));
}
/**
* Min by.
*
* @param the generic type
* @param a the a
* @param keyMapper the key mapper
* @return the nullable
*/
@SuppressWarnings("rawtypes")
public static Nullable minBy(final T[] a, final Function super T, ? extends Comparable> keyMapper) {
return min(a, Fn.comparingBy(keyMapper));
}
/**
* Max.
*
* @param the generic type
* @param c the c
* @return the nullable
*/
public static > Nullable max(final Collection extends T> c) {
return N.isNullOrEmpty(c) ? Nullable. empty() : Nullable.of(N.max(c));
}
/**
* Max.
*
* @param the generic type
* @param a the a
* @return the nullable
*/
public static > Nullable max(final T[] a) {
return N.isNullOrEmpty(a) ? Nullable. empty() : Nullable.of(N.max(a));
}
/**
* Max.
*
* @param the generic type
* @param c the c
* @param cmp the cmp
* @return the nullable
*/
public static Nullable max(final Collection extends T> c, final Comparator super T> cmp) {
return N.isNullOrEmpty(c) ? Nullable. empty() : Nullable.of(N.max(c, cmp));
}
/**
* Max.
*
* @param the generic type
* @param a the a
* @param cmp the cmp
* @return the nullable
*/
public static Nullable max(final T[] a, final Comparator super T> cmp) {
return N.isNullOrEmpty(a) ? Nullable. empty() : Nullable.of(N.max(a, cmp));
}
/**
* Max by.
*
* @param the generic type
* @param c the c
* @param keyMapper the key mapper
* @return the nullable
*/
@SuppressWarnings("rawtypes")
public static Nullable maxBy(final Collection extends T> c, final Function super T, ? extends Comparable> keyMapper) {
return max(c, Fn.comparingBy(keyMapper));
}
/**
* Max by.
*
* @param the generic type
* @param a the a
* @param keyMapper the key mapper
* @return the nullable
*/
@SuppressWarnings("rawtypes")
public static Nullable maxBy(final T[] a, final Function super T, ? extends Comparable> keyMapper) {
return max(a, Fn.comparingBy(keyMapper));
}
/**
* Median.
*
* @param the generic type
* @param c the c
* @return the nullable
*/
public static > Nullable median(final Collection extends T> c) {
return N.isNullOrEmpty(c) ? Nullable. empty() : Nullable.of(N.median(c));
}
/**
* Median.
*
* @param the generic type
* @param a the a
* @return the nullable
*/
public static > Nullable median(final T[] a) {
return N.isNullOrEmpty(a) ? Nullable. empty() : Nullable.of(N.median(a));
}
/**
* Median.
*
* @param the generic type
* @param c the c
* @param cmp the cmp
* @return the nullable
*/
public static Nullable median(final Collection extends T> c, final Comparator super T> cmp) {
return N.isNullOrEmpty(c) ? Nullable. empty() : Nullable.of(N.median(c, cmp));
}
/**
* Median.
*
* @param the generic type
* @param a the a
* @param cmp the cmp
* @return the nullable
*/
public static Nullable median(final T[] a, final Comparator super T> cmp) {
return N.isNullOrEmpty(a) ? Nullable. empty() : Nullable.of(N.median(a, cmp));
}
/**
* Median by.
*
* @param the generic type
* @param c the c
* @param keyMapper the key mapper
* @return the nullable
*/
@SuppressWarnings("rawtypes")
public static Nullable medianBy(final Collection extends T> c, final Function super T, ? extends Comparable> keyMapper) {
return median(c, Fn.comparingBy(keyMapper));
}
/**
* Median by.
*
* @param the generic type
* @param a the a
* @param keyMapper the key mapper
* @return the nullable
*/
@SuppressWarnings("rawtypes")
public static Nullable medianBy(final T[] a, final Function super T, ? extends Comparable> keyMapper) {
return median(a, Fn.comparingBy(keyMapper));
}
/**
* Kth largest.
*
* @param the generic type
* @param c the c
* @param k the k
* @return the nullable
*/
public static > Nullable kthLargest(final Collection extends T> c, final int k) {
return N.isNullOrEmpty(c) ? Nullable. empty() : Nullable.of(N.kthLargest(c, k));
}
/**
* Kth largest.
*
* @param the generic type
* @param a the a
* @param k the k
* @return the nullable
*/
public static > Nullable kthLargest(final T[] a, final int k) {
return N.isNullOrEmpty(a) ? Nullable. empty() : Nullable.of(N.kthLargest(a, k));
}
/**
* Kth largest.
*
* @param the generic type
* @param c the c
* @param k the k
* @param cmp the cmp
* @return the nullable
*/
public static Nullable kthLargest(final Collection extends T> c, final int k, final Comparator super T> cmp) {
return N.isNullOrEmpty(c) ? Nullable. empty() : Nullable.of(N.kthLargest(c, k, cmp));
}
/**
* Kth largest.
*
* @param the generic type
* @param a the a
* @param k the k
* @param cmp the cmp
* @return the nullable
*/
public static Nullable kthLargest(final T[] a, final int k, final Comparator super T> cmp) {
return N.isNullOrEmpty(a) ? Nullable. empty() : Nullable.of(N.kthLargest(a, k, cmp));
}
/**
* Index of.
*
* @param c the c
* @param objToFind the obj to find
* @return the optional int
*/
public static OptionalInt indexOf(final Collection> c, final Object objToFind) {
if (N.isNullOrEmpty(c)) {
return OptionalInt.empty();
}
int idx = 0;
for (Object e : c) {
if (N.equals(e, objToFind)) {
return OptionalInt.of(idx);
}
idx++;
}
return OptionalInt.empty();
}
/**
* Index of.
*
* @param a the a
* @param objToFind the obj to find
* @return the optional int
*/
public static OptionalInt indexOf(final Object[] a, final Object objToFind) {
if (N.isNullOrEmpty(a)) {
return OptionalInt.empty();
}
for (int i = 0, len = a.length; i < len; i++) {
if (N.equals(a[i], objToFind)) {
return OptionalInt.of(i);
}
}
return OptionalInt.empty();
}
/**
* Last index of.
*
* @param c the c
* @param objToFind the obj to find
* @return the optional int
*/
public static OptionalInt lastIndexOf(final Collection> c, final Object objToFind) {
if (N.isNullOrEmpty(c)) {
return OptionalInt.empty();
}
final int size = c.size();
if (c instanceof List) {
final List