Please wait. This can take some minutes ...
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.
com.landawn.abacus.util.N Maven / Gradle / Ivy
Go to download
A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.
/*
* Copyright (c) 2015, 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.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.RandomAccess;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import com.landawn.abacus.annotation.Beta;
import com.landawn.abacus.exception.DuplicatedResultException;
import com.landawn.abacus.exception.UncheckedException;
import com.landawn.abacus.parser.DeserializationConfig;
import com.landawn.abacus.parser.JSONDeserializationConfig;
import com.landawn.abacus.parser.JSONDeserializationConfig.JDC;
import com.landawn.abacus.parser.JSONSerializationConfig;
import com.landawn.abacus.parser.XMLDeserializationConfig;
import com.landawn.abacus.parser.XMLDeserializationConfig.XDC;
import com.landawn.abacus.parser.XMLSerializationConfig;
import com.landawn.abacus.type.Type;
import com.landawn.abacus.util.Fn.Factory;
import com.landawn.abacus.util.Fn.Suppliers;
import com.landawn.abacus.util.Tuple.Tuple2;
import com.landawn.abacus.util.Tuple.Tuple3;
import com.landawn.abacus.util.Tuple.Tuple4;
import com.landawn.abacus.util.Tuple.Tuple5;
import com.landawn.abacus.util.u.Nullable;
import com.landawn.abacus.util.function.BiFunction;
import com.landawn.abacus.util.function.BiPredicate;
import com.landawn.abacus.util.function.IntBiFunction;
import com.landawn.abacus.util.function.IntFunction;
import com.landawn.abacus.util.function.Predicate;
import com.landawn.abacus.util.function.Supplier;
import com.landawn.abacus.util.stream.ObjIteratorEx;
import com.landawn.abacus.util.stream.Stream;
/**
*
* Note: This class includes codes copied from Apache Commons Lang, Google Guava and other open source projects under the Apache License 2.0.
* The methods copied from other libraries/frameworks/projects may be modified in this class.
*
* Class N
is a general java utility class. It provides the most daily used operations for Object/primitive types/String/Array/Collection/Map/Entity...:
*
* When to throw exception? It's designed to avoid throwing any unnecessary
* exception if the contract defined by method is not broken. for example, if
* user tries to reverse a null or empty String. the input String will be
* returned. But exception will be thrown if trying to repeat/swap a null or
* empty string or operate Array/Collection by adding/removing...
*
* @author Haiyang Li
*
* @version $Revision: 0.8 $ 07/03/10
*
* @see com.landawn.abacus.util.Array
* @see com.landawn.abacus.util.Iterables
* @see com.landawn.abacus.util.Iterators
* @see com.landawn.abacus.util.Maps
* @see com.landawn.abacus.util.StringUtil
* @see com.landawn.abacus.util.IOUtil
*/
public final class N extends CommonUtil {
public static final int CPU_CORES = Runtime.getRuntime().availableProcessors();
public static final int MAX_MEMORY_IN_MB = (int) (Runtime.getRuntime().maxMemory() / (1024 * 1024));
public static final JavaVersion JAVA_VERSION = JavaVersion.of(System.getProperty("java.version"));
private static final float LOAD_FACTOR_FOR_FLAT_MAP = 1.75f;
private static final int LOAD_FACTOR_FOR_TWO_FLAT_MAP = 2;
static final AsyncExecutor asyncExecutor = new AsyncExecutor(Math.max(64, Math.min(IOUtil.CPU_CORES * 8, IOUtil.MAX_MEMORY_IN_MB / 1024) * 32),
Math.max(256, (IOUtil.MAX_MEMORY_IN_MB / 1024) * 64), 180L, TimeUnit.SECONDS);
static final ScheduledExecutorService SCHEDULED_EXECUTOR;
static {
final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(Math.max(64, CPU_CORES));
executor.setKeepAliveTime(180, TimeUnit.SECONDS);
executor.allowCoreThreadTimeOut(true);
executor.setRemoveOnCancelPolicy(true);
SCHEDULED_EXECUTOR = MoreExecutors.getExitingScheduledExecutorService(executor);
}
private N() {
// Utility class.
}
/**
*
* @param a
* @param valueToFind
* @return
*/
public static int occurrencesOf(final boolean[] a, final boolean valueToFind) {
if (isNullOrEmpty(a)) {
return 0;
}
int occurrences = 0;
for (boolean element : a) {
if (element == valueToFind) {
occurrences++;
}
}
return occurrences;
}
/**
*
* @param a
* @param valueToFind
* @return
*/
public static int occurrencesOf(final char[] a, final char valueToFind) {
if (isNullOrEmpty(a)) {
return 0;
}
int occurrences = 0;
for (char element : a) {
if (element == valueToFind) {
occurrences++;
}
}
return occurrences;
}
/**
*
* @param a
* @param valueToFind
* @return
*/
public static int occurrencesOf(final byte[] a, final byte valueToFind) {
if (isNullOrEmpty(a)) {
return 0;
}
int occurrences = 0;
for (byte element : a) {
if (element == valueToFind) {
occurrences++;
}
}
return occurrences;
}
/**
*
* @param a
* @param valueToFind
* @return
*/
public static int occurrencesOf(final short[] a, final short valueToFind) {
if (isNullOrEmpty(a)) {
return 0;
}
int occurrences = 0;
for (short element : a) {
if (element == valueToFind) {
occurrences++;
}
}
return occurrences;
}
/**
*
* @param a
* @param valueToFind
* @return
*/
public static int occurrencesOf(final int[] a, final int valueToFind) {
if (isNullOrEmpty(a)) {
return 0;
}
int occurrences = 0;
for (int element : a) {
if (element == valueToFind) {
occurrences++;
}
}
return occurrences;
}
/**
*
* @param a
* @param valueToFind
* @return
*/
public static int occurrencesOf(final long[] a, final long valueToFind) {
if (isNullOrEmpty(a)) {
return 0;
}
int occurrences = 0;
for (long element : a) {
if (element == valueToFind) {
occurrences++;
}
}
return occurrences;
}
/**
*
* @param a
* @param valueToFind
* @return
*/
public static int occurrencesOf(final float[] a, final float valueToFind) {
if (isNullOrEmpty(a)) {
return 0;
}
int occurrences = 0;
for (float element : a) {
if (Float.compare(element, valueToFind) == 0) {
occurrences++;
}
}
return occurrences;
}
/**
*
* @param a
* @param valueToFind
* @return
*/
public static int occurrencesOf(final double[] a, final double valueToFind) {
if (isNullOrEmpty(a)) {
return 0;
}
int occurrences = 0;
for (double element : a) {
if (Double.compare(element, valueToFind) == 0) {
occurrences++;
}
}
return occurrences;
}
/**
*
* @param a
* @param valueToFind
* @return
*/
public static int occurrencesOf(final Object[] a, final Object valueToFind) {
if (isNullOrEmpty(a)) {
return 0;
}
int occurrences = 0;
if (valueToFind == null) {
for (Object element : a) {
if (element == null) {
occurrences++;
}
}
} else {
for (Object element : a) {
if (valueToFind.equals(element)) {
occurrences++;
}
}
}
return occurrences;
}
/**
*
* @param c
* @param valueToFind
* @return
* @see java.util.Collections#frequency(Collection, Object)
*/
public static int occurrencesOf(final Iterable> c, final Object valueToFind) {
if (c == null) {
return 0;
}
long occurrences = 0;
if (valueToFind == null) {
for (Object e : c) {
if (e == null) {
occurrences++;
}
}
} else {
for (Object e : c) {
if (N.equals(e, valueToFind)) {
occurrences++;
}
}
}
return N.toIntExact(occurrences);
}
public static long occurrencesOf(final Iterator> iter, final Object valueToFind) {
if (iter == null) {
return 0;
}
long occurrences = 0;
if (valueToFind == null) {
while (iter.hasNext()) {
if (iter.next() == null) {
occurrences++;
}
}
} else {
while (iter.hasNext()) {
if (N.equals(iter.next(), valueToFind)) {
occurrences++;
}
}
}
return occurrences;
}
/**
*
* @param str
* @param substr
* @return
* @see StringUtil#occurrencesOf(String, String)
*/
public static int occurrencesOf(final String str, final String substr) {
return StringUtil.occurrencesOf(str, substr);
}
/**
*
* @param
* @param a
* @return
* @see Multiset#from(Collection)
*/
public static Map occurrencesMap(final T[] a) {
return occurrencesMap(a, Suppliers. ofMap());
}
/**
*
* @param
* @param a
* @param mapSupplier
* @return
* @see Multiset#from(Collection)
*/
public static Map occurrencesMap(final T[] a, final Supplier> mapSupplier) {
if (N.isNullOrEmpty(a)) {
return mapSupplier.get();
}
final Map map = mapSupplier.get();
for (T e : a) {
map.merge(e, 1, (o, n) -> o + n);
}
return map;
}
/**
*
* @param
* @param c
* @return
* @see Multiset#from(Collection)
*/
public static Map occurrencesMap(final Iterable c) {
return occurrencesMap(c, Suppliers. ofMap());
}
/**
*
* @param
* @param c
* @param mapSupplier
* @return
* @see Multiset#from(Collection)
*/
public static Map occurrencesMap(final Iterable c, final Supplier> mapSupplier) {
if (c == null) {
return mapSupplier.get();
}
final Map map = mapSupplier.get();
for (T e : c) {
map.merge(e, 1, (o, n) -> o + n);
}
return map;
}
/**
*
* @param
* @param c
* @return
* @see Multiset#from(Iterator)
*/
public static Map occurrencesMap(final Iterator iter) {
return occurrencesMap(iter, Suppliers. ofMap());
}
/**
*
* @param
* @param iter
* @param mapSupplier
* @return
* @see Multiset#from(Iterator)
*/
public static Map occurrencesMap(final Iterator iter, final Supplier> mapSupplier) {
if (iter == null) {
return mapSupplier.get();
}
final Map map = mapSupplier.get();
while (iter.hasNext()) {
map.merge(iter.next(), 1, (o, n) -> o + n);
}
return map;
}
/**
*
* @param a
* @param valueToFind
* @return
*/
public static boolean contains(final boolean[] a, final boolean valueToFind) {
return indexOf(a, valueToFind) != INDEX_NOT_FOUND;
}
/**
*
* @param a
* @param valueToFind
* @return
*/
public static boolean contains(final char[] a, final char valueToFind) {
return indexOf(a, valueToFind) != INDEX_NOT_FOUND;
}
/**
*
* @param a
* @param valueToFind
* @return
*/
public static boolean contains(final byte[] a, final byte valueToFind) {
return indexOf(a, valueToFind) != INDEX_NOT_FOUND;
}
/**
*
* @param a
* @param valueToFind
* @return
*/
public static boolean contains(final short[] a, final short valueToFind) {
return indexOf(a, valueToFind) != INDEX_NOT_FOUND;
}
/**
*
* @param a
* @param valueToFind
* @return
*/
public static boolean contains(final int[] a, final int valueToFind) {
return indexOf(a, valueToFind) != INDEX_NOT_FOUND;
}
/**
*
* @param a
* @param valueToFind
* @return
*/
public static boolean contains(final long[] a, final long valueToFind) {
return indexOf(a, valueToFind) != INDEX_NOT_FOUND;
}
/**
*
* @param a
* @param valueToFind
* @return
*/
public static boolean contains(final float[] a, final float valueToFind) {
return indexOf(a, valueToFind) != INDEX_NOT_FOUND;
}
/**
*
* @param a
* @param valueToFind
* @return
*/
public static boolean contains(final double[] a, final double valueToFind) {
return indexOf(a, valueToFind) != INDEX_NOT_FOUND;
}
/**
*
* @param a
* @param valueToFind
* @return
*/
public static boolean contains(final Object[] a, final Object valueToFind) {
return indexOf(a, valueToFind) != INDEX_NOT_FOUND;
}
/**
*
* @param c
* @param valueToFind
* @return
*/
public static boolean contains(final Collection> c, final Object valueToFind) {
if (isNullOrEmpty(c)) {
return false;
}
return c.contains(valueToFind);
}
public static boolean contains(final Iterator> iter, final Object valueToFind) {
if (iter == null) {
return false;
}
while (iter.hasNext()) {
if (N.equals(iter.next(), valueToFind)) {
return true;
}
}
return false;
}
/**
*
* @param c
* @param valuesToFind the objs to find
* @return
*/
public static boolean containsAll(final Collection> c, final Collection> valuesToFind) {
if (N.isNullOrEmpty(valuesToFind)) {
return true;
} else if (N.isNullOrEmpty(c)) {
return false;
}
return c.containsAll(valuesToFind);
}
/**
*
* @param c
* @param valuesToFind the objs to find
* @return
*/
public static boolean containsAll(final Collection> c, final Object[] valuesToFind) {
if (N.isNullOrEmpty(valuesToFind)) {
return true;
} else if (N.isNullOrEmpty(c)) {
return false;
}
return c.containsAll(Array.asList(valuesToFind));
}
public static boolean containsAll(final Iterator> iter, final Collection> valuesToFind) {
if (N.isNullOrEmpty(valuesToFind)) {
return true;
} else if (iter == null) {
return false;
}
final Set> set = new HashSet<>(valuesToFind);
while (iter.hasNext()) {
if (set.remove(iter.next()) && (set.size() == 0)) {
return true;
}
}
return false;
}
/**
*
* @param c
* @param valuesToFind the objs to find
* @return
*/
public static boolean containsAny(final Collection> c, final Collection> valuesToFind) {
if (N.isNullOrEmpty(c) || N.isNullOrEmpty(valuesToFind)) {
return false;
}
return !N.disjoint(c, valuesToFind);
}
/**
*
* @param c
* @param valuesToFind the objs to find
* @return
*/
public static boolean containsAny(final Collection> c, final Object[] valuesToFind) {
if (N.isNullOrEmpty(c) || N.isNullOrEmpty(valuesToFind)) {
return false;
}
return !N.disjoint(c, Array.asList(valuesToFind));
}
public static boolean containsAny(final Iterator> iter, final Set> valuesToFind) {
if (iter == null || N.isNullOrEmpty(valuesToFind)) {
return false;
}
while (iter.hasNext()) {
if (valuesToFind.contains(iter.next())) {
return true;
}
}
return false;
}
/**
* Gets the only element.
*
* @param
* @param iterable
* @return throws DuplicatedResultException if there are more than one elements in the specified {@code iterable}.
*/
public static Nullable getOnlyElement(Iterable extends T> iterable) throws DuplicatedResultException {
if (iterable == null) {
return Nullable.empty();
}
return getOnlyElement(iterable.iterator());
}
/**
* Gets the only element.
*
* @param
* @param iter
* @return throws DuplicatedResultException if there are more than one elements in the specified {@code iter}.
* @throws DuplicatedResultException the duplicated result exception
*/
public static Nullable getOnlyElement(final Iterator extends T> iter) throws DuplicatedResultException {
if (iter == null) {
return Nullable.empty();
}
final T first = iter.next();
if (iter.hasNext()) {
throw new DuplicatedResultException("Expected at most one element but was: [" + StringUtil.concat(first, ", ", iter.next(), "...]"));
}
return Nullable.of(first);
}
/**
* Returns consecutive sub arrays of an array, each of the same size (the final list may be smaller),
* or an empty List if the specified array is {@code null} or empty.
*
* @param a
* @param chunkSize the desired size of each sub array (the last may be smaller).
* @return
*/
public static List split(final boolean[] a, final int chunkSize) {
checkArgPositive(chunkSize, "chunkSize");
if (isNullOrEmpty(a)) {
return new ArrayList<>();
}
final int len = a.length;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int fromIndex = 0, toIndex = a.length; fromIndex < toIndex; fromIndex += chunkSize) {
res.add(copyOfRange(a, fromIndex, fromIndex <= toIndex - chunkSize ? fromIndex + chunkSize : toIndex));
}
return res;
}
/**
* Returns consecutive sub arrays of an array, each of the same chunkSize (the final list may be smaller),
* or an empty List if the specified array is {@code null} or empty.
*
* @param a
* @param fromIndex
* @param toIndex
* @param chunkSize the desired size of each sub array (the last may be smaller).
* @return
*/
public static List split(final boolean[] a, final int fromIndex, final int toIndex, final int chunkSize) {
checkFromToIndex(fromIndex, toIndex, len(a));
checkArgPositive(chunkSize, "chunkSize");
if (isNullOrEmpty(a) || fromIndex == toIndex) {
return new ArrayList<>();
}
final int len = toIndex - fromIndex;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int i = fromIndex; i < toIndex; i = i <= toIndex - chunkSize ? i + chunkSize : toIndex) {
res.add(copyOfRange(a, i, i <= toIndex - chunkSize ? i + chunkSize : toIndex));
}
return res;
}
/**
* Returns consecutive sub arrays of an array, each of the same chunkSize (the final list may be smaller),
* or an empty List if the specified array is {@code null} or empty.
*
* @param a
* @param chunkSize the desired size of each sub array (the last may be smaller).
* @return
*/
public static List split(final char[] a, final int chunkSize) {
checkArgPositive(chunkSize, "chunkSize");
if (isNullOrEmpty(a)) {
return new ArrayList<>();
}
final int len = a.length;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int fromIndex = 0, toIndex = a.length; fromIndex < toIndex; fromIndex += chunkSize) {
res.add(copyOfRange(a, fromIndex, fromIndex <= toIndex - chunkSize ? fromIndex + chunkSize : toIndex));
}
return res;
}
/**
* Returns consecutive sub arrays of an array, each of the same chunkSize (the final list may be smaller),
* or an empty List if the specified array is {@code null} or empty.
*
* @param a
* @param fromIndex
* @param toIndex
* @param chunkSize the desired size of each sub array (the last may be smaller).
* @return
*/
public static List split(final char[] a, final int fromIndex, final int toIndex, final int chunkSize) {
checkFromToIndex(fromIndex, toIndex, len(a));
checkArgPositive(chunkSize, "chunkSize");
if (isNullOrEmpty(a) || fromIndex == toIndex) {
return new ArrayList<>();
}
final int len = toIndex - fromIndex;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int i = fromIndex; i < toIndex; i = i <= toIndex - chunkSize ? i + chunkSize : toIndex) {
res.add(copyOfRange(a, i, i <= toIndex - chunkSize ? i + chunkSize : toIndex));
}
return res;
}
/**
* Returns consecutive sub arrays of an array, each of the same chunkSize (the final list may be smaller),
* or an empty List if the specified array is {@code null} or empty.
*
* @param a
* @param chunkSize the desired size of each sub array (the last may be smaller).
* @return
*/
public static List split(final byte[] a, final int chunkSize) {
checkArgPositive(chunkSize, "chunkSize");
if (isNullOrEmpty(a)) {
return new ArrayList<>();
}
final int len = a.length;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int fromIndex = 0, toIndex = a.length; fromIndex < toIndex; fromIndex += chunkSize) {
res.add(copyOfRange(a, fromIndex, fromIndex <= toIndex - chunkSize ? fromIndex + chunkSize : toIndex));
}
return res;
}
/**
* Returns consecutive sub arrays of an array, each of the same chunkSize (the final list may be smaller),
* or an empty List if the specified array is {@code null} or empty.
*
* @param a
* @param fromIndex
* @param toIndex
* @param chunkSize the desired size of each sub array (the last may be smaller).
* @return
*/
public static List split(final byte[] a, final int fromIndex, final int toIndex, final int chunkSize) {
checkFromToIndex(fromIndex, toIndex, len(a));
checkArgPositive(chunkSize, "chunkSize");
if (isNullOrEmpty(a) || fromIndex == toIndex) {
return new ArrayList<>();
}
final int len = toIndex - fromIndex;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int i = fromIndex; i < toIndex; i = i <= toIndex - chunkSize ? i + chunkSize : toIndex) {
res.add(copyOfRange(a, i, i <= toIndex - chunkSize ? i + chunkSize : toIndex));
}
return res;
}
/**
* Returns consecutive sub arrays of an array, each of the same chunkSize (the final list may be smaller),
* or an empty List if the specified array is {@code null} or empty.
*
* @param a
* @param chunkSize the desired size of each sub array (the last may be smaller).
* @return
*/
public static List split(final short[] a, final int chunkSize) {
checkArgPositive(chunkSize, "chunkSize");
if (isNullOrEmpty(a)) {
return new ArrayList<>();
}
final int len = a.length;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int fromIndex = 0, toIndex = a.length; fromIndex < toIndex; fromIndex += chunkSize) {
res.add(copyOfRange(a, fromIndex, fromIndex <= toIndex - chunkSize ? fromIndex + chunkSize : toIndex));
}
return res;
}
/**
* Returns consecutive sub arrays of an array, each of the same chunkSize (the final list may be smaller),
* or an empty List if the specified array is {@code null} or empty.
*
* @param a
* @param fromIndex
* @param toIndex
* @param chunkSize the desired size of each sub array (the last may be smaller).
* @return
*/
public static List split(final short[] a, final int fromIndex, final int toIndex, final int chunkSize) {
checkFromToIndex(fromIndex, toIndex, len(a));
checkArgPositive(chunkSize, "chunkSize");
if (isNullOrEmpty(a) || fromIndex == toIndex) {
return new ArrayList<>();
}
final int len = toIndex - fromIndex;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int i = fromIndex; i < toIndex; i = i <= toIndex - chunkSize ? i + chunkSize : toIndex) {
res.add(copyOfRange(a, i, i <= toIndex - chunkSize ? i + chunkSize : toIndex));
}
return res;
}
/**
* Returns consecutive sub arrays of an array, each of the same chunkSize (the final list may be smaller),
* or an empty List if the specified array is {@code null} or empty.
*
* @param a
* @param chunkSize the desired size of each sub array (the last may be smaller).
* @return
*/
public static List split(final int[] a, final int chunkSize) {
checkArgPositive(chunkSize, "chunkSize");
if (isNullOrEmpty(a)) {
return new ArrayList<>();
}
final int len = a.length;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int fromIndex = 0, toIndex = a.length; fromIndex < toIndex; fromIndex += chunkSize) {
res.add(copyOfRange(a, fromIndex, fromIndex <= toIndex - chunkSize ? fromIndex + chunkSize : toIndex));
}
return res;
}
/**
* Returns consecutive sub arrays of an array, each of the same chunkSize (the final list may be smaller),
* or an empty List if the specified array is {@code null} or empty.
*
* @param a
* @param fromIndex
* @param toIndex
* @param chunkSize the desired size of each sub array (the last may be smaller).
* @return
*/
public static List split(final int[] a, final int fromIndex, final int toIndex, final int chunkSize) {
checkFromToIndex(fromIndex, toIndex, len(a));
checkArgPositive(chunkSize, "chunkSize");
if (isNullOrEmpty(a) || fromIndex == toIndex) {
return new ArrayList<>();
}
final int len = toIndex - fromIndex;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int i = fromIndex; i < toIndex; i = i <= toIndex - chunkSize ? i + chunkSize : toIndex) {
res.add(copyOfRange(a, i, i <= toIndex - chunkSize ? i + chunkSize : toIndex));
}
return res;
}
/**
* Returns consecutive sub arrays of an array, each of the same chunkSize (the final list may be smaller),
* or an empty List if the specified array is {@code null} or empty.
*
* @param a
* @param chunkSize the desired size of each sub array (the last may be smaller).
* @return
*/
public static List split(final long[] a, final int chunkSize) {
checkArgPositive(chunkSize, "chunkSize");
if (isNullOrEmpty(a)) {
return new ArrayList<>();
}
final int len = a.length;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int fromIndex = 0, toIndex = a.length; fromIndex < toIndex; fromIndex += chunkSize) {
res.add(copyOfRange(a, fromIndex, fromIndex <= toIndex - chunkSize ? fromIndex + chunkSize : toIndex));
}
return res;
}
/**
* Returns consecutive sub arrays of an array, each of the same chunkSize (the final list may be smaller),
* or an empty List if the specified array is {@code null} or empty.
*
* @param a
* @param fromIndex
* @param toIndex
* @param chunkSize the desired size of each sub array (the last may be smaller).
* @return
*/
public static List split(final long[] a, final int fromIndex, final int toIndex, final int chunkSize) {
checkFromToIndex(fromIndex, toIndex, len(a));
checkArgPositive(chunkSize, "chunkSize");
if (isNullOrEmpty(a) || fromIndex == toIndex) {
return new ArrayList<>();
}
final int len = toIndex - fromIndex;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int i = fromIndex; i < toIndex; i = i <= toIndex - chunkSize ? i + chunkSize : toIndex) {
res.add(copyOfRange(a, i, i <= toIndex - chunkSize ? i + chunkSize : toIndex));
}
return res;
}
/**
* Returns consecutive sub arrays of an array, each of the same chunkSize (the final list may be smaller),
* or an empty List if the specified array is {@code null} or empty.
*
* @param a
* @param chunkSize the desired size of each sub array (the last may be smaller).
* @return
*/
public static List split(final float[] a, final int chunkSize) {
checkArgPositive(chunkSize, "chunkSize");
if (isNullOrEmpty(a)) {
return new ArrayList<>();
}
final int len = a.length;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int fromIndex = 0, toIndex = a.length; fromIndex < toIndex; fromIndex += chunkSize) {
res.add(copyOfRange(a, fromIndex, fromIndex <= toIndex - chunkSize ? fromIndex + chunkSize : toIndex));
}
return res;
}
/**
* Returns consecutive sub arrays of an array, each of the same chunkSize (the final list may be smaller),
* or an empty List if the specified array is {@code null} or empty.
*
* @param a
* @param fromIndex
* @param toIndex
* @param chunkSize the desired size of each sub array (the last may be smaller).
* @return
*/
public static List split(final float[] a, final int fromIndex, final int toIndex, final int chunkSize) {
checkFromToIndex(fromIndex, toIndex, len(a));
checkArgPositive(chunkSize, "chunkSize");
if (isNullOrEmpty(a) || fromIndex == toIndex) {
return new ArrayList<>();
}
final int len = toIndex - fromIndex;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int i = fromIndex; i < toIndex; i = i <= toIndex - chunkSize ? i + chunkSize : toIndex) {
res.add(copyOfRange(a, i, i <= toIndex - chunkSize ? i + chunkSize : toIndex));
}
return res;
}
/**
* Returns consecutive sub arrays of an array, each of the same chunkSize (the final list may be smaller),
* or an empty List if the specified array is {@code null} or empty.
*
* @param a
* @param chunkSize the desired size of each sub array (the last may be smaller).
* @return
*/
public static List split(final double[] a, final int chunkSize) {
checkArgPositive(chunkSize, "chunkSize");
if (isNullOrEmpty(a)) {
return new ArrayList<>();
}
final int len = a.length;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int fromIndex = 0, toIndex = a.length; fromIndex < toIndex; fromIndex += chunkSize) {
res.add(copyOfRange(a, fromIndex, fromIndex <= toIndex - chunkSize ? fromIndex + chunkSize : toIndex));
}
return res;
}
/**
* Returns consecutive sub arrays of an array, each of the same chunkSize (the final list may be smaller),
* or an empty List if the specified array is {@code null} or empty.
*
* @param a
* @param fromIndex
* @param toIndex
* @param chunkSize the desired size of each sub array (the last may be smaller).
* @return
*/
public static List split(final double[] a, final int fromIndex, final int toIndex, final int chunkSize) {
checkFromToIndex(fromIndex, toIndex, len(a));
checkArgPositive(chunkSize, "chunkSize");
if (isNullOrEmpty(a) || fromIndex == toIndex) {
return new ArrayList<>();
}
final int len = toIndex - fromIndex;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int i = fromIndex; i < toIndex; i = i <= toIndex - chunkSize ? i + chunkSize : toIndex) {
res.add(copyOfRange(a, i, i <= toIndex - chunkSize ? i + chunkSize : toIndex));
}
return res;
}
/**
* Returns consecutive sub arrays of an array, each of the same chunkSize (the final list may be smaller),
* or an empty List if the specified array is {@code null} or empty.
*
* @param
* @param a
* @param chunkSize the desired size of each sub array (the last may be smaller).
* @return
*/
public static List split(final T[] a, final int chunkSize) {
checkArgPositive(chunkSize, "chunkSize");
if (isNullOrEmpty(a)) {
return new ArrayList<>();
}
final int len = a.length;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int fromIndex = 0, toIndex = a.length; fromIndex < toIndex; fromIndex += chunkSize) {
res.add(copyOfRange(a, fromIndex, fromIndex <= toIndex - chunkSize ? fromIndex + chunkSize : toIndex));
}
return res;
}
/**
* Returns consecutive sub arrays of an array, each of the same chunkSize (the final list may be smaller),
* or an empty List if the specified array is {@code null} or empty.
*
* @param
* @param a
* @param fromIndex
* @param toIndex
* @param chunkSize the desired size of each sub array (the last may be smaller).
* @return
*/
public static List split(final T[] a, final int fromIndex, final int toIndex, final int chunkSize) {
checkFromToIndex(fromIndex, toIndex, len(a));
checkArgPositive(chunkSize, "chunkSize");
if (isNullOrEmpty(a) || fromIndex == toIndex) {
return new ArrayList<>();
}
final int len = toIndex - fromIndex;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int i = fromIndex; i < toIndex; i = i <= toIndex - chunkSize ? i + chunkSize : toIndex) {
res.add(copyOfRange(a, i, i <= toIndex - chunkSize ? i + chunkSize : toIndex));
}
return res;
}
/**
* Returns consecutive sub lists of a collection, each of the same chunkSize (the final list may be smaller).
* or an empty List if the specified collection is {@code null} or empty. The order of elements in the original collection is kept
*
* @param
* @param c
* @param chunkSize the desired size of each sub list (the last may be smaller).
* @return
*/
public static List> split(final Iterable extends T> c, final int chunkSize) {
checkArgPositive(chunkSize, "chunkSize");
if (c == null) {
return new ArrayList<>();
} else if (c instanceof Collection) {
final Collection coll = (Collection) c;
return split(coll, 0, coll.size(), chunkSize);
} else {
return N.toList(split(c.iterator(), chunkSize));
}
}
/**
* Returns consecutive sub lists of a collection, each of the same chunkSize (the final list may be smaller).
* or an empty List if the specified collection is {@code null} or empty. The order of elements in the original collection is kept
*
* @param
* @param c
* @param fromIndex
* @param toIndex
* @param chunkSize the desired size of each sub list (the last may be smaller).
* @return
*/
public static List> split(final Collection extends T> c, final int fromIndex, final int toIndex, final int chunkSize) {
checkFromToIndex(fromIndex, toIndex, size(c));
checkArgPositive(chunkSize, "chunkSize");
if (isNullOrEmpty(c) || fromIndex == toIndex) {
return new ArrayList<>();
}
final int len = toIndex - fromIndex;
final List> res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
if (c instanceof List) {
final List list = (List) c;
for (int i = fromIndex; i < toIndex; i = i <= toIndex - chunkSize ? i + chunkSize : toIndex) {
res.add(new ArrayList<>(list.subList(i, i <= toIndex - chunkSize ? i + chunkSize : toIndex)));
}
} else {
final Iterator extends T> iter = c.iterator();
for (int i = 0; i < toIndex;) {
if (i < fromIndex) {
iter.next();
i++;
continue;
}
final List subList = new ArrayList<>(min(chunkSize, toIndex - i));
for (int j = i <= toIndex - chunkSize ? i + chunkSize : toIndex; i < j; i++) {
subList.add(iter.next());
}
res.add(subList);
}
}
return res;
}
public static ObjIterator> split(final Iterator extends T> iter, final int chunkSize) {
N.checkArgument(chunkSize > 0, "'chunkSize' must be greater than 0, can't be: %s", chunkSize);
if (iter == null) {
return ObjIterator.empty();
}
return new ObjIterator<>() {
private final Iterator extends T> iterator = iter;
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public List next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
final List next = new ArrayList<>(chunkSize);
for (int i = 0; i < chunkSize && iterator.hasNext(); i++) {
next.add(iterator.next());
}
return next;
}
};
}
/**
* Returns consecutive substring of the specified string, each of the same length (the final list may be smaller),
* or an empty array if the specified string is {@code null} or empty.
*
* @param str
* @param chunkSize the desired size of each sub String (the last may be smaller).
* @return
*/
public static List split(final CharSequence str, final int chunkSize) {
checkArgPositive(chunkSize, "chunkSize");
if (isNullOrEmpty(str)) {
return new ArrayList<>();
}
return split(str, 0, str.length(), chunkSize);
}
/**
* Returns consecutive substring of the specified string, each of the same length (the final list may be smaller),
* or an empty array if the specified string is {@code null} or empty.
*
* @param str
* @param fromIndex
* @param toIndex
* @param chunkSize the desired size of each sub String (the last may be smaller).
* @return
*/
public static List split(final CharSequence str, final int fromIndex, final int toIndex, final int chunkSize) {
checkFromToIndex(fromIndex, toIndex, len(str));
checkArgPositive(chunkSize, "chunkSize");
if (isNullOrEmpty(str)) {
return new ArrayList<>();
}
final int len = toIndex - fromIndex;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int i = fromIndex; i < toIndex; i = i <= toIndex - chunkSize ? i + chunkSize : toIndex) {
res.add(str.subSequence(i, i <= toIndex - chunkSize ? i + chunkSize : toIndex).toString());
}
return res;
}
/**
*
*
* final int[] a = Array.rangeClosed(1, 7);
* splitByCount(5, 7, true, (fromIndex, toIndex) -> N.copyOfRange(a, fromIndex, toIndex)).forEach(Fn.println()); // [[1], [2], [3], [4, 5], [6, 7]]
* splitByCount(5, 7, false, (fromIndex, toIndex) -> N.copyOfRange(a, fromIndex, toIndex)).forEach(Fn.println()); // [[1, 2], [3, 4], [5], [6], [7]]
*
*
*
* @param
* @param maxCount max count of chunk want to split {@code totalSize} into.
* @param totalSize
* @param smallerFirst
* @param func
* @return the stream
*/
public static Stream splitByCount(final int maxCount, final int totalSize, final boolean smallerFirst, final IntBiFunction func) {
if (smallerFirst) {
return splitByCountSmallerFirst(maxCount, totalSize, func);
} else {
return splitByCountSmallerLast(maxCount, totalSize, func);
}
}
/**
*
*
* final int[] a = Array.rangeClosed(1, 7);
* splitByCountSmallerFirst(5, 7, (fromIndex, toIndex) -> N.copyOfRange(a, fromIndex, toIndex)).forEach(Fn.println()); // [[1], [2], [3], [4, 5], [6, 7]]
* splitByCountSmallerLast(5, 7, (fromIndex, toIndex) -> N.copyOfRange(a, fromIndex, toIndex)).forEach(Fn.println()); // [[1, 2], [3, 4], [5], [6], [7]]
*
*
*
* @param
* @param maxCount
* @param totalSize
* @param func
* @return the stream
*/
static Stream splitByCountSmallerFirst(final int maxCount, final int totalSize, final IntBiFunction func) {
N.checkArgPositive(maxCount, "maxCount");
N.checkArgNotNegative(totalSize, "totalSize");
N.checkArgNotNull(func, "func");
if (totalSize == 0) {
return Stream.empty();
}
final Iterator iter = new ObjIteratorEx<>() {
private final int smallerSize = Math.max(totalSize / maxCount, 1);
private final int biggerSize = totalSize % maxCount == 0 ? totalSize / maxCount : totalSize / maxCount + 1;
private int count = totalSize >= maxCount ? maxCount : totalSize;
private int biggerCount = totalSize % maxCount;
private int cursor = 0;
@Override
public boolean hasNext() {
return cursor < totalSize;
}
@Override
public T next() {
if (cursor >= totalSize) {
throw new NoSuchElementException();
}
return func.apply(cursor, cursor = (count-- > biggerCount ? cursor + smallerSize : cursor + biggerSize));
}
@Override
public void advance(long n) {
N.checkArgNotNegative(n, "n");
if (n > 0) {
while (n-- > 0 && cursor < totalSize) {
cursor = count-- > biggerCount ? cursor + smallerSize : cursor + biggerSize;
}
}
}
@Override
public long count() {
return count;
}
};
return Stream.of(iter);
}
/**
*
*
* final int[] a = Array.rangeClosed(1, 7);
* splitByCountSmallerFirst(5, 7, (fromIndex, toIndex) -> N.copyOfRange(a, fromIndex, toIndex)).forEach(Fn.println()); // [[1], [2], [3], [4, 5], [6, 7]]
* splitByCountSmallerLast(5, 7, (fromIndex, toIndex) -> N.copyOfRange(a, fromIndex, toIndex)).forEach(Fn.println()); // [[1, 2], [3, 4], [5], [6], [7]]
*
*
*
* @param
* @param maxCount
* @param totalSize
* @param func
* @return the stream
*/
static Stream splitByCountSmallerLast(final int maxCount, final int totalSize, final IntBiFunction func) {
N.checkArgPositive(maxCount, "maxCount");
N.checkArgNotNegative(totalSize, "totalSize");
N.checkArgNotNull(func, "func");
if (totalSize == 0) {
return Stream.empty();
}
final Iterator iter = new ObjIteratorEx<>() {
private final int smallerSize = Math.max(totalSize / maxCount, 1);
private final int biggerSize = totalSize % maxCount == 0 ? totalSize / maxCount : totalSize / maxCount + 1;
private int count = totalSize >= maxCount ? maxCount : totalSize;
private int smallerCount = count - totalSize % maxCount;
private int cursor = 0;
@Override
public boolean hasNext() {
return cursor < totalSize;
}
@Override
public T next() {
if (cursor >= totalSize) {
throw new NoSuchElementException();
}
return func.apply(cursor, cursor = (count-- > smallerCount ? cursor + biggerSize : cursor + smallerSize));
}
@Override
public void advance(long n) {
N.checkArgNotNegative(n, "n");
if (n > 0) {
while (n-- > 0 && cursor < totalSize) {
cursor = count-- > smallerCount ? cursor + biggerSize : cursor + smallerSize;
}
}
}
@Override
public long count() {
return count;
}
};
return Stream.of(iter);
}
/**
*
* @param a
* @param b
* @return
*/
public static boolean[] concat(final boolean[] a, final boolean[] b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? EMPTY_BOOLEAN_ARRAY : b.clone();
} else if (isNullOrEmpty(b)) {
return isNullOrEmpty(a) ? EMPTY_BOOLEAN_ARRAY : a.clone();
}
final boolean[] c = new boolean[a.length + b.length];
copy(a, 0, c, 0, a.length);
copy(b, 0, c, a.length, b.length);
return c;
}
/**
*
* @param aa
* @return
*/
@SafeVarargs
public static boolean[] concat(final boolean[]... aa) {
if (isNullOrEmpty(aa)) {
return EMPTY_BOOLEAN_ARRAY;
} else if (aa.length == 1) {
return isNullOrEmpty(aa[0]) ? EMPTY_BOOLEAN_ARRAY : aa[0].clone();
}
int len = 0;
for (boolean[] a : aa) {
if (isNullOrEmpty(a)) {
continue;
}
len += a.length;
}
final boolean[] c = new boolean[len];
int fromIndex = 0;
for (boolean[] a : aa) {
if (isNullOrEmpty(a)) {
continue;
}
System.arraycopy(a, 0, c, fromIndex, a.length);
fromIndex += a.length;
}
return c;
}
/**
*
* @param a
* @param b
* @return
*/
public static char[] concat(final char[] a, final char[] b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? EMPTY_CHAR_ARRAY : b.clone();
} else if (isNullOrEmpty(b)) {
return isNullOrEmpty(a) ? EMPTY_CHAR_ARRAY : a.clone();
}
final char[] c = new char[a.length + b.length];
copy(a, 0, c, 0, a.length);
copy(b, 0, c, a.length, b.length);
return c;
}
/**
*
* @param aa
* @return
*/
@SafeVarargs
public static char[] concat(final char[]... aa) {
if (isNullOrEmpty(aa)) {
return EMPTY_CHAR_ARRAY;
} else if (aa.length == 1) {
return isNullOrEmpty(aa[0]) ? EMPTY_CHAR_ARRAY : aa[0].clone();
}
int len = 0;
for (char[] a : aa) {
if (isNullOrEmpty(a)) {
continue;
}
len += a.length;
}
final char[] c = new char[len];
int fromIndex = 0;
for (char[] a : aa) {
if (isNullOrEmpty(a)) {
continue;
}
System.arraycopy(a, 0, c, fromIndex, a.length);
fromIndex += a.length;
}
return c;
}
/**
*
* @param a
* @param b
* @return
*/
public static byte[] concat(final byte[] a, final byte[] b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? EMPTY_BYTE_ARRAY : b.clone();
} else if (isNullOrEmpty(b)) {
return isNullOrEmpty(a) ? EMPTY_BYTE_ARRAY : a.clone();
}
final byte[] c = new byte[a.length + b.length];
copy(a, 0, c, 0, a.length);
copy(b, 0, c, a.length, b.length);
return c;
}
/**
*
* @param aa
* @return
*/
@SafeVarargs
public static byte[] concat(final byte[]... aa) {
if (isNullOrEmpty(aa)) {
return EMPTY_BYTE_ARRAY;
} else if (aa.length == 1) {
return isNullOrEmpty(aa[0]) ? EMPTY_BYTE_ARRAY : aa[0].clone();
}
int len = 0;
for (byte[] a : aa) {
if (isNullOrEmpty(a)) {
continue;
}
len += a.length;
}
final byte[] c = new byte[len];
int fromIndex = 0;
for (byte[] a : aa) {
if (isNullOrEmpty(a)) {
continue;
}
System.arraycopy(a, 0, c, fromIndex, a.length);
fromIndex += a.length;
}
return c;
}
/**
*
* @param a
* @param b
* @return
*/
public static short[] concat(final short[] a, final short[] b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? EMPTY_SHORT_ARRAY : b.clone();
} else if (isNullOrEmpty(b)) {
return isNullOrEmpty(a) ? EMPTY_SHORT_ARRAY : a.clone();
}
final short[] c = new short[a.length + b.length];
copy(a, 0, c, 0, a.length);
copy(b, 0, c, a.length, b.length);
return c;
}
/**
*
* @param aa
* @return
*/
@SafeVarargs
public static short[] concat(final short[]... aa) {
if (isNullOrEmpty(aa)) {
return EMPTY_SHORT_ARRAY;
} else if (aa.length == 1) {
return isNullOrEmpty(aa[0]) ? EMPTY_SHORT_ARRAY : aa[0].clone();
}
int len = 0;
for (short[] a : aa) {
if (isNullOrEmpty(a)) {
continue;
}
len += a.length;
}
final short[] c = new short[len];
int fromIndex = 0;
for (short[] a : aa) {
if (isNullOrEmpty(a)) {
continue;
}
System.arraycopy(a, 0, c, fromIndex, a.length);
fromIndex += a.length;
}
return c;
}
/**
*
* @param a
* @param b
* @return
*/
public static int[] concat(final int[] a, final int[] b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? EMPTY_INT_ARRAY : b.clone();
} else if (isNullOrEmpty(b)) {
return isNullOrEmpty(a) ? EMPTY_INT_ARRAY : a.clone();
}
final int[] c = new int[a.length + b.length];
copy(a, 0, c, 0, a.length);
copy(b, 0, c, a.length, b.length);
return c;
}
/**
*
* @param aa
* @return
*/
@SafeVarargs
public static int[] concat(final int[]... aa) {
if (isNullOrEmpty(aa)) {
return EMPTY_INT_ARRAY;
} else if (aa.length == 1) {
return isNullOrEmpty(aa[0]) ? EMPTY_INT_ARRAY : aa[0].clone();
}
int len = 0;
for (int[] a : aa) {
if (isNullOrEmpty(a)) {
continue;
}
len += a.length;
}
final int[] c = new int[len];
int fromIndex = 0;
for (int[] a : aa) {
if (isNullOrEmpty(a)) {
continue;
}
System.arraycopy(a, 0, c, fromIndex, a.length);
fromIndex += a.length;
}
return c;
}
/**
*
* @param a
* @param b
* @return
*/
public static long[] concat(final long[] a, final long[] b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? EMPTY_LONG_ARRAY : b.clone();
} else if (isNullOrEmpty(b)) {
return isNullOrEmpty(a) ? EMPTY_LONG_ARRAY : a.clone();
}
final long[] c = new long[a.length + b.length];
copy(a, 0, c, 0, a.length);
copy(b, 0, c, a.length, b.length);
return c;
}
/**
*
* @param aa
* @return
*/
@SafeVarargs
public static long[] concat(final long[]... aa) {
if (isNullOrEmpty(aa)) {
return EMPTY_LONG_ARRAY;
} else if (aa.length == 1) {
return isNullOrEmpty(aa[0]) ? EMPTY_LONG_ARRAY : aa[0].clone();
}
int len = 0;
for (long[] a : aa) {
if (isNullOrEmpty(a)) {
continue;
}
len += a.length;
}
final long[] c = new long[len];
int fromIndex = 0;
for (long[] a : aa) {
if (isNullOrEmpty(a)) {
continue;
}
System.arraycopy(a, 0, c, fromIndex, a.length);
fromIndex += a.length;
}
return c;
}
/**
*
* @param a
* @param b
* @return
*/
public static float[] concat(final float[] a, final float[] b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? EMPTY_FLOAT_ARRAY : b.clone();
} else if (isNullOrEmpty(b)) {
return isNullOrEmpty(a) ? EMPTY_FLOAT_ARRAY : a.clone();
}
final float[] c = new float[a.length + b.length];
copy(a, 0, c, 0, a.length);
copy(b, 0, c, a.length, b.length);
return c;
}
/**
*
* @param aa
* @return
*/
@SafeVarargs
public static float[] concat(final float[]... aa) {
if (isNullOrEmpty(aa)) {
return EMPTY_FLOAT_ARRAY;
} else if (aa.length == 1) {
return isNullOrEmpty(aa[0]) ? EMPTY_FLOAT_ARRAY : aa[0].clone();
}
int len = 0;
for (float[] a : aa) {
if (isNullOrEmpty(a)) {
continue;
}
len += a.length;
}
final float[] c = new float[len];
int fromIndex = 0;
for (float[] a : aa) {
if (isNullOrEmpty(a)) {
continue;
}
System.arraycopy(a, 0, c, fromIndex, a.length);
fromIndex += a.length;
}
return c;
}
/**
*
* @param a
* @param b
* @return
*/
public static double[] concat(final double[] a, final double[] b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? EMPTY_DOUBLE_ARRAY : b.clone();
} else if (isNullOrEmpty(b)) {
return isNullOrEmpty(a) ? EMPTY_DOUBLE_ARRAY : a.clone();
}
final double[] c = new double[a.length + b.length];
copy(a, 0, c, 0, a.length);
copy(b, 0, c, a.length, b.length);
return c;
}
/**
*
* @param aa
* @return
*/
@SafeVarargs
public static double[] concat(final double[]... aa) {
if (isNullOrEmpty(aa)) {
return EMPTY_DOUBLE_ARRAY;
} else if (aa.length == 1) {
return isNullOrEmpty(aa[0]) ? EMPTY_DOUBLE_ARRAY : aa[0].clone();
}
int len = 0;
for (double[] a : aa) {
if (isNullOrEmpty(a)) {
continue;
}
len += a.length;
}
final double[] c = new double[len];
int fromIndex = 0;
for (double[] a : aa) {
if (isNullOrEmpty(a)) {
continue;
}
System.arraycopy(a, 0, c, fromIndex, a.length);
fromIndex += a.length;
}
return c;
}
/**
*
* @param
* @param a
* @param b
* @return
*/
@SuppressWarnings("unchecked")
public static T[] concat(final T[] a, final T[] b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? a : b.clone();
} else if (isNullOrEmpty(b)) {
return a.clone();
}
final T[] c = (T[]) newArray(a.getClass().getComponentType(), a.length + b.length);
copy(a, 0, c, 0, a.length);
copy(b, 0, c, a.length, b.length);
return c;
}
/**
*
* @param
* @param aa
* @return
* @throws IllegalArgumentException if the specified {@code Array} is null
.
*/
@SafeVarargs
public static T[] concat(final T[]... aa) throws IllegalArgumentException {
// checkArgNotNull(aa, "aa");
if (aa == null) {
return null;
} else if (aa.length == 0) {
return newArray(aa.getClass().getComponentType().getComponentType(), 0);
} else if (aa.length == 1) {
return aa[0] == null ? newArray(aa.getClass().getComponentType().getComponentType(), 0) : aa[0].clone();
}
int len = 0;
for (T[] a : aa) {
if (isNullOrEmpty(a)) {
continue;
}
len += a.length;
}
final T[] c = newArray(aa.getClass().getComponentType().getComponentType(), len);
int fromIndex = 0;
for (T[] a : aa) {
if (isNullOrEmpty(a)) {
continue;
}
System.arraycopy(a, 0, c, fromIndex, a.length);
fromIndex += a.length;
}
return c;
}
/**
*
* @param
* @param a
* @param b
* @return
*/
public static List concat(final Iterable extends T> a, final Iterable extends T> b) {
return concat(Arrays.asList(a, b));
}
/**
*
* @param
* @param a
* @return
*/
@SafeVarargs
public static List concat(final Iterable extends T>... a) {
if (isNullOrEmpty(a)) {
return new ArrayList<>();
}
return concat(Arrays.asList(a));
}
/**
*
* @param
* @param c
* @return
*/
public static List concat(final Collection extends Iterable extends T>> c) {
return concat(c, Factory. ofList());
}
/**
*
* @param
* @param
* @param c
* @param supplier
* @return
*/
public static > C concat(final Collection extends Iterable extends T>> c, final IntFunction extends C> supplier) {
if (isNullOrEmpty(c)) {
return supplier.apply(0);
}
long count = 0;
for (Iterable extends T> e : c) {
count += getSizeOrDefault(e, 0);
}
final C result = supplier.apply(N.toIntExact(count));
for (Iterable extends T> e : c) {
if (e != null) {
if (e instanceof Collection) {
result.addAll((Collection) e);
} else {
for (T t : e) {
result.add(t);
}
}
}
}
return result;
}
/**
*
* @param
* @param a
* @param b
* @return
* @see Iterators#concat(Iterator...)
*/
public static ObjIterator concat(final Iterator extends T> a, final Iterator extends T> b) {
return Iterators.concat(a, b);
}
/**
*
* @param
* @param a
* @return
* @see Iterators#concat(Iterator...)
*/
@SafeVarargs
public static ObjIterator concat(final Iterator extends T>... a) {
return Iterators.concat(a);
}
/**
*
* @param a
* @param b
* @return
* @see IntList#intersection(IntList)
*/
public static boolean[] intersection(final boolean[] a, final boolean[] b) {
if (isNullOrEmpty(a) || isNullOrEmpty(b)) {
return isNullOrEmpty(a) ? a : EMPTY_BOOLEAN_ARRAY;
}
return BooleanList.of(a).intersection(BooleanList.of(b)).trimToSize().array();
}
/**
*
* @param a
* @param b
* @return
* @see IntList#intersection(IntList)
*/
public static char[] intersection(final char[] a, final char[] b) {
if (isNullOrEmpty(a) || isNullOrEmpty(b)) {
return EMPTY_CHAR_ARRAY;
}
return CharList.of(a).intersection(CharList.of(b)).trimToSize().array();
}
/**
*
* @param a
* @param b
* @return
* @see IntList#intersection(IntList)
*/
public static byte[] intersection(final byte[] a, final byte[] b) {
if (isNullOrEmpty(a) || isNullOrEmpty(b)) {
return EMPTY_BYTE_ARRAY;
}
return ByteList.of(a).intersection(ByteList.of(b)).trimToSize().array();
}
/**
*
* @param a
* @param b
* @return
* @see IntList#intersection(IntList)
*/
public static short[] intersection(final short[] a, final short[] b) {
if (isNullOrEmpty(a) || isNullOrEmpty(b)) {
return EMPTY_SHORT_ARRAY;
}
return ShortList.of(a).intersection(ShortList.of(b)).trimToSize().array();
}
/**
* Returns a new array with all the elements in b
removed by occurrences.
*
*
* int[] a = {0, 1, 2, 2, 3};
* int[] b = {2, 5, 1};
* int[] c = retainAll(a, b); // The elements c in a will b: [1, 2, 2].
*
* int[] a = {0, 1, 2, 2, 3};
* int[] b = {2, 5, 1};
* int[] c = intersection(a, b); // The elements c in a will b: [1, 2].
*
*
* @param a
* @param b
* @return
* @see IntList#intersection(IntList)
*/
public static int[] intersection(final int[] a, final int[] b) {
if (isNullOrEmpty(a) || isNullOrEmpty(b)) {
return EMPTY_INT_ARRAY;
}
return IntList.of(a).intersection(IntList.of(b)).trimToSize().array();
}
/**
*
* @param a
* @param b
* @return
* @see IntList#intersection(IntList)
*/
public static long[] intersection(final long[] a, final long[] b) {
if (isNullOrEmpty(a) || isNullOrEmpty(b)) {
return EMPTY_LONG_ARRAY;
}
return LongList.of(a).intersection(LongList.of(b)).trimToSize().array();
}
/**
*
* @param a
* @param b
* @return
* @see IntList#intersection(IntList)
*/
public static float[] intersection(final float[] a, final float[] b) {
if (isNullOrEmpty(a) || isNullOrEmpty(b)) {
return EMPTY_FLOAT_ARRAY;
}
return FloatList.of(a).intersection(FloatList.of(b)).trimToSize().array();
}
/**
*
* @param a
* @param b
* @return
* @see IntList#intersection(IntList)
*/
public static double[] intersection(final double[] a, final double[] b) {
if (isNullOrEmpty(a) || isNullOrEmpty(b)) {
return EMPTY_DOUBLE_ARRAY;
}
return DoubleList.of(a).intersection(DoubleList.of(b)).trimToSize().array();
}
/**
*
* @param
* @param a
* @param b
* @return
* @see IntList#intersection(IntList)
*/
public static List intersection(final T[] a, final Object[] b) {
if (isNullOrEmpty(a) || isNullOrEmpty(b)) {
return new ArrayList<>();
}
final Multiset> bOccurrences = Multiset.of(b);
final List result = new ArrayList<>(min(9, a.length, b.length));
for (T e : a) {
if (bOccurrences.getAndRemove(e) > 0) {
result.add(e);
}
}
return result;
}
/**
*
* @param
* @param a
* @param b
* @return
* @see IntList#intersection(IntList)
* @see #commonSet(Collection, Collection)
* @see Collection#retainAll(Collection)
*/
public static List intersection(final Collection extends T> a, final Collection> b) {
if (isNullOrEmpty(a) || isNullOrEmpty(b)) {
return new ArrayList<>();
}
final Multiset bOccurrences = Multiset.from(b);
final List result = new ArrayList<>(min(9, a.size(), b.size()));
for (T e : a) {
if (bOccurrences.getAndRemove(e) > 0) {
result.add(e);
}
}
return result;
}
/**
*
* @param
* @param c
* @return
* @see #commonSet(Collection)
* @see Collection#retainAll(Collection)
*/
public static List intersection(final Collection extends Collection extends T>> c) {
if (isNullOrEmpty(c)) {
return new ArrayList<>();
} else if (c.size() == 1) {
return newArrayList(c.iterator().next());
}
for (Collection extends T> e : c) {
if (isNullOrEmpty(e)) {
return new ArrayList<>();
}
}
final Iterator extends Collection extends T>> iter = c.iterator();
List result = intersection(iter.next(), iter.next());
while (iter.hasNext()) {
result = intersection(result, iter.next());
if (result.size() == 0) {
break;
}
}
return result;
}
/**
* Returns the elements from {@code a}, but exclude the elements in {@code b} by occurrences.
*
* @param a
* @param b
* @return
* @see IntList#difference(IntList)
*/
public static boolean[] difference(final boolean[] a, final boolean[] b) {
if (isNullOrEmpty(a)) {
return EMPTY_BOOLEAN_ARRAY;
} else if (isNullOrEmpty(b)) {
return a.clone();
}
return BooleanList.of(a).difference(BooleanList.of(b)).trimToSize().array();
}
/**
* Returns the elements from {@code a}, but exclude the elements in {@code b} by occurrences.
*
* @param a
* @param b
* @return
* @see IntList#difference(IntList)
*/
public static char[] difference(final char[] a, final char[] b) {
if (isNullOrEmpty(a)) {
return EMPTY_CHAR_ARRAY;
} else if (isNullOrEmpty(b)) {
return a.clone();
}
return CharList.of(a).difference(CharList.of(b)).trimToSize().array();
}
/**
* Returns the elements from {@code a}, but exclude the elements in {@code b} by occurrences.
*
* @param a
* @param b
* @return
* @see IntList#difference(IntList)
*/
public static byte[] difference(final byte[] a, final byte[] b) {
if (isNullOrEmpty(a)) {
return EMPTY_BYTE_ARRAY;
} else if (isNullOrEmpty(b)) {
return a.clone();
}
return ByteList.of(a).difference(ByteList.of(b)).trimToSize().array();
}
/**
* Returns the elements from {@code a}, but exclude the elements in {@code b} by occurrences.
*
* @param a
* @param b
* @return
* @see IntList#difference(IntList)
*/
public static short[] difference(final short[] a, final short[] b) {
if (isNullOrEmpty(a)) {
return EMPTY_SHORT_ARRAY;
} else if (isNullOrEmpty(b)) {
return a.clone();
}
return ShortList.of(a).difference(ShortList.of(b)).trimToSize().array();
}
/**
* Returns the elements from {@code a}, but exclude the elements in {@code b} by occurrences.
*
*
* int[] a = {0, 1, 2, 2, 3};
* int[] b = {2, 5, 1};
* int[] c = removeAll(a, b); // The elements c in a will b: [0, 3].
*
* int[] a = {0, 1, 2, 2, 3};
* int[] b = {2, 5, 1};
* int[] c = difference(a, b); // The elements c in a will b: [0, 2, 3].
*
*
* @param a
* @param b
* @return
* @see IntList#difference(IntList)
*/
public static int[] difference(final int[] a, final int[] b) {
if (isNullOrEmpty(a)) {
return EMPTY_INT_ARRAY;
} else if (isNullOrEmpty(b)) {
return a.clone();
}
return IntList.of(a).difference(IntList.of(b)).trimToSize().array();
}
/**
* Returns the elements from {@code a}, but exclude the elements in {@code b} by occurrences.
*
* @param a
* @param b
* @return
* @see IntList#difference(IntList)
*/
public static long[] difference(final long[] a, final long[] b) {
if (isNullOrEmpty(a)) {
return EMPTY_LONG_ARRAY;
} else if (isNullOrEmpty(b)) {
return a.clone();
}
return LongList.of(a).difference(LongList.of(b)).trimToSize().array();
}
/**
* Returns the elements from {@code a}, but exclude the elements in {@code b} by occurrences.
*
* @param a
* @param b
* @return
* @see IntList#difference(IntList)
*/
public static float[] difference(final float[] a, final float[] b) {
if (isNullOrEmpty(a)) {
return EMPTY_FLOAT_ARRAY;
} else if (isNullOrEmpty(b)) {
return a.clone();
}
return FloatList.of(a).difference(FloatList.of(b)).trimToSize().array();
}
/**
* Returns the elements from {@code a}, but exclude the elements in {@code b} by occurrences.
*
* @param a
* @param b
* @return
* @see IntList#difference(IntList)
*/
public static double[] difference(final double[] a, final double[] b) {
if (isNullOrEmpty(a)) {
return EMPTY_DOUBLE_ARRAY;
} else if (isNullOrEmpty(b)) {
return a.clone();
}
return DoubleList.of(a).difference(DoubleList.of(b)).trimToSize().array();
}
/**
* Returns the elements from {@code a}, but exclude the elements in {@code b} by occurrences.
*
* @param
* @param a
* @param b
* @return
* @see IntList#difference(IntList)
* @see #differentSet(Collection, Collection)
* @see #excludeAll(Collection, Collection)
* @see #excludeAllToSet(Collection, Collection)
* @see #removeAll(Collection, Collection)
* @see Difference#of(Collection, Collection)
*/
public static List difference(final T[] a, final Object[] b) {
if (isNullOrEmpty(a)) {
return new ArrayList<>();
} else if (isNullOrEmpty(b)) {
return asList(a);
}
final Multiset> bOccurrences = Multiset.of(b);
final List result = new ArrayList<>(min(a.length, max(9, a.length - b.length)));
for (T e : a) {
if (bOccurrences.getAndRemove(e) < 1) {
result.add(e);
}
}
return result;
}
/**
* Returns the elements from {@code a}, but exclude the elements in {@code b} by occurrences.
*
* @param
* @param a
* @param b
* @return
* @see IntList#difference(IntList)
* @see #differentSet(Collection, Collection)
* @see #excludeAll(Collection, Collection)
* @see #excludeAllToSet(Collection, Collection)
* @see #removeAll(Collection, Collection)
* @see Difference#of(Collection, Collection)
*/
public static List difference(final Collection extends T> a, final Collection> b) {
if (isNullOrEmpty(a)) {
return new ArrayList<>();
} else if (isNullOrEmpty(b)) {
return new ArrayList<>(a);
}
final Multiset bOccurrences = Multiset.from(b);
final List result = new ArrayList<>(min(a.size(), max(9, a.size() - b.size())));
for (T e : a) {
if (bOccurrences.getAndRemove(e) < 1) {
result.add(e);
}
}
return result;
}
/**
*
* @param a
* @param b
* @return
* @see IntList#symmetricDifference(IntList)
*/
public static boolean[] symmetricDifference(final boolean[] a, final boolean[] b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? EMPTY_BOOLEAN_ARRAY : b.clone();
} else if (isNullOrEmpty(b)) {
return a.clone();
}
return BooleanList.of(a).symmetricDifference(BooleanList.of(b)).trimToSize().array();
}
/**
*
* @param a
* @param b
* @return
* @see IntList#symmetricDifference(IntList)
*/
public static char[] symmetricDifference(final char[] a, final char[] b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? EMPTY_CHAR_ARRAY : b.clone();
} else if (isNullOrEmpty(b)) {
return a.clone();
}
return CharList.of(a).symmetricDifference(CharList.of(b)).trimToSize().array();
}
/**
*
* @param a
* @param b
* @return
* @see IntList#symmetricDifference(IntList)
*/
public static byte[] symmetricDifference(final byte[] a, final byte[] b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? EMPTY_BYTE_ARRAY : b.clone();
} else if (isNullOrEmpty(b)) {
return a.clone();
}
return ByteList.of(a).symmetricDifference(ByteList.of(b)).trimToSize().array();
}
/**
*
* @param a
* @param b
* @return
* @see IntList#symmetricDifference(IntList)
*/
public static short[] symmetricDifference(final short[] a, final short[] b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? EMPTY_SHORT_ARRAY : b.clone();
} else if (isNullOrEmpty(b)) {
return a.clone();
}
return ShortList.of(a).symmetricDifference(ShortList.of(b)).trimToSize().array();
}
/**
*
* int[] a = {0, 1, 2, 2, 3};
* int[] b = {2, 5, 1};
* int[] c = symmetricDifference(a, b); // The elements c in a will b: [0, 2, 3, 5].
*
*
* @param a
* @param b
* @return
* @see IntList#symmetricDifference(IntList)
* @see CommonUtil#difference(int[], int[])
*/
public static int[] symmetricDifference(final int[] a, final int[] b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? EMPTY_INT_ARRAY : b.clone();
} else if (isNullOrEmpty(b)) {
return a.clone();
}
return IntList.of(a).symmetricDifference(IntList.of(b)).trimToSize().array();
}
/**
*
* @param a
* @param b
* @return
* @see IntList#symmetricDifference(IntList)
*/
public static long[] symmetricDifference(final long[] a, final long[] b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? EMPTY_LONG_ARRAY : b.clone();
} else if (isNullOrEmpty(b)) {
return a.clone();
}
return LongList.of(a).symmetricDifference(LongList.of(b)).trimToSize().array();
}
/**
*
* @param a
* @param b
* @return
* @see IntList#symmetricDifference(IntList)
*/
public static float[] symmetricDifference(final float[] a, final float[] b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? EMPTY_FLOAT_ARRAY : b.clone();
} else if (isNullOrEmpty(b)) {
return a.clone();
}
return FloatList.of(a).symmetricDifference(FloatList.of(b)).trimToSize().array();
}
/**
*
* @param a
* @param b
* @return
* @see IntList#symmetricDifference(IntList)
*/
public static double[] symmetricDifference(final double[] a, final double[] b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? EMPTY_DOUBLE_ARRAY : b.clone();
} else if (isNullOrEmpty(b)) {
return a.clone();
}
return DoubleList.of(a).symmetricDifference(DoubleList.of(b)).trimToSize().array();
}
/**
*
* @param
* @param a
* @param b
* @return
* @see IntList#symmetricDifference(IntList)
* @see #symmetricDifferentSet(Collection, Collection)
* @see #excludeAll(Collection, Collection)
* @see #excludeAllToSet(Collection, Collection)
* @see Difference#of(Collection, Collection)
*/
public static List symmetricDifference(final T[] a, final T[] b) {
if (isNullOrEmpty(a)) {
return asList(b);
} else if (isNullOrEmpty(b)) {
return asList(a);
}
final Multiset bOccurrences = Multiset.of(b);
final List result = new ArrayList<>(max(9, Math.abs(a.length - b.length)));
for (T e : a) {
if (bOccurrences.getAndRemove(e) < 1) {
result.add(e);
}
}
for (T e : b) {
if (bOccurrences.getAndRemove(e) > 0) {
result.add(e);
}
if (bOccurrences.isEmpty()) {
break;
}
}
return result;
}
/**
*
* @param
* @param a
* @param b
* @return
* @see IntList#symmetricDifference(IntList)
* @see #symmetricDifferentSet(Collection, Collection)
* @see #excludeAll(Collection, Collection)
* @see #excludeAllToSet(Collection, Collection)
* @see Difference#of(Collection, Collection)
*/
public static List symmetricDifference(final Collection extends T> a, final Collection extends T> b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? new ArrayList<>() : new ArrayList<>(b);
} else if (isNullOrEmpty(b)) {
return isNullOrEmpty(a) ? new ArrayList<>() : new ArrayList<>(a);
}
final Multiset bOccurrences = Multiset.from(b);
final List result = new ArrayList<>(max(9, Math.abs(a.size() - b.size())));
for (T e : a) {
if (bOccurrences.getAndRemove(e) < 1) {
result.add(e);
}
}
for (T e : b) {
if (bOccurrences.getAndRemove(e) > 0) {
result.add(e);
}
if (bOccurrences.isEmpty()) {
break;
}
}
return result;
}
/**
*
* @param
* @param a
* @param b
* @return the sets the
* @see #difference(Collection, Collection)
* @see #excludeAll(Collection, Collection)
* @see #excludeAllToSet(Collection, Collection)
* @see #removeAll(Collection, Collection)
* @see Difference#of(Collection, Collection)
*/
@SuppressWarnings("rawtypes")
public static Set differentSet(final Collection extends T> a, final Collection> b) {
if (N.isNullOrEmpty(a)) {
return N.newHashSet();
} else if (N.isNullOrEmpty(b)) {
return N.newHashSet(a);
}
final Set result = N.newHashSet(a);
N.removeAll(result, (Collection) b);
return result;
}
/**
* Symmetric different set.
*
* @param
* @param a
* @param b
* @return the sets the
* @see #symmetricDifference(Collection, Collection)
* @see #excludeAll(Collection, Collection)
* @see #excludeAllToSet(Collection, Collection)
* @see Difference#of(Collection, Collection)
*/
public static Set symmetricDifferentSet(final Collection extends T> a, final Collection extends T> b) {
if (N.isNullOrEmpty(a)) {
return N.isNullOrEmpty(b) ? N. newHashSet() : N. newHashSet(b);
} else if (N.isNullOrEmpty(b)) {
return N.isNullOrEmpty(a) ? N. newHashSet() : N. newHashSet(a);
}
final Set commonSet = commonSet(a, b);
final Set result = N.newHashSet();
for (T e : a) {
if (!commonSet.contains(e)) {
result.add(e);
}
}
for (T e : b) {
if (!commonSet.contains(e)) {
result.add(e);
}
}
return result;
}
/**
*
* @param
* @param a
* @param b
* @return the sets the
* @see #intersection(Collection, Collection)
* @see Collection#retainAll(Collection)
*/
public static Set commonSet(final Collection extends T> a, final Collection> b) {
if (N.isNullOrEmpty(a) || N.isNullOrEmpty(b)) {
return N.newHashSet();
}
return commonSet(Array.asList(a, (Collection extends T>) b));
}
/**
*
* @param
* @param c
* @return the sets the
* @see #intersection(Collection)
* @see Collection#retainAll(Collection)
*/
public static Set commonSet(final Collection extends Collection extends T>> c) {
if (N.isNullOrEmpty(c)) {
return N.newHashSet();
} else if (c.size() == 1) {
return N.newHashSet(c.iterator().next());
}
Collection extends T> smallest = null;
for (final Collection extends T> e : c) {
if (N.isNullOrEmpty(e)) {
return N.newHashSet();
}
if (smallest == null || e.size() < smallest.size()) {
smallest = e;
}
}
final Map map = new HashMap<>();
for (T e : smallest) {
map.put(e, new MutableInt(1));
}
int cnt = 1;
MutableInt val = null;
for (final Collection extends T> ec : c) {
if (ec == smallest) {
continue;
}
for (T e : ec) {
val = map.get(e);
if (val == null) {
// do nothing.
} else if (val.intValue() < cnt) {
// map.remove(e);
} else if (val.intValue() == cnt) {
val.increment();
}
}
cnt++;
}
final Set result = N.newHashSet(map.size());
for (Map.Entry entry : map.entrySet()) {
if (entry.getValue().intValue() == cnt) {
result.add(entry.getKey());
}
}
return result;
}
/**
* Returns a new {@code List} with specified {@code objToExclude} excluded.
* That's to say no more {@code objToExclude} will present in the returned {@code List}.
*
* @param
* @param c
* @param objToExclude
* @return a new {@code List}
* @see #difference(Collection, Collection)
* @see #removeAll(Collection, Collection)
* @see Difference#of(Collection, Collection)
*/
public static List exclude(final Collection c, final Object objToExclude) {
if (N.isNullOrEmpty(c)) {
return new ArrayList<>();
}
final List result = new ArrayList<>(c.size() - 1);
for (T e : c) {
if (!N.equals(e, objToExclude)) {
result.add(e);
}
}
return result;
}
/**
* Returns a new {@code Set} with specified {@code objToExclude} excluded.
* That's to say no more {@code objToExclude} will present in the returned {@code Set}.
*
* @param
* @param c
* @param objToExclude
* @return a new {@code Set}
* @see #difference(Collection, Collection)
* @see #removeAll(Collection, Collection)
* @see Difference#of(Collection, Collection)
*/
public static Set excludeToSet(final Collection c, final Object objToExclude) {
if (N.isNullOrEmpty(c)) {
return new HashSet<>();
}
final Set result = N.newHashSet(c);
result.remove(objToExclude);
return result;
}
/**
* Returns a new {@code List} with specified {@code objsToExclude} excluded.
* That's to say no more {@code objsToExclude} will present in the returned {@code List}.
*
* @param c
* @param objsToExclude
* @return a new {@code List}
* @see #difference(Collection, Collection)
* @see #removeAll(Collection, Collection)
* @see Difference#of(Collection, Collection)
*/
public static List excludeAll(final Collection c, final Collection> objsToExclude) {
if (N.isNullOrEmpty(c)) {
return new ArrayList<>();
} else if (N.isNullOrEmpty(objsToExclude)) {
return new ArrayList<>(c);
} else if (objsToExclude.size() == 1) {
return exclude(c, N.firstOrNullIfEmpty(objsToExclude));
}
final Set set = objsToExclude instanceof Set ? ((Set) objsToExclude) : new HashSet<>(objsToExclude);
final List result = new ArrayList<>(N.max(0, c.size() - set.size()));
for (T e : c) {
if (!set.contains(e)) {
result.add(e);
}
}
return result;
}
/**
* Returns a new {@code Set} with specified {@code objsToExclude} excluded.
* That's to say no more {@code objsToExclude} will present in the returned {@code Set}.
*
* @param c
* @param objsToExclude
* @return a new {@code Set}
* @see #difference(Collection, Collection)
* @see #removeAll(Collection, Collection)
* @see Difference#of(Collection, Collection)
*/
public static Set excludeAllToSet(final Collection c, final Collection> objsToExclude) {
if (N.isNullOrEmpty(c)) {
return new HashSet<>();
} else if (N.isNullOrEmpty(objsToExclude)) {
return new HashSet<>(c);
} else if (objsToExclude.size() == 1) {
return excludeToSet(c, N.firstOrNullIfEmpty(objsToExclude));
}
final Set set = objsToExclude instanceof Set ? ((Set) objsToExclude) : new HashSet<>(objsToExclude);
final Set result = N.newHashSet(N.max(0, c.size() - set.size()));
for (T e : c) {
if (!set.contains(e)) {
result.add(e);
}
}
return result;
}
/**
* Returns {@code true} if subColl is a sub-collection of coll ,
* that is, if the cardinality of e in subColl is less than or
* equal to the cardinality of e in coll , for each element e
* in subColl .
*
* @param subColl the first (sub?) collection, must not be null
* @param coll the second (super?) collection, must not be null
* @return true
if subColl is a sub-collection of coll
* @see #isProperSubCollection
* @see Collection#containsAll
*/
public static boolean isSubCollection(final Collection> subColl, final Collection> coll) {
N.checkArgNotNull(subColl, "a");
N.checkArgNotNull(coll, "b");
if (N.isNullOrEmpty(coll)) {
return true;
} else if (N.isNullOrEmpty(subColl)) {
return false;
}
final Multiset> multisetA = Multiset.from(subColl);
final Multiset> multisetB = Multiset.from(coll);
for (final Object e : subColl) {
if (multisetA.occurrencesOf(e) > multisetB.occurrencesOf(e)) {
return false;
}
}
return true;
}
/**
* Returns {@code true} if subColl is a proper sub-collection of coll ,
* that is, if the cardinality of e in subColl is less
* than or equal to the cardinality of e in coll ,
* for each element e in subColl , and there is at least one
* element f such that the cardinality of f in coll
* is strictly greater than the cardinality of f in subColl .
*
* The implementation assumes
*
*
* subColl.size()
and coll.size()
represent the
* total cardinality of a and b , resp.
* subColl.size() < Integer.MAXVALUE
*
*
* @param subColl the first (sub?) collection, must not be null
* @param coll the second (super?) collection, must not be null
* @return true
if subColl is a proper sub-collection of coll
* @see #isSubCollection
* @see Collection#containsAll
*/
public static boolean isProperSubCollection(final Collection> subColl, final Collection> coll) {
N.checkArgNotNull(subColl, "a");
N.checkArgNotNull(coll, "b");
return subColl.size() < coll.size() && isSubCollection(subColl, coll);
}
/**
* Returns {@code true} if the given {@link Collection}s contain
* exactly the same elements with exactly the same cardinalities.
*
* That is, if the cardinality of e in a is
* equal to the cardinality of e in b ,
* for each element e in a or b .
*
*
* @param a the first collection
* @param b the second collection
* @return true
if the collections contain the same elements with the same cardinalities.
*/
public static boolean isEqualCollection(final Collection> a, final Collection> b) {
if (a == null && b == null) {
return true;
} else if ((a == null && b != null) || (a != null && b == null)) {
return false;
}
final int sizeA = N.size(a);
final int sizeB = N.size(b);
if (sizeA != sizeB) {
return false;
}
final Multiset> multisetA = Multiset.from(a);
final Multiset> multisetB = Multiset.from(b);
if (multisetA.size() != multisetB.size()) {
return false;
}
for (final Object e : b) {
if (multisetA.occurrencesOf(e) != multisetB.occurrencesOf(e)) {
return false;
}
}
return true;
}
/**
*
* @param a
* @param oldVal
* @param newVal
* @return
*/
public static int replaceAll(final boolean[] a, final boolean oldVal, final boolean newVal) {
if (isNullOrEmpty(a)) {
return 0;
}
int result = 0;
for (int i = 0, len = a.length; i < len; i++) {
if (a[i] == oldVal) {
a[i] = newVal;
result++;
}
}
return result;
}
/**
*
* @param a
* @param oldVal
* @param newVal
* @return
*/
public static int replaceAll(final char[] a, final char oldVal, final char newVal) {
if (isNullOrEmpty(a)) {
return 0;
}
int result = 0;
for (int i = 0, len = a.length; i < len; i++) {
if (a[i] == oldVal) {
a[i] = newVal;
result++;
}
}
return result;
}
/**
*
* @param a
* @param oldVal
* @param newVal
* @return
*/
public static int replaceAll(final byte[] a, final byte oldVal, final byte newVal) {
if (isNullOrEmpty(a)) {
return 0;
}
int result = 0;
for (int i = 0, len = a.length; i < len; i++) {
if (a[i] == oldVal) {
a[i] = newVal;
result++;
}
}
return result;
}
/**
*
* @param a
* @param oldVal
* @param newVal
* @return
*/
public static int replaceAll(final short[] a, final short oldVal, final short newVal) {
if (isNullOrEmpty(a)) {
return 0;
}
int result = 0;
for (int i = 0, len = a.length; i < len; i++) {
if (a[i] == oldVal) {
a[i] = newVal;
result++;
}
}
return result;
}
/**
*
* @param a
* @param oldVal
* @param newVal
* @return
*/
public static int replaceAll(final int[] a, final int oldVal, final int newVal) {
if (isNullOrEmpty(a)) {
return 0;
}
int result = 0;
for (int i = 0, len = a.length; i < len; i++) {
if (a[i] == oldVal) {
a[i] = newVal;
result++;
}
}
return result;
}
/**
*
* @param a
* @param oldVal
* @param newVal
* @return
*/
public static int replaceAll(final long[] a, final long oldVal, final long newVal) {
if (isNullOrEmpty(a)) {
return 0;
}
int result = 0;
for (int i = 0, len = a.length; i < len; i++) {
if (a[i] == oldVal) {
a[i] = newVal;
result++;
}
}
return result;
}
/**
*
* @param a
* @param oldVal
* @param newVal
* @return
*/
public static int replaceAll(final float[] a, final float oldVal, final float newVal) {
if (isNullOrEmpty(a)) {
return 0;
}
int result = 0;
for (int i = 0, len = a.length; i < len; i++) {
if (Float.compare(a[i], oldVal) == 0) {
a[i] = newVal;
result++;
}
}
return result;
}
/**
*
* @param a
* @param oldVal
* @param newVal
* @return
*/
public static int replaceAll(final double[] a, final double oldVal, final double newVal) {
if (isNullOrEmpty(a)) {
return 0;
}
int result = 0;
for (int i = 0, len = a.length; i < len; i++) {
if (Double.compare(a[i], oldVal) == 0) {
a[i] = newVal;
result++;
}
}
return result;
}
/**
*
* @param
* @param a
* @param oldVal
* @param newVal
* @return
*/
public static int replaceAll(final T[] a, final Object oldVal, final T newVal) {
if (isNullOrEmpty(a)) {
return 0;
}
int result = 0;
if (oldVal == null) {
for (int i = 0, len = a.length; i < len; i++) {
if (a[i] == null) {
a[i] = newVal;
result++;
}
}
} else {
for (int i = 0, len = a.length; i < len; i++) {
if (equals(a[i], oldVal)) {
a[i] = newVal;
result++;
}
}
}
return result;
}
/**
*
* @param
* @param list
* @param oldVal
* @param newVal
* @return
*/
public static int replaceAll(final List list, final Object oldVal, final T newVal) {
if (isNullOrEmpty(list)) {
return 0;
}
int result = 0;
final int size = list.size();
if (size < REPLACEALL_THRESHOLD || list instanceof RandomAccess) {
if (oldVal == null) {
for (int i = 0; i < size; i++) {
if (list.get(i) == null) {
list.set(i, newVal);
result++;
}
}
} else {
for (int i = 0; i < size; i++) {
if (oldVal.equals(list.get(i))) {
list.set(i, newVal);
result++;
}
}
}
} else {
final ListIterator itr = list.listIterator();
if (oldVal == null) {
for (int i = 0; i < size; i++) {
if (itr.next() == null) {
itr.set(newVal);
result++;
}
}
} else {
for (int i = 0; i < size; i++) {
if (oldVal.equals(itr.next())) {
itr.set(newVal);
result++;
}
}
}
}
return result;
}
public static void replaceAll(final boolean[] a, final Throwables.BooleanUnaryOperator operator) throws E {
if (N.isNullOrEmpty(a)) {
return;
}
for (int i = 0, n = a.length; i < n; i++) {
a[i] = operator.applyAsBoolean(a[i]);
}
}
public static void replaceAll(final char[] a, final Throwables.CharUnaryOperator operator) throws E {
if (N.isNullOrEmpty(a)) {
return;
}
for (int i = 0, n = a.length; i < n; i++) {
a[i] = operator.applyAsChar(a[i]);
}
}
public static void replaceAll(final byte[] a, final Throwables.ByteUnaryOperator operator) throws E {
if (N.isNullOrEmpty(a)) {
return;
}
for (int i = 0, n = a.length; i < n; i++) {
a[i] = operator.applyAsByte(a[i]);
}
}
public static void replaceAll(final short[] a, final Throwables.ShortUnaryOperator operator) throws E {
if (N.isNullOrEmpty(a)) {
return;
}
for (int i = 0, n = a.length; i < n; i++) {
a[i] = operator.applyAsShort(a[i]);
}
}
public static void replaceAll(final int[] a, final Throwables.IntUnaryOperator operator) throws E {
if (N.isNullOrEmpty(a)) {
return;
}
for (int i = 0, n = a.length; i < n; i++) {
a[i] = operator.applyAsInt(a[i]);
}
}
public static void replaceAll(final long[] a, final Throwables.LongUnaryOperator operator) throws E {
if (N.isNullOrEmpty(a)) {
return;
}
for (int i = 0, n = a.length; i < n; i++) {
a[i] = operator.applyAsLong(a[i]);
}
}
public static void replaceAll(final float[] a, final Throwables.FloatUnaryOperator operator) throws E {
if (N.isNullOrEmpty(a)) {
return;
}
for (int i = 0, n = a.length; i < n; i++) {
a[i] = operator.applyAsFloat(a[i]);
}
}
public static void replaceAll(final double[] a, final Throwables.DoubleUnaryOperator operator) throws E {
if (N.isNullOrEmpty(a)) {
return;
}
for (int i = 0, n = a.length; i < n; i++) {
a[i] = operator.applyAsDouble(a[i]);
}
}
public static void replaceAll(final T[] a, final Throwables.UnaryOperator operator) throws E {
if (N.isNullOrEmpty(a)) {
return;
}
for (int i = 0, n = a.length; i < n; i++) {
a[i] = operator.apply(a[i]);
}
}
public static int replaceAll(final List list, final Throwables.UnaryOperator operator) throws E {
if (isNullOrEmpty(list)) {
return 0;
}
int result = 0;
final int size = list.size();
if (size < REPLACEALL_THRESHOLD || list instanceof RandomAccess) {
for (int i = 0; i < size; i++) {
list.set(i, operator.apply(list.get(i)));
}
} else {
final ListIterator itr = list.listIterator();
for (int i = 0; i < size; i++) {
itr.set(operator.apply(itr.next()));
}
}
return result;
}
public static int replaceIf(final boolean[] a, final Throwables.BooleanPredicate predicate, final boolean newValue) throws E {
if (N.isNullOrEmpty(a)) {
return 0;
}
int result = 0;
for (int i = 0, n = a.length; i < n; i++) {
if (predicate.test(a[i])) {
a[i] = newValue;
result++;
}
}
return result;
}
public static int replaceIf(final char[] a, final Throwables.CharPredicate predicate, final char newValue) throws E {
if (N.isNullOrEmpty(a)) {
return 0;
}
int result = 0;
for (int i = 0, n = a.length; i < n; i++) {
if (predicate.test(a[i])) {
a[i] = newValue;
result++;
}
}
return result;
}
public static int replaceIf(final byte[] a, final Throwables.BytePredicate predicate, final byte newValue) throws E {
if (N.isNullOrEmpty(a)) {
return 0;
}
int result = 0;
for (int i = 0, n = a.length; i < n; i++) {
if (predicate.test(a[i])) {
a[i] = newValue;
result++;
}
}
return result;
}
public static int replaceIf(final short[] a, final Throwables.ShortPredicate predicate, final short newValue) throws E {
if (N.isNullOrEmpty(a)) {
return 0;
}
int result = 0;
for (int i = 0, n = a.length; i < n; i++) {
if (predicate.test(a[i])) {
a[i] = newValue;
result++;
}
}
return result;
}
public static int replaceIf(final int[] a, final Throwables.IntPredicate predicate, final int newValue) throws E {
if (N.isNullOrEmpty(a)) {
return 0;
}
int result = 0;
for (int i = 0, n = a.length; i < n; i++) {
if (predicate.test(a[i])) {
a[i] = newValue;
result++;
}
}
return result;
}
public static int replaceIf(final long[] a, final Throwables.LongPredicate predicate, final long newValue) throws E {
if (N.isNullOrEmpty(a)) {
return 0;
}
int result = 0;
for (int i = 0, n = a.length; i < n; i++) {
if (predicate.test(a[i])) {
a[i] = newValue;
result++;
}
}
return result;
}
public static int replaceIf(final float[] a, final Throwables.FloatPredicate