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.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.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
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.TimeUnit;
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.Tuple.Tuple2;
import com.landawn.abacus.util.u.Nullable;
import com.landawn.abacus.util.u.OptionalDouble;
import com.landawn.abacus.util.function.IntFunction;
// TODO: Auto-generated Javadoc
/**
*
* 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.IOUtil
* @see com.landawn.abacus.util.StringUtil
* @see com.landawn.abacus.util.Iterables
* @see com.landawn.abacus.util.Iterators
* @see com.landawn.abacus.util.Maps
* @see com.landawn.abacus.util.Primitives
* @see com.landawn.abacus.util.Array
* @see com.landawn.abacus.util.Seq
*/
public final class N extends CommonUtil {
private static final float LOAD_FACTOR_FOR_FLAT_MAP = 1.75f;
private static final int LOAD_FACTOR_FOR_TWO_FLAT_MAP = 2;
private N() {
// Utility class.
}
/**
*
* @param a
* @param objectToFind
* @return
*/
public static int occurrencesOf(final boolean[] a, final boolean objectToFind) {
if (isNullOrEmpty(a)) {
return 0;
}
int occurrences = 0;
for (int i = 0, len = a.length; i < len; i++) {
if (a[i] == objectToFind) {
occurrences++;
}
}
return occurrences;
}
/**
*
* @param a
* @param objectToFind
* @return
*/
public static int occurrencesOf(final char[] a, final char objectToFind) {
if (isNullOrEmpty(a)) {
return 0;
}
int occurrences = 0;
for (int i = 0, len = a.length; i < len; i++) {
if (a[i] == objectToFind) {
occurrences++;
}
}
return occurrences;
}
/**
*
* @param a
* @param objectToFind
* @return
*/
public static int occurrencesOf(final byte[] a, final byte objectToFind) {
if (isNullOrEmpty(a)) {
return 0;
}
int occurrences = 0;
for (int i = 0, len = a.length; i < len; i++) {
if (a[i] == objectToFind) {
occurrences++;
}
}
return occurrences;
}
/**
*
* @param a
* @param objectToFind
* @return
*/
public static int occurrencesOf(final short[] a, final short objectToFind) {
if (isNullOrEmpty(a)) {
return 0;
}
int occurrences = 0;
for (int i = 0, len = a.length; i < len; i++) {
if (a[i] == objectToFind) {
occurrences++;
}
}
return occurrences;
}
/**
*
* @param a
* @param objectToFind
* @return
*/
public static int occurrencesOf(final int[] a, final int objectToFind) {
if (isNullOrEmpty(a)) {
return 0;
}
int occurrences = 0;
for (int i = 0, len = a.length; i < len; i++) {
if (a[i] == objectToFind) {
occurrences++;
}
}
return occurrences;
}
/**
*
* @param a
* @param objectToFind
* @return
*/
public static int occurrencesOf(final long[] a, final long objectToFind) {
if (isNullOrEmpty(a)) {
return 0;
}
int occurrences = 0;
for (int i = 0, len = a.length; i < len; i++) {
if (a[i] == objectToFind) {
occurrences++;
}
}
return occurrences;
}
/**
*
* @param a
* @param objectToFind
* @return
*/
public static int occurrencesOf(final float[] a, final float objectToFind) {
if (isNullOrEmpty(a)) {
return 0;
}
int occurrences = 0;
for (int i = 0, len = a.length; i < len; i++) {
if (Float.compare(a[i], objectToFind) == 0) {
occurrences++;
}
}
return occurrences;
}
/**
*
* @param a
* @param objectToFind
* @return
*/
public static int occurrencesOf(final double[] a, final double objectToFind) {
if (isNullOrEmpty(a)) {
return 0;
}
int occurrences = 0;
for (int i = 0, len = a.length; i < len; i++) {
if (Double.compare(a[i], objectToFind) == 0) {
occurrences++;
}
}
return occurrences;
}
/**
*
* @param a
* @param objectToFind
* @return
*/
public static int occurrencesOf(final Object[] a, final Object objectToFind) {
if (isNullOrEmpty(a)) {
return 0;
}
int occurrences = 0;
if (objectToFind == null) {
for (int i = 0, len = a.length; i < len; i++) {
if (a[i] == null) {
occurrences++;
}
}
} else {
for (int i = 0, len = a.length; i < len; i++) {
if (objectToFind.equals(a[i])) {
occurrences++;
}
}
}
return occurrences;
}
/**
*
* @param c
* @param objectToFind
* @return
* @see java.util.Collections#frequency(Collection, Object)
*/
public static int occurrencesOf(final Collection> c, final Object objectToFind) {
if (isNullOrEmpty(c)) {
return 0;
}
return Collections.frequency(c, objectToFind);
}
/**
*
* @param a
* @param objectToFind
* @return true, if successful
*/
public static boolean contains(final boolean[] a, final boolean objectToFind) {
return indexOf(a, objectToFind) != INDEX_NOT_FOUND;
}
/**
*
* @param a
* @param objectToFind
* @return true, if successful
*/
public static boolean contains(final char[] a, final char objectToFind) {
return indexOf(a, objectToFind) != INDEX_NOT_FOUND;
}
/**
*
* @param a
* @param objectToFind
* @return true, if successful
*/
public static boolean contains(final byte[] a, final byte objectToFind) {
return indexOf(a, objectToFind) != INDEX_NOT_FOUND;
}
/**
*
* @param a
* @param objectToFind
* @return true, if successful
*/
public static boolean contains(final short[] a, final short objectToFind) {
return indexOf(a, objectToFind) != INDEX_NOT_FOUND;
}
/**
*
* @param a
* @param objectToFind
* @return true, if successful
*/
public static boolean contains(final int[] a, final int objectToFind) {
return indexOf(a, objectToFind) != INDEX_NOT_FOUND;
}
/**
*
* @param a
* @param objectToFind
* @return true, if successful
*/
public static boolean contains(final long[] a, final long objectToFind) {
return indexOf(a, objectToFind) != INDEX_NOT_FOUND;
}
/**
*
* @param a
* @param objectToFind
* @return true, if successful
*/
public static boolean contains(final float[] a, final float objectToFind) {
return indexOf(a, objectToFind) != INDEX_NOT_FOUND;
}
/**
*
* @param a
* @param objectToFind
* @return true, if successful
*/
public static boolean contains(final double[] a, final double objectToFind) {
return indexOf(a, objectToFind) != INDEX_NOT_FOUND;
}
/**
*
* @param a
* @param objectToFind
* @return true, if successful
*/
public static boolean contains(final Object[] a, final Object objectToFind) {
return indexOf(a, objectToFind) != INDEX_NOT_FOUND;
}
/**
*
* @param c
* @param e
* @return true, if successful
*/
public static boolean contains(final Collection> c, final Object e) {
if (isNullOrEmpty(c)) {
return false;
}
return c.contains(e);
}
/**
* Contains all.
*
* @param c the c
* @param objsToFind the objs to find
* @return true, if successful
*/
public static boolean containsAll(final Collection> c, final Collection> objsToFind) {
if (N.isNullOrEmpty(objsToFind)) {
return true;
} else if (N.isNullOrEmpty(c)) {
return false;
}
return c.containsAll(objsToFind);
}
/**
* Contains all.
*
* @param c the c
* @param objsToFind the objs to find
* @return true, if successful
*/
public static boolean containsAll(final Collection> c, final Object[] objsToFind) {
if (N.isNullOrEmpty(objsToFind)) {
return true;
} else if (N.isNullOrEmpty(c)) {
return false;
}
return c.containsAll(Array.asList(objsToFind));
}
/**
* Contains any.
*
* @param c the c
* @param objsToFind the objs to find
* @return true, if successful
*/
public static boolean containsAny(final Collection> c, final Collection> objsToFind) {
if (N.isNullOrEmpty(c) || N.isNullOrEmpty(objsToFind)) {
return false;
}
return !N.disjoint(c, objsToFind);
}
/**
* Contains any.
*
* @param c the c
* @param objsToFind the objs to find
* @return true, if successful
*/
public static boolean containsAny(final Collection> c, final Object[] objsToFind) {
if (N.isNullOrEmpty(c) || N.isNullOrEmpty(objsToFind)) {
return false;
}
return !N.disjoint(c, Array.asList(objsToFind));
}
/**
* Gets the only element.
*
* @param the generic type
* @param iterable the 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 Iterators.getOnlyElement(iterable.iterator());
}
/**
* 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 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 from = 0, toIndex = a.length; from < toIndex; from += chunkSize) {
res.add(copyOfRange(a, from, from <= toIndex - chunkSize ? from + 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 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)) {
return new ArrayList<>();
}
final int len = toIndex - fromIndex;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int from = fromIndex; from < toIndex; from += chunkSize) {
res.add(copyOfRange(a, from, from <= toIndex - chunkSize ? from + 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 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 from = 0, toIndex = a.length; from < toIndex; from += chunkSize) {
res.add(copyOfRange(a, from, from <= toIndex - chunkSize ? from + 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 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)) {
return new ArrayList<>();
}
final int len = toIndex - fromIndex;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int from = fromIndex; from < toIndex; from += chunkSize) {
res.add(copyOfRange(a, from, from <= toIndex - chunkSize ? from + 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 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 from = 0, toIndex = a.length; from < toIndex; from += chunkSize) {
res.add(copyOfRange(a, from, from <= toIndex - chunkSize ? from + 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 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)) {
return new ArrayList<>();
}
final int len = toIndex - fromIndex;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int from = fromIndex; from < toIndex; from += chunkSize) {
res.add(copyOfRange(a, from, from <= toIndex - chunkSize ? from + 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 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 from = 0, toIndex = a.length; from < toIndex; from += chunkSize) {
res.add(copyOfRange(a, from, from <= toIndex - chunkSize ? from + 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 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)) {
return new ArrayList<>();
}
final int len = toIndex - fromIndex;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int from = fromIndex; from < toIndex; from += chunkSize) {
res.add(copyOfRange(a, from, from <= toIndex - chunkSize ? from + 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 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 from = 0, toIndex = a.length; from < toIndex; from += chunkSize) {
res.add(copyOfRange(a, from, from <= toIndex - chunkSize ? from + 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 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)) {
return new ArrayList<>();
}
final int len = toIndex - fromIndex;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int from = fromIndex; from < toIndex; from += chunkSize) {
res.add(copyOfRange(a, from, from <= toIndex - chunkSize ? from + 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 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 from = 0, toIndex = a.length; from < toIndex; from += chunkSize) {
res.add(copyOfRange(a, from, from <= toIndex - chunkSize ? from + 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 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)) {
return new ArrayList<>();
}
final int len = toIndex - fromIndex;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int from = fromIndex; from < toIndex; from += chunkSize) {
res.add(copyOfRange(a, from, from <= toIndex - chunkSize ? from + 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 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 from = 0, toIndex = a.length; from < toIndex; from += chunkSize) {
res.add(copyOfRange(a, from, from <= toIndex - chunkSize ? from + 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 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)) {
return new ArrayList<>();
}
final int len = toIndex - fromIndex;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int from = fromIndex; from < toIndex; from += chunkSize) {
res.add(copyOfRange(a, from, from <= toIndex - chunkSize ? from + 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 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 from = 0, toIndex = a.length; from < toIndex; from += chunkSize) {
res.add(copyOfRange(a, from, from <= toIndex - chunkSize ? from + 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 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)) {
return new ArrayList<>();
}
final int len = toIndex - fromIndex;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int from = fromIndex; from < toIndex; from += chunkSize) {
res.add(copyOfRange(a, from, from <= toIndex - chunkSize ? from + 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 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 from = 0, toIndex = a.length; from < toIndex; from += chunkSize) {
res.add(copyOfRange(a, from, from <= toIndex - chunkSize ? from + 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 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)) {
return new ArrayList<>();
}
final int len = toIndex - fromIndex;
final List res = new ArrayList<>(len % chunkSize == 0 ? len / chunkSize : (len / chunkSize) + 1);
for (int from = fromIndex; from < toIndex; from += chunkSize) {
res.add(copyOfRange(a, from, from <= toIndex - chunkSize ? from + 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 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 Collection extends T> c, final int chunkSize) {
checkArgPositive(chunkSize, "chunkSize");
if (isNullOrEmpty(c)) {
return new ArrayList<>();
}
return split(c, 0, c.size(), 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 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)) {
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 += chunkSize) {
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; i += chunkSize) {
if (i < fromIndex) {
iter.next();
i++;
continue;
}
final List subList = new ArrayList<>(min(chunkSize, toIndex - i));
for (int j = i, to = i <= toIndex - chunkSize ? i + chunkSize : toIndex; j < to; j++) {
subList.add(iter.next());
}
res.add(subList);
}
}
return res;
}
/**
* 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 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 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 from = fromIndex; from < toIndex; from += chunkSize) {
res.add(str.subSequence(from, from <= toIndex - chunkSize ? from + chunkSize : toIndex).toString());
}
return res;
}
/**
*
* @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)
*/
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
*/
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;
}
/**
*
* @param
* @param a
* @param b
* @return
* @see IntList#difference(IntList)
*/
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;
}
/**
*
* @param
* @param a
* @param b
* @return
* @see IntList#difference(IntList)
*/
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
* @param a
* @param b
* @return
* @see IntList#symmetricDifference(IntList)
*/
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)
*/
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;
}
/**
* Different set.
*
* @param the generic type
* @param a the a
* @param b the b
* @return the sets the
*/
@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(a, (Collection) b);
return result;
}
/**
* Symmetric different set.
*
* @param the generic type
* @param a the a
* @param b the b
* @return the sets the
*/
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(a);
for (T e : a) {
if (!commonSet.contains(e)) {
result.add(e);
}
}
for (T e : b) {
if (!commonSet.contains(e)) {
result.add(e);
}
}
return result;
}
/**
* Common set.
*
* @param the generic type
* @param a the a
* @param b the b
* @return the sets the
*/
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));
}
/**
* Common set.
*
* @param the generic type
* @param c the c
* @return the sets the
*/
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;
}
/**
*
* @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 NullPointerException if the specified aa
is null
.
*/
@SafeVarargs
public static T[] concat(final T[]... aa) {
checkArgNotNull(aa, "aa");
if (aa.length == 1) {
return isNullOrEmpty(aa[0]) ? aa[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 Collection extends T> a, final Collection extends T> b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? new ArrayList(0) : new ArrayList<>(b);
} else if (isNullOrEmpty(b)) {
return new ArrayList<>(a);
}
final List result = new ArrayList<>(a.size() + b.size());
result.addAll(a);
result.addAll(b);
return result;
}
/**
*
* @param
* @param a
* @return
*/
@SafeVarargs
public static List concat(final Collection 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 Collection extends T>> c) {
return concat(c, Factory. ofList());
}
/**
*
* @param
* @param
* @param c
* @param supplier
* @return
*/
public static > C concat(final Collection extends Collection extends T>> c, final IntFunction extends C> supplier) {
if (isNullOrEmpty(c)) {
return supplier.apply(0);
}
int count = 0;
for (Collection extends T> e : c) {
if (notNullOrEmpty(e)) {
count += e.size();
}
}
final C result = supplier.apply(count);
for (Collection extends T> e : c) {
if (notNullOrEmpty(e)) {
result.addAll(e);
}
}
return result;
}
/**
*
* @param
* @param a
* @param b
* @return
*/
public static ObjIterator concat(final Iterator extends T> a, final Iterator extends T> b) {
return Iterators.concat(a, b);
}
/**
*
* @param
* @param a
* @return
*/
@SafeVarargs
public static ObjIterator concat(final Iterator extends T>... a) {
return Iterators.concat(a);
}
/**
*
* @param
* @param c
* @return
*/
public static ObjIterator concatt(final Collection extends Iterator extends T>> c) {
return Iterators.concat(c);
}
/**
*
* @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;
}
/**
*
* Copies the given array and adds the given element at the end of the new
* array.
*
* @param a
* @param element
* @return A new array containing the existing elements plus the new element
*/
public static boolean[] add(final boolean[] a, final boolean element) {
if (isNullOrEmpty(a)) {
return Array.of(element);
}
final boolean[] newArray = new boolean[a.length + 1];
copy(a, 0, newArray, 0, a.length);
newArray[a.length] = element;
return newArray;
}
/**
*
* Copies the given array and adds the given element at the end of the new
* array.
*
* @param a
* @param element
* @return A new array containing the existing elements plus the new element
*/
public static char[] add(final char[] a, final char element) {
if (isNullOrEmpty(a)) {
return Array.of(element);
}
final char[] newArray = new char[a.length + 1];
copy(a, 0, newArray, 0, a.length);
newArray[a.length] = element;
return newArray;
}
/**
*
* Copies the given array and adds the given element at the end of the new
* array.
*
* @param a
* @param element
* @return A new array containing the existing elements plus the new element
*/
public static byte[] add(final byte[] a, final byte element) {
if (isNullOrEmpty(a)) {
return Array.of(element);
}
final byte[] newArray = new byte[a.length + 1];
copy(a, 0, newArray, 0, a.length);
newArray[a.length] = element;
return newArray;
}
/**
*
* Copies the given array and adds the given element at the end of the new
* array.
*
* @param a
* @param element
* @return A new array containing the existing elements plus the new element
*/
public static short[] add(final short[] a, final short element) {
if (isNullOrEmpty(a)) {
return Array.of(element);
}
final short[] newArray = new short[a.length + 1];
copy(a, 0, newArray, 0, a.length);
newArray[a.length] = element;
return newArray;
}
/**
*
* Copies the given array and adds the given element at the end of the new
* array.
*
* @param a
* @param element
* @return A new array containing the existing elements plus the new element
*/
public static int[] add(final int[] a, final int element) {
if (isNullOrEmpty(a)) {
return Array.of(element);
}
final int[] newArray = new int[a.length + 1];
copy(a, 0, newArray, 0, a.length);
newArray[a.length] = element;
return newArray;
}
/**
*
* Copies the given array and adds the given element at the end of the new
* array.
*
* @param a
* @param element
* @return A new array containing the existing elements plus the new element
*/
public static long[] add(final long[] a, final long element) {
if (isNullOrEmpty(a)) {
return Array.of(element);
}
final long[] newArray = new long[a.length + 1];
copy(a, 0, newArray, 0, a.length);
newArray[a.length] = element;
return newArray;
}
/**
*
* Copies the given array and adds the given element at the end of the new
* array.
*
* @param a
* @param element
* @return A new array containing the existing elements plus the new element
*/
public static float[] add(final float[] a, final float element) {
if (isNullOrEmpty(a)) {
return Array.of(element);
}
final float[] newArray = new float[a.length + 1];
copy(a, 0, newArray, 0, a.length);
newArray[a.length] = element;
return newArray;
}
/**
*
* Copies the given array and adds the given element at the end of the new
* array.
*
* @param a
* @param element
* @return A new array containing the existing elements plus the new element
*/
public static double[] add(final double[] a, final double element) {
if (isNullOrEmpty(a)) {
return Array.of(element);
}
final double[] newArray = new double[a.length + 1];
copy(a, 0, newArray, 0, a.length);
newArray[a.length] = element;
return newArray;
}
/**
*
* Copies the given array and adds the given element at the end of the new
* array.
*
* @param a
* @param element
* @return A new array containing the existing elements plus the new element
*/
public static String[] add(final String[] a, final String element) {
if (isNullOrEmpty(a)) {
return asArray(element);
}
final String[] newArray = new String[a.length + 1];
copy(a, 0, newArray, 0, a.length);
newArray[a.length] = element;
return newArray;
}
/**
*
* Copies the given array and adds the given element at the end of the new
* array.
*
* @param
* @param a
* @param element
* @return A new array containing the existing elements plus the new element
* @throws NullPointerException if the specified a
is null
.
*/
public static T[] add(final T[] a, final T element) {
checkArgNotNull(a, "a");
if (isNullOrEmpty(a)) {
return asArray(element);
}
final T[] newArray = (T[]) Array.newInstance(a.getClass().getComponentType(), a.length + 1);
copy(a, 0, newArray, 0, a.length);
newArray[a.length] = element;
return newArray;
}
/**
*
* Adds all the elements of the given arrays into a new array.
*
*
* @param a
* the first array whose elements are added to the new array.
* @param b
* the second array whose elements are added to the new array.
* @return A new array containing the elements from a and b
*/
@SafeVarargs
public static boolean[] addAll(final boolean[] a, final boolean... b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? EMPTY_BOOLEAN_ARRAY : b.clone();
}
final boolean[] newArray = new boolean[a.length + b.length];
copy(a, 0, newArray, 0, a.length);
copy(b, 0, newArray, a.length, b.length);
return newArray;
}
/**
*
* Adds all the elements of the given arrays into a new array.
*
*
* @param a
* the first array whose elements are added to the new array.
* @param b
* the second array whose elements are added to the new array.
* @return A new array containing the elements from a and b
*/
@SafeVarargs
public static char[] addAll(final char[] a, final char... b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? EMPTY_CHAR_ARRAY : b.clone();
}
final char[] newArray = new char[a.length + b.length];
copy(a, 0, newArray, 0, a.length);
copy(b, 0, newArray, a.length, b.length);
return newArray;
}
/**
*
* Adds all the elements of the given arrays into a new array.
*
*
* @param a
* the first array whose elements are added to the new array.
* @param b
* the second array whose elements are added to the new array.
* @return A new array containing the elements from a and b
*/
@SafeVarargs
public static byte[] addAll(final byte[] a, final byte... b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? EMPTY_BYTE_ARRAY : b.clone();
}
final byte[] newArray = new byte[a.length + b.length];
copy(a, 0, newArray, 0, a.length);
copy(b, 0, newArray, a.length, b.length);
return newArray;
}
/**
*
* Adds all the elements of the given arrays into a new array.
*
*
* @param a
* the first array whose elements are added to the new array.
* @param b
* the second array whose elements are added to the new array.
* @return A new array containing the elements from a and b
*/
@SafeVarargs
public static short[] addAll(final short[] a, final short... b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? EMPTY_SHORT_ARRAY : b.clone();
}
final short[] newArray = new short[a.length + b.length];
copy(a, 0, newArray, 0, a.length);
copy(b, 0, newArray, a.length, b.length);
return newArray;
}
/**
*
* Adds all the elements of the given arrays into a new array.
*
*
* @param a
* the first array whose elements are added to the new array.
* @param b
* the second array whose elements are added to the new array.
* @return A new array containing the elements from a and b
*/
@SafeVarargs
public static int[] addAll(final int[] a, final int... b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? EMPTY_INT_ARRAY : b.clone();
}
final int[] newArray = new int[a.length + b.length];
copy(a, 0, newArray, 0, a.length);
copy(b, 0, newArray, a.length, b.length);
return newArray;
}
/**
*
* Adds all the elements of the given arrays into a new array.
*
*
* @param a
* the first array whose elements are added to the new array.
* @param b
* the second array whose elements are added to the new array.
* @return A new array containing the elements from a and b
*/
@SafeVarargs
public static long[] addAll(final long[] a, final long... b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? EMPTY_LONG_ARRAY : b.clone();
}
final long[] newArray = new long[a.length + b.length];
copy(a, 0, newArray, 0, a.length);
copy(b, 0, newArray, a.length, b.length);
return newArray;
}
/**
*
* Adds all the elements of the given arrays into a new array.
*
*
* @param a
* the first array whose elements are added to the new array.
* @param b
* the second array whose elements are added to the new array.
* @return A new array containing the elements from a and b
*/
@SafeVarargs
public static float[] addAll(final float[] a, final float... b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? EMPTY_FLOAT_ARRAY : b.clone();
}
final float[] newArray = new float[a.length + b.length];
copy(a, 0, newArray, 0, a.length);
copy(b, 0, newArray, a.length, b.length);
return newArray;
}
/**
*
* Adds all the elements of the given arrays into a new array.
*
*
* @param a
* the first array whose elements are added to the new array.
* @param b
* the second array whose elements are added to the new array.
* @return A new array containing the elements from a and b
*/
@SafeVarargs
public static double[] addAll(final double[] a, final double... b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? EMPTY_DOUBLE_ARRAY : b.clone();
}
final double[] newArray = new double[a.length + b.length];
copy(a, 0, newArray, 0, a.length);
copy(b, 0, newArray, a.length, b.length);
return newArray;
}
/**
*
* Adds all the elements of the given arrays into a new array.
*
*
* @param a
* the first array whose elements are added to the new array.
* @param b
* the second array whose elements are added to the new array.
* @return A new array containing the elements from a and b
*/
@SafeVarargs
public static String[] addAll(final String[] a, final String... b) {
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? EMPTY_STRING_ARRAY : b.clone();
}
final String[] newArray = new String[a.length + b.length];
copy(a, 0, newArray, 0, a.length);
copy(b, 0, newArray, a.length, b.length);
return newArray;
}
/**
*
* Adds all the elements of the given arrays into a new array.
*
*
* @param
* @param a the first array whose elements are added to the new array.
* @param b the second array whose elements are added to the new array.
* @return A new array containing the elements from a and b
* @throws NullPointerException if the specified a
is null
.
*/
@SafeVarargs
public static T[] addAll(final T[] a, final T... b) {
checkArgNotNull(a, "a");
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? b : b.clone();
}
final T[] newArray = (T[]) Array.newInstance(a.getClass().getComponentType(), a.length + b.length);
copy(a, 0, newArray, 0, a.length);
copy(b, 0, newArray, a.length, b.length);
return newArray;
}
/**
*
* Inserts the specified element at the specified position in the array.
* Shifts the element currently at that position (if any) and any subsequent
* elements to the right (adds one to their indices).
*
*
*
* This method returns a new array with the same elements of the input array
* plus the given element on the specified position. The component type of
* the returned array is always the same as that of the input array.
*
*
* @param a
* @param index the position of the new object
* @param element the object to add
* @return A new array containing the existing elements and the new element
*/
public static boolean[] insert(final boolean[] a, final int index, final boolean element) {
if (isNullOrEmpty(a) && index == 0) {
return Array.of(element);
}
final boolean[] newArray = new boolean[a.length + 1];
if (index > 0) {
copy(a, 0, newArray, 0, index);
}
newArray[index] = element;
if (index < a.length) {
copy(a, index, newArray, index + 1, a.length - index);
}
return newArray;
}
/**
*
* Inserts the specified element at the specified position in the array.
* Shifts the element currently at that position (if any) and any subsequent
* elements to the right (adds one to their indices).
*
*
*
* This method returns a new array with the same elements of the input array
* plus the given element on the specified position. The component type of
* the returned array is always the same as that of the input array.
*
*
* @param a
* @param index the position of the new object
* @param element the object to add
* @return A new array containing the existing elements and the new element
*/
public static char[] insert(final char[] a, final int index, final char element) {
if (isNullOrEmpty(a) && index == 0) {
return Array.of(element);
}
final char[] newArray = new char[a.length + 1];
if (index > 0) {
copy(a, 0, newArray, 0, index);
}
newArray[index] = element;
if (index < a.length) {
copy(a, index, newArray, index + 1, a.length - index);
}
return newArray;
}
/**
*
* Inserts the specified element at the specified position in the array.
* Shifts the element currently at that position (if any) and any subsequent
* elements to the right (adds one to their indices).
*
*
*
* This method returns a new array with the same elements of the input array
* plus the given element on the specified position. The component type of
* the returned array is always the same as that of the input array.
*
*
* @param a
* @param index the position of the new object
* @param element the object to add
* @return A new array containing the existing elements and the new element
*/
public static byte[] insert(final byte[] a, final int index, final byte element) {
if (isNullOrEmpty(a) && index == 0) {
return Array.of(element);
}
final byte[] newArray = new byte[a.length + 1];
if (index > 0) {
copy(a, 0, newArray, 0, index);
}
newArray[index] = element;
if (index < a.length) {
copy(a, index, newArray, index + 1, a.length - index);
}
return newArray;
}
/**
*
* Inserts the specified element at the specified position in the array.
* Shifts the element currently at that position (if any) and any subsequent
* elements to the right (adds one to their indices).
*
*
*
* This method returns a new array with the same elements of the input array
* plus the given element on the specified position. The component type of
* the returned array is always the same as that of the input array.
*
*
* @param a
* @param index the position of the new object
* @param element the object to add
* @return A new array containing the existing elements and the new element
*/
public static short[] insert(final short[] a, final int index, final short element) {
if (isNullOrEmpty(a) && index == 0) {
return Array.of(element);
}
final short[] newArray = new short[a.length + 1];
if (index > 0) {
copy(a, 0, newArray, 0, index);
}
newArray[index] = element;
if (index < a.length) {
copy(a, index, newArray, index + 1, a.length - index);
}
return newArray;
}
/**
*
* Inserts the specified element at the specified position in the array.
* Shifts the element currently at that position (if any) and any subsequent
* elements to the right (adds one to their indices).
*
*
*
* This method returns a new array with the same elements of the input array
* plus the given element on the specified position. The component type of
* the returned array is always the same as that of the input array.
*
*
* @param a
* @param index the position of the new object
* @param element the object to add
* @return A new array containing the existing elements and the new element
*/
public static int[] insert(final int[] a, final int index, final int element) {
if (isNullOrEmpty(a) && index == 0) {
return Array.of(element);
}
final int[] newArray = new int[a.length + 1];
if (index > 0) {
copy(a, 0, newArray, 0, index);
}
newArray[index] = element;
if (index < a.length) {
copy(a, index, newArray, index + 1, a.length - index);
}
return newArray;
}
/**
*
* Inserts the specified element at the specified position in the array.
* Shifts the element currently at that position (if any) and any subsequent
* elements to the right (adds one to their indices).
*
*
*
* This method returns a new array with the same elements of the input array
* plus the given element on the specified position. The component type of
* the returned array is always the same as that of the input array.
*
*
* @param a
* @param index the position of the new object
* @param element the object to add
* @return A new array containing the existing elements and the new element
*/
public static long[] insert(final long[] a, final int index, final long element) {
if (isNullOrEmpty(a) && index == 0) {
return Array.of(element);
}
final long[] newArray = new long[a.length + 1];
if (index > 0) {
copy(a, 0, newArray, 0, index);
}
newArray[index] = element;
if (index < a.length) {
copy(a, index, newArray, index + 1, a.length - index);
}
return newArray;
}
/**
*
* Inserts the specified element at the specified position in the array.
* Shifts the element currently at that position (if any) and any subsequent
* elements to the right (adds one to their indices).
*
*
*
* This method returns a new array with the same elements of the input array
* plus the given element on the specified position. The component type of
* the returned array is always the same as that of the input array.
*
*
* @param a
* @param index the position of the new object
* @param element the object to add
* @return A new array containing the existing elements and the new element
*/
public static float[] insert(final float[] a, final int index, final float element) {
if (isNullOrEmpty(a) && index == 0) {
return Array.of(element);
}
final float[] newArray = new float[a.length + 1];
if (index > 0) {
copy(a, 0, newArray, 0, index);
}
newArray[index] = element;
if (index < a.length) {
copy(a, index, newArray, index + 1, a.length - index);
}
return newArray;
}
/**
*
* Inserts the specified element at the specified position in the array.
* Shifts the element currently at that position (if any) and any subsequent
* elements to the right (adds one to their indices).
*
*
*
* This method returns a new array with the same elements of the input array
* plus the given element on the specified position. The component type of
* the returned array is always the same as that of the input array.
*
*
* @param a
* @param index the position of the new object
* @param element the object to add
* @return A new array containing the existing elements and the new element
*/
public static double[] insert(final double[] a, final int index, final double element) {
if (isNullOrEmpty(a) && index == 0) {
return Array.of(element);
}
final double[] newArray = new double[a.length + 1];
if (index > 0) {
copy(a, 0, newArray, 0, index);
}
newArray[index] = element;
if (index < a.length) {
copy(a, index, newArray, index + 1, a.length - index);
}
return newArray;
}
/**
*
* @param a
* @param index
* @param element
* @return
*/
public static String[] insert(final String[] a, final int index, final String element) {
if (isNullOrEmpty(a) && index == 0) {
return asArray(element);
}
final String[] newArray = new String[a.length + 1];
if (index > 0) {
copy(a, 0, newArray, 0, index);
}
newArray[index] = element;
if (index < a.length) {
copy(a, index, newArray, index + 1, a.length - index);
}
return newArray;
}
/**
*
* Inserts the specified element at the specified position in the array.
* Shifts the element currently at that position (if any) and any subsequent
* elements to the right (adds one to their indices).
*
*
*
* This method returns a new array with the same elements of the input array
* plus the given element on the specified position. The component type of
* the returned array is always the same as that of the input array.
*
*
* @param
* @param a
* @param index the position of the new object
* @param element the object to add
* @return A new array containing the existing elements and the new element
* @throws NullPointerException if the specified a
is null
.
*/
public static T[] insert(final T[] a, final int index, final T element) {
checkArgNotNull(a, "a");
final T[] newArray = newArray(a.getClass().getComponentType(), a.length + 1);
if (index > 0) {
copy(a, 0, newArray, 0, index);
}
newArray[index] = element;
if (index < a.length) {
copy(a, index, newArray, index + 1, a.length - index);
}
return newArray;
}
/**
*
* Inserts the specified elements at the specified position in the array.
* Shifts the element currently at that position (if any) and any subsequent
* elements to the right (adds one to their indices).
*
*
* @param a
* the first array whose elements are added to the new array.
* @param index
* the position of the new elements start from
* @param b
* the second array whose elements are added to the new array.
* @return A new array containing the elements from a and b
*/
@SafeVarargs
public static boolean[] insertAll(final boolean[] a, final int index, final boolean... b) {
if (isNullOrEmpty(a) && index == 0) {
return b.clone();
}
final boolean[] newArray = new boolean[a.length + b.length];
if (index > 0) {
copy(a, 0, newArray, 0, index);
}
copy(b, 0, newArray, index, b.length);
if (index < a.length) {
copy(a, index, newArray, index + b.length, a.length - index);
}
return newArray;
}
/**
*
* Inserts the specified elements at the specified position in the array.
* Shifts the element currently at that position (if any) and any subsequent
* elements to the right (adds one to their indices).
*
*
* @param a
* the first array whose elements are added to the new array.
* @param index
* the position of the new elements start from
* @param b
* the second array whose elements are added to the new array.
* @return A new array containing the elements from a and b
*/
@SafeVarargs
public static char[] insertAll(final char[] a, final int index, final char... b) {
if (isNullOrEmpty(a) && index == 0) {
return b.clone();
}
final char[] newArray = new char[a.length + b.length];
if (index > 0) {
copy(a, 0, newArray, 0, index);
}
copy(b, 0, newArray, index, b.length);
if (index < a.length) {
copy(a, index, newArray, index + b.length, a.length - index);
}
return newArray;
}
/**
*
* Inserts the specified elements at the specified position in the array.
* Shifts the element currently at that position (if any) and any subsequent
* elements to the right (adds one to their indices).
*
*
* @param a
* the first array whose elements are added to the new array.
* @param index
* the position of the new elements start from
* @param b
* the second array whose elements are added to the new array.
* @return A new array containing the elements from a and b
*/
@SafeVarargs
public static byte[] insertAll(final byte[] a, final int index, final byte... b) {
if (isNullOrEmpty(a) && index == 0) {
return b.clone();
}
final byte[] newArray = new byte[a.length + b.length];
if (index > 0) {
copy(a, 0, newArray, 0, index);
}
copy(b, 0, newArray, index, b.length);
if (index < a.length) {
copy(a, index, newArray, index + b.length, a.length - index);
}
return newArray;
}
/**
*
* Inserts the specified elements at the specified position in the array.
* Shifts the element currently at that position (if any) and any subsequent
* elements to the right (adds one to their indices).
*
*
* @param a
* the first array whose elements are added to the new array.
* @param index
* the position of the new elements start from
* @param b
* the second array whose elements are added to the new array.
* @return A new array containing the elements from a and b
*/
@SafeVarargs
public static short[] insertAll(final short[] a, final int index, final short... b) {
if (isNullOrEmpty(a) && index == 0) {
return b.clone();
}
final short[] newArray = new short[a.length + b.length];
if (index > 0) {
copy(a, 0, newArray, 0, index);
}
copy(b, 0, newArray, index, b.length);
if (index < a.length) {
copy(a, index, newArray, index + b.length, a.length - index);
}
return newArray;
}
/**
*
* Inserts the specified elements at the specified position in the array.
* Shifts the element currently at that position (if any) and any subsequent
* elements to the right (adds one to their indices).
*
*
* @param a
* the first array whose elements are added to the new array.
* @param index
* the position of the new elements start from
* @param b
* the second array whose elements are added to the new array.
* @return A new array containing the elements from a and b
*/
@SafeVarargs
public static int[] insertAll(final int[] a, final int index, final int... b) {
if (isNullOrEmpty(a) && index == 0) {
return b.clone();
}
final int[] newArray = new int[a.length + b.length];
if (index > 0) {
copy(a, 0, newArray, 0, index);
}
copy(b, 0, newArray, index, b.length);
if (index < a.length) {
copy(a, index, newArray, index + b.length, a.length - index);
}
return newArray;
}
/**
*
* Inserts the specified elements at the specified position in the array.
* Shifts the element currently at that position (if any) and any subsequent
* elements to the right (adds one to their indices).
*
*
* @param a
* the first array whose elements are added to the new array.
* @param index
* the position of the new elements start from
* @param b
* the second array whose elements are added to the new array.
* @return A new array containing the elements from a and b
*/
@SafeVarargs
public static long[] insertAll(final long[] a, final int index, final long... b) {
if (isNullOrEmpty(a) && index == 0) {
return b.clone();
}
final long[] newArray = new long[a.length + b.length];
if (index > 0) {
copy(a, 0, newArray, 0, index);
}
copy(b, 0, newArray, index, b.length);
if (index < a.length) {
copy(a, index, newArray, index + b.length, a.length - index);
}
return newArray;
}
/**
*
* Inserts the specified elements at the specified position in the array.
* Shifts the element currently at that position (if any) and any subsequent
* elements to the right (adds one to their indices).
*
*
* @param a
* the first array whose elements are added to the new array.
* @param index
* the position of the new elements start from
* @param b
* the second array whose elements are added to the new array.
* @return A new array containing the elements from a and b
*/
@SafeVarargs
public static float[] insertAll(final float[] a, final int index, final float... b) {
if (isNullOrEmpty(a) && index == 0) {
return b.clone();
}
final float[] newArray = new float[a.length + b.length];
if (index > 0) {
copy(a, 0, newArray, 0, index);
}
copy(b, 0, newArray, index, b.length);
if (index < a.length) {
copy(a, index, newArray, index + b.length, a.length - index);
}
return newArray;
}
/**
*
* Inserts the specified elements at the specified position in the array.
* Shifts the element currently at that position (if any) and any subsequent
* elements to the right (adds one to their indices).
*
*
* @param a
* the first array whose elements are added to the new array.
* @param index
* the position of the new elements start from
* @param b
* the second array whose elements are added to the new array.
* @return A new array containing the elements from a and b
*/
@SafeVarargs
public static double[] insertAll(final double[] a, final int index, final double... b) {
if (isNullOrEmpty(a) && index == 0) {
return b.clone();
}
final double[] newArray = new double[a.length + b.length];
if (index > 0) {
copy(a, 0, newArray, 0, index);
}
copy(b, 0, newArray, index, b.length);
if (index < a.length) {
copy(a, index, newArray, index + b.length, a.length - index);
}
return newArray;
}
/**
*
* @param a
* @param index
* @param b
* @return
*/
@SafeVarargs
public static String[] insertAll(final String[] a, final int index, final String... b) {
if (isNullOrEmpty(a) && index == 0) {
return b.clone();
}
final String[] newArray = new String[a.length + b.length];
if (index > 0) {
copy(a, 0, newArray, 0, index);
}
copy(b, 0, newArray, index, b.length);
if (index < a.length) {
copy(a, index, newArray, index + b.length, a.length - index);
}
return newArray;
}
/**
*
* Inserts the specified elements at the specified position in the array.
* Shifts the element currently at that position (if any) and any subsequent
* elements to the right (adds one to their indices).
*
*
* @param
* @param a the first array whose elements are added to the new array.
* @param index the position of the new elements start from
* @param b the second array whose elements are added to the new array.
* @return A new array containing the elements from a and b
* @throws NullPointerException if the specified a
is null
.
*/
@SafeVarargs
public static T[] insertAll(final T[] a, final int index, final T... b) {
checkArgNotNull(a, "a");
final T[] newArray = (T[]) Array.newInstance(a.getClass().getComponentType(), a.length + b.length);
if (index > 0) {
copy(a, 0, newArray, 0, index);
}
copy(b, 0, newArray, index, b.length);
if (index < a.length) {
copy(a, index, newArray, index + b.length, a.length - index);
}
return newArray;
}
/**
*
* Removes the element at the specified position from the specified array.
* All subsequent elements are shifted to the left (subtracts one from their
* indices).
*
*
*
* This method returns a new array with the same elements of the input array
* except the element on the specified position. The component type of the
* returned array is always the same as that of the input array.
*
*
* @param a
* @param index the position of the element to be removed
* @return A new array containing the existing elements except the element
* at the specified position.
*/
public static boolean[] delete(final boolean[] a, final int index) {
final boolean[] result = new boolean[a.length - 1];
if (index > 0) {
copy(a, 0, result, 0, index);
}
if (index + 1 < a.length) {
copy(a, index + 1, result, index, a.length - index - 1);
}
return result;
}
/**
*
* Removes the element at the specified position from the specified array.
* All subsequent elements are shifted to the left (subtracts one from their
* indices).
*
*
*
* This method returns a new array with the same elements of the input array
* except the element on the specified position. The component type of the
* returned array is always the same as that of the input array.
*
*
* @param a
* @param index the position of the element to be removed
* @return A new array containing the existing elements except the element
* at the specified position.
*/
public static char[] delete(final char[] a, final int index) {
final char[] result = new char[a.length - 1];
if (index > 0) {
copy(a, 0, result, 0, index);
}
if (index + 1 < a.length) {
copy(a, index + 1, result, index, a.length - index - 1);
}
return result;
}
/**
*
* Removes the element at the specified position from the specified array.
* All subsequent elements are shifted to the left (subtracts one from their
* indices).
*
*
*
* This method returns a new array with the same elements of the input array
* except the element on the specified position. The component type of the
* returned array is always the same as that of the input array.
*
*
* @param a
* @param index the position of the element to be removed
* @return A new array containing the existing elements except the element
* at the specified position.
*/
public static byte[] delete(final byte[] a, final int index) {
final byte[] result = new byte[a.length - 1];
if (index > 0) {
copy(a, 0, result, 0, index);
}
if (index + 1 < a.length) {
copy(a, index + 1, result, index, a.length - index - 1);
}
return result;
}
/**
*
* Removes the element at the specified position from the specified array.
* All subsequent elements are shifted to the left (subtracts one from their
* indices).
*
*
*
* This method returns a new array with the same elements of the input array
* except the element on the specified position. The component type of the
* returned array is always the same as that of the input array.
*
*
* @param a
* @param index the position of the element to be removed
* @return A new array containing the existing elements except the element
* at the specified position.
*/
public static short[] delete(final short[] a, final int index) {
final short[] result = new short[a.length - 1];
if (index > 0) {
copy(a, 0, result, 0, index);
}
if (index + 1 < a.length) {
copy(a, index + 1, result, index, a.length - index - 1);
}
return result;
}
/**
*
* Removes the element at the specified position from the specified array.
* All subsequent elements are shifted to the left (subtracts one from their
* indices).
*
*
*
* This method returns a new array with the same elements of the input array
* except the element on the specified position. The component type of the
* returned array is always the same as that of the input array.
*
*
* @param a
* @param index the position of the element to be removed
* @return A new array containing the existing elements except the element
* at the specified position.
*/
public static int[] delete(final int[] a, final int index) {
final int[] result = new int[a.length - 1];
if (index > 0) {
copy(a, 0, result, 0, index);
}
if (index + 1 < a.length) {
copy(a, index + 1, result, index, a.length - index - 1);
}
return result;
}
/**
*
* Removes the element at the specified position from the specified array.
* All subsequent elements are shifted to the left (subtracts one from their
* indices).
*
*
*
* This method returns a new array with the same elements of the input array
* except the element on the specified position. The component type of the
* returned array is always the same as that of the input array.
*
*
* @param a
* @param index the position of the element to be removed
* @return A new array containing the existing elements except the element
* at the specified position.
*/
public static long[] delete(final long[] a, final int index) {
final long[] result = new long[a.length - 1];
if (index > 0) {
copy(a, 0, result, 0, index);
}
if (index + 1 < a.length) {
copy(a, index + 1, result, index, a.length - index - 1);
}
return result;
}
/**
*
* Removes the element at the specified position from the specified array.
* All subsequent elements are shifted to the left (subtracts one from their
* indices).
*
*
*
* This method returns a new array with the same elements of the input array
* except the element on the specified position. The component type of the
* returned array is always the same as that of the input array.
*
*
* @param a
* @param index the position of the element to be removed
* @return A new array containing the existing elements except the element
* at the specified position.
*/
public static float[] delete(final float[] a, final int index) {
final float[] result = new float[a.length - 1];
if (index > 0) {
copy(a, 0, result, 0, index);
}
if (index + 1 < a.length) {
copy(a, index + 1, result, index, a.length - index - 1);
}
return result;
}
/**
*
* Removes the element at the specified position from the specified array.
* All subsequent elements are shifted to the left (subtracts one from their
* indices).
*
*
*
* This method returns a new array with the same elements of the input array
* except the element on the specified position. The component type of the
* returned array is always the same as that of the input array.
*
*
* @param a
* @param index the position of the element to be removed
* @return A new array containing the existing elements except the element
* at the specified position.
*/
public static double[] delete(final double[] a, final int index) {
final double[] result = new double[a.length - 1];
if (index > 0) {
copy(a, 0, result, 0, index);
}
if (index + 1 < a.length) {
copy(a, index + 1, result, index, a.length - index - 1);
}
return result;
}
/**
*
* Removes the element at the specified position from the specified array.
* All subsequent elements are shifted to the left (subtracts one from their
* indices).
*
*
*
* This method returns a new array with the same elements of the input array
* except the element on the specified position. The component type of the
* returned array is always the same as that of the input array.
*
*
* @param the component type of the array
* @param a
* @param index the position of the element to be removed
* @return A new array containing the existing elements except the element
* at the specified position.
*/
public static T[] delete(final T[] a, final int index) {
final T[] result = newArray(a.getClass().getComponentType(), a.length - 1);
if (index > 0) {
copy(a, 0, result, 0, index);
}
if (index + 1 < a.length) {
copy(a, index + 1, result, index, a.length - index - 1);
}
return result;
}
/**
*
* Removes the elements at the specified positions from the specified array.
* All remaining elements are shifted to the left.
*
*
*
* This method returns a new array with the same elements of the input array
* except those at the specified positions. The component type of the
* returned array is always the same as that of the input array.
*
*
*
* If the input array is {@code null}, an IndexOutOfBoundsException will be
* thrown, because in that case no valid index can be specified.
*
*
*
* N.deleteAll([true, false, true], 0, 2) = [false]
* N.removeAll([true, false, true], 1, 2) = [true]
*
*
* @param a
* the array to remove the element from, may not be {@code null}
* @param indices
* the positions of the elements to be removed
* @return A new array containing the existing elements except those at the
* specified positions.
*/
@SafeVarargs
public static boolean[] deleteAll(final boolean[] a, int... indices) {
if (isNullOrEmpty(indices)) {
return a.clone();
} else if (indices.length == 1) {
return delete(a, indices[0]);
}
indices = indices.clone();
sort(indices);
final int lastIndex = indices[indices.length - 1];
if (indices[0] < 0 || lastIndex >= a.length) {
throw new IndexOutOfBoundsException("The specified indices are from: " + indices[0] + " to: " + lastIndex);
}
int diff = 1;
for (int i = 1, len = indices.length; i < len; i++) {
if (indices[i] == indices[i - 1]) {
continue;
}
diff++;
}
final boolean[] result = new boolean[a.length - diff];
int dest = 0;
int len = 0;
for (int i = 0, preIndex = -1; i < indices.length; preIndex = indices[i], i++) {
if (indices[i] - preIndex > 1) {
len = indices[i] - preIndex - 1;
copy(a, preIndex + 1, result, dest, len);
dest += len;
}
}
if (lastIndex < a.length - 1) {
len = a.length - lastIndex - 1;
copy(a, lastIndex + 1, result, dest, len);
dest += len;
}
return result;
}
/**
*
* Removes the elements at the specified positions from the specified array.
* All remaining elements are shifted to the left.
*
*
*
* This method returns a new array with the same elements of the input array
* except those at the specified positions. The component type of the
* returned array is always the same as that of the input array.
*
*
*
* N.deleteAll([1], 0) = []
* N.deleteAll([2, 6], 0) = [6]
* N.deleteAll([2, 6], 0, 1) = []
* N.deleteAll([2, 6, 3], 1, 2) = [2]
* N.deleteAll([2, 6, 3], 0, 2) = [6]
* N.deleteAll([2, 6, 3], 0, 1, 2) = []
*
*
* @param a
* @param indices the positions of the elements to be removed
* @return A new array containing the existing elements except those at the
* specified positions.
*/
@SafeVarargs
public static char[] deleteAll(final char[] a, int... indices) {
if (isNullOrEmpty(indices)) {
return a.clone();
} else if (indices.length == 1) {
return delete(a, indices[0]);
}
indices = indices.clone();
sort(indices);
final int lastIndex = indices[indices.length - 1];
if (indices[0] < 0 || lastIndex >= a.length) {
throw new IndexOutOfBoundsException("The specified indices are from: " + indices[0] + " to: " + lastIndex);
}
int diff = 1;
for (int i = 1, len = indices.length; i < len; i++) {
if (indices[i] == indices[i - 1]) {
continue;
}
diff++;
}
final char[] result = new char[a.length - diff];
int dest = 0;
int len = 0;
for (int i = 0, preIndex = -1; i < indices.length; preIndex = indices[i], i++) {
if (indices[i] - preIndex > 1) {
len = indices[i] - preIndex - 1;
copy(a, preIndex + 1, result, dest, len);
dest += len;
}
}
if (lastIndex < a.length - 1) {
len = a.length - lastIndex - 1;
copy(a, lastIndex + 1, result, dest, len);
dest += len;
}
return result;
}
/**
*
* Removes the elements at the specified positions from the specified array.
* All remaining elements are shifted to the left.
*
*
*
* This method returns a new array with the same elements of the input array
* except those at the specified positions. The component type of the
* returned array is always the same as that of the input array.
*
*
*
* N.deleteAll([1], 0) = []
* N.deleteAll([2, 6], 0) = [6]
* N.deleteAll([2, 6], 0, 1) = []
* N.deleteAll([2, 6, 3], 1, 2) = [2]
* N.deleteAll([2, 6, 3], 0, 2) = [6]
* N.deleteAll([2, 6, 3], 0, 1, 2) = []
*
*
* @param a
* @param indices the positions of the elements to be removed
* @return A new array containing the existing elements except those at the
* specified positions.
*/
@SafeVarargs
public static byte[] deleteAll(final byte[] a, int... indices) {
if (isNullOrEmpty(indices)) {
return a.clone();
} else if (indices.length == 1) {
return delete(a, indices[0]);
}
indices = indices.clone();
sort(indices);
final int lastIndex = indices[indices.length - 1];
if (indices[0] < 0 || lastIndex >= a.length) {
throw new IndexOutOfBoundsException("The specified indices are from: " + indices[0] + " to: " + lastIndex);
}
int diff = 1;
for (int i = 1, len = indices.length; i < len; i++) {
if (indices[i] == indices[i - 1]) {
continue;
}
diff++;
}
final byte[] result = new byte[a.length - diff];
int dest = 0;
int len = 0;
for (int i = 0, preIndex = -1; i < indices.length; preIndex = indices[i], i++) {
if (indices[i] - preIndex > 1) {
len = indices[i] - preIndex - 1;
copy(a, preIndex + 1, result, dest, len);
dest += len;
}
}
if (lastIndex < a.length - 1) {
len = a.length - lastIndex - 1;
copy(a, lastIndex + 1, result, dest, len);
dest += len;
}
return result;
}
/**
*
* Removes the elements at the specified positions from the specified array.
* All remaining elements are shifted to the left.
*
*
*
* This method returns a new array with the same elements of the input array
* except those at the specified positions. The component type of the
* returned array is always the same as that of the input array.
*
*
*
* N.deleteAll([1], 0) = []
* N.deleteAll([2, 6], 0) = [6]
* N.deleteAll([2, 6], 0, 1) = []
* N.deleteAll([2, 6, 3], 1, 2) = [2]
* N.deleteAll([2, 6, 3], 0, 2) = [6]
* N.deleteAll([2, 6, 3], 0, 1, 2) = []
*
*
* @param a
* @param indices the positions of the elements to be removed
* @return A new array containing the existing elements except those at the
* specified positions.
*/
@SafeVarargs
public static short[] deleteAll(final short[] a, int... indices) {
if (isNullOrEmpty(indices)) {
return a.clone();
} else if (indices.length == 1) {
return delete(a, indices[0]);
}
indices = indices.clone();
sort(indices);
final int lastIndex = indices[indices.length - 1];
if (indices[0] < 0 || lastIndex >= a.length) {
throw new IndexOutOfBoundsException("The specified indices are from: " + indices[0] + " to: " + lastIndex);
}
int diff = 1;
for (int i = 1, len = indices.length; i < len; i++) {
if (indices[i] == indices[i - 1]) {
continue;
}
diff++;
}
final short[] result = new short[a.length - diff];
int dest = 0;
int len = 0;
for (int i = 0, preIndex = -1; i < indices.length; preIndex = indices[i], i++) {
if (indices[i] - preIndex > 1) {
len = indices[i] - preIndex - 1;
copy(a, preIndex + 1, result, dest, len);
dest += len;
}
}
if (lastIndex < a.length - 1) {
len = a.length - lastIndex - 1;
copy(a, lastIndex + 1, result, dest, len);
dest += len;
}
return result;
}
/**
*
* Removes the elements at the specified positions from the specified array.
* All remaining elements are shifted to the left.
*
*
*
* This method returns a new array with the same elements of the input array
* except those at the specified positions. The component type of the
* returned array is always the same as that of the input array.
*
*
*
* N.deleteAll([1], 0) = []
* N.deleteAll([2, 6], 0) = [6]
* N.deleteAll([2, 6], 0, 1) = []
* N.deleteAll([2, 6, 3], 1, 2) = [2]
* N.deleteAll([2, 6, 3], 0, 2) = [6]
* N.deleteAll([2, 6, 3], 0, 1, 2) = []
*
*
* @param a
* @param indices the positions of the elements to be removed
* @return A new array containing the existing elements except those at the
* specified positions.
* @throws IndexOutOfBoundsException if any index is out of range (index < 0 || index >=
* array.length), or if the array is {@code null}.
*/
@SafeVarargs
public static int[] deleteAll(final int[] a, int... indices) {
if (isNullOrEmpty(indices)) {
return a.clone();
} else if (indices.length == 1) {
return delete(a, indices[0]);
}
indices = indices.clone();
sort(indices);
final int lastIndex = indices[indices.length - 1];
if (indices[0] < 0 || lastIndex >= a.length) {
throw new IndexOutOfBoundsException("The specified indices are from: " + indices[0] + " to: " + lastIndex);
}
int diff = 1;
for (int i = 1, len = indices.length; i < len; i++) {
if (indices[i] == indices[i - 1]) {
continue;
}
diff++;
}
final int[] result = new int[a.length - diff];
int dest = 0;
int len = 0;
for (int i = 0, preIndex = -1; i < indices.length; preIndex = indices[i], i++) {
if (indices[i] - preIndex > 1) {
len = indices[i] - preIndex - 1;
copy(a, preIndex + 1, result, dest, len);
dest += len;
}
}
if (lastIndex < a.length - 1) {
len = a.length - lastIndex - 1;
copy(a, lastIndex + 1, result, dest, len);
dest += len;
}
return result;
}
/**
*
* Removes the elements at the specified positions from the specified array.
* All remaining elements are shifted to the left.
*
*
*
* This method returns a new array with the same elements of the input array
* except those at the specified positions. The component type of the
* returned array is always the same as that of the input array.
*
*
*
* N.deleteAll([1], 0) = []
* N.deleteAll([2, 6], 0) = [6]
* N.deleteAll([2, 6], 0, 1) = []
* N.deleteAll([2, 6, 3], 1, 2) = [2]
* N.deleteAll([2, 6, 3], 0, 2) = [6]
* N.deleteAll([2, 6, 3], 0, 1, 2) = []
*
*
* @param a
* the array to remove the element from, may not be {@code null}
* @param indices
* the positions of the elements to be removed
* @return A new array containing the existing elements except those at the
* specified positions.
*/
@SafeVarargs
public static long[] deleteAll(final long[] a, int... indices) {
if (isNullOrEmpty(indices)) {
return a.clone();
} else if (indices.length == 1) {
return delete(a, indices[0]);
}
indices = indices.clone();
sort(indices);
final int lastIndex = indices[indices.length - 1];
if (indices[0] < 0 || lastIndex >= a.length) {
throw new IndexOutOfBoundsException("The specified indices are from: " + indices[0] + " to: " + lastIndex);
}
int diff = 1;
for (int i = 1, len = indices.length; i < len; i++) {
if (indices[i] == indices[i - 1]) {
continue;
}
diff++;
}
final long[] result = new long[a.length - diff];
int dest = 0;
int len = 0;
for (int i = 0, preIndex = -1; i < indices.length; preIndex = indices[i], i++) {
if (indices[i] - preIndex > 1) {
len = indices[i] - preIndex - 1;
copy(a, preIndex + 1, result, dest, len);
dest += len;
}
}
if (lastIndex < a.length - 1) {
len = a.length - lastIndex - 1;
copy(a, lastIndex + 1, result, dest, len);
dest += len;
}
return result;
}
/**
*
* Removes the elements at the specified positions from the specified array.
* All remaining elements are shifted to the left.
*
*
*
* This method returns a new array with the same elements of the input array
* except those at the specified positions. The component type of the
* returned array is always the same as that of the input array.
*
*
*
* N.deleteAll([1], 0) = []
* N.deleteAll([2, 6], 0) = [6]
* N.deleteAll([2, 6], 0, 1) = []
* N.deleteAll([2, 6, 3], 1, 2) = [2]
* N.deleteAll([2, 6, 3], 0, 2) = [6]
* N.deleteAll([2, 6, 3], 0, 1, 2) = []
*
*
* @param a
* @param indices the positions of the elements to be removed
* @return A new array containing the existing elements except those at the
* specified positions.
*/
@SafeVarargs
public static float[] deleteAll(final float[] a, int... indices) {
if (isNullOrEmpty(indices)) {
return a.clone();
} else if (indices.length == 1) {
return delete(a, indices[0]);
}
indices = indices.clone();
sort(indices);
final int lastIndex = indices[indices.length - 1];
if (indices[0] < 0 || lastIndex >= a.length) {
throw new IndexOutOfBoundsException("The specified indices are from: " + indices[0] + " to: " + lastIndex);
}
int diff = 1;
for (int i = 1, len = indices.length; i < len; i++) {
if (indices[i] == indices[i - 1]) {
continue;
}
diff++;
}
final float[] result = new float[a.length - diff];
int dest = 0;
int len = 0;
for (int i = 0, preIndex = -1; i < indices.length; preIndex = indices[i], i++) {
if (indices[i] - preIndex > 1) {
len = indices[i] - preIndex - 1;
copy(a, preIndex + 1, result, dest, len);
dest += len;
}
}
if (lastIndex < a.length - 1) {
len = a.length - lastIndex - 1;
copy(a, lastIndex + 1, result, dest, len);
dest += len;
}
return result;
}
/**
*
* Removes the elements at the specified positions from the specified array.
* All remaining elements are shifted to the left.
*
*
*
* This method returns a new array with the same elements of the input array
* except those at the specified positions. The component type of the
* returned array is always the same as that of the input array.
*
*
*
* N.deleteAll([1], 0) = []
* N.deleteAll([2, 6], 0) = [6]
* N.deleteAll([2, 6], 0, 1) = []
* N.deleteAll([2, 6, 3], 1, 2) = [2]
* N.deleteAll([2, 6, 3], 0, 2) = [6]
* N.deleteAll([2, 6, 3], 0, 1, 2) = []
*
*
* @param a
* @param indices the positions of the elements to be removed
* @return A new array containing the existing elements except those at the
* specified positions.
*/
@SafeVarargs
public static double[] deleteAll(final double[] a, int... indices) {
if (isNullOrEmpty(indices)) {
return a.clone();
} else if (indices.length == 1) {
return delete(a, indices[0]);
}
indices = indices.clone();
sort(indices);
final int lastIndex = indices[indices.length - 1];
if (indices[0] < 0 || lastIndex >= a.length) {
throw new IndexOutOfBoundsException("The specified indices are from: " + indices[0] + " to: " + lastIndex);
}
int diff = 1;
for (int i = 1, len = indices.length; i < len; i++) {
if (indices[i] == indices[i - 1]) {
continue;
}
diff++;
}
final double[] result = new double[a.length - diff];
int dest = 0;
int len = 0;
for (int i = 0, preIndex = -1; i < indices.length; preIndex = indices[i], i++) {
if (indices[i] - preIndex > 1) {
len = indices[i] - preIndex - 1;
copy(a, preIndex + 1, result, dest, len);
dest += len;
}
}
if (lastIndex < a.length - 1) {
len = a.length - lastIndex - 1;
copy(a, lastIndex + 1, result, dest, len);
dest += len;
}
return result;
}
/**
*
* Removes the elements at the specified positions from the specified array.
* All remaining elements are shifted to the left.
*
*
*
* This method returns a new array with the same elements of the input array
* except those at the specified positions. The component type of the
* returned array is always the same as that of the input array.
*
*
*
*
* N.deleteAll(["a", "b", "c"], 0, 2) = ["b"]
* N.deleteAll(["a", "b", "c"], 1, 2) = ["a"]
*
*
* @param the component type of the array
* @param a
* @param indices the positions of the elements to be removed
* @return A new array containing the existing elements except those at the
* specified positions.
* @throws NullPointerException if the specified a
is null
.
*/
@SafeVarargs
public static T[] deleteAll(final T[] a, int... indices) {
checkArgNotNull(a, "a");
if (isNullOrEmpty(indices)) {
return a.clone();
} else if (indices.length == 1) {
return delete(a, indices[0]);
}
indices = indices.clone();
sort(indices);
return deleteAllBySortedIndices(a, indices);
}
/**
* Delete all by sorted indices.
*
* @param
* @param a
* @param indices
* @return
*/
private static T[] deleteAllBySortedIndices(final T[] a, int... indices) {
final int lastIndex = indices[indices.length - 1];
if (indices[0] < 0 || lastIndex >= a.length) {
throw new IndexOutOfBoundsException("The specified indices are from: " + indices[0] + " to: " + lastIndex);
}
int diff = 1;
for (int i = 1, len = indices.length; i < len; i++) {
if (indices[i] == indices[i - 1]) {
continue;
}
diff++;
}
final T[] result = newArray(a.getClass().getComponentType(), a.length - diff);
int dest = 0;
int len = 0;
for (int i = 0, preIndex = -1; i < indices.length; preIndex = indices[i], i++) {
if (indices[i] - preIndex > 1) {
len = indices[i] - preIndex - 1;
copy(a, preIndex + 1, result, dest, len);
dest += len;
}
}
if (lastIndex < a.length - 1) {
len = a.length - lastIndex - 1;
copy(a, lastIndex + 1, result, dest, len);
dest += len;
}
return result;
}
/**
* Removes the elements at the specified positions from the specified List.
*
* @param list
* @param indices
* @return true, if successful
*/
@SuppressWarnings("rawtypes")
@SafeVarargs
public static boolean deleteAll(final List> list, int... indices) {
checkArgNotNull(list);
if (isNullOrEmpty(indices)) {
return false;
} else if (indices.length == 1) {
list.remove(indices[0]);
return true;
}
indices = indices.clone();
sort(indices);
if (indices[0] < 0 || indices[indices.length - 1] >= list.size()) {
throw new IndexOutOfBoundsException("The specified indices are from: " + indices[0] + " to: " + indices[indices.length - 1]);
}
if (list instanceof LinkedList) {
final Iterator> iterator = list.iterator();
int idx = -1;
for (int i = 0, len = indices.length; i < len; i++) {
if (i > 0 && indices[i] == indices[i - 1]) {
continue;
}
while (idx < indices[i]) {
idx++;
iterator.next();
}
iterator.remove();
}
} else {
final Object[] a = list.toArray();
final Object[] res = deleteAllBySortedIndices(a, indices);
list.clear();
list.addAll((List) Arrays.asList(res));
}
return true;
}
/**
* Deletes the values from {@code fromIndex} to {@code toIndex}.
*
* @param a
* @param fromIndex
* @param toIndex
* @return a new array
*/
public static boolean[] deleteRange(final boolean[] a, final int fromIndex, final int toIndex) {
checkFromToIndex(fromIndex, toIndex, a.length);
if (fromIndex == toIndex) {
return a.clone();
}
final boolean[] b = new boolean[a.length - (toIndex - fromIndex)];
if (fromIndex > 0) {
copy(a, 0, b, 0, fromIndex);
}
if (toIndex < a.length) {
copy(a, toIndex, b, fromIndex, a.length - toIndex);
}
return b;
}
/**
* Deletes the values from {@code fromIndex} to {@code toIndex}.
*
* @param a
* @param fromIndex
* @param toIndex
* @return a new array
*/
public static char[] deleteRange(final char[] a, final int fromIndex, final int toIndex) {
checkFromToIndex(fromIndex, toIndex, a.length);
if (fromIndex == toIndex) {
return a.clone();
}
final char[] b = new char[a.length - (toIndex - fromIndex)];
if (fromIndex > 0) {
copy(a, 0, b, 0, fromIndex);
}
if (toIndex < a.length) {
copy(a, toIndex, b, fromIndex, a.length - toIndex);
}
return b;
}
/**
* Deletes the values from {@code fromIndex} to {@code toIndex}.
*
* @param a
* @param fromIndex
* @param toIndex
* @return a new array
*/
public static byte[] deleteRange(final byte[] a, final int fromIndex, final int toIndex) {
checkFromToIndex(fromIndex, toIndex, a.length);
if (fromIndex == toIndex) {
return a.clone();
}
final byte[] b = new byte[a.length - (toIndex - fromIndex)];
if (fromIndex > 0) {
copy(a, 0, b, 0, fromIndex);
}
if (toIndex < a.length) {
copy(a, toIndex, b, fromIndex, a.length - toIndex);
}
return b;
}
/**
* Deletes the values from {@code fromIndex} to {@code toIndex}.
*
* @param a
* @param fromIndex
* @param toIndex
* @return a new array
*/
public static short[] deleteRange(final short[] a, final int fromIndex, final int toIndex) {
checkFromToIndex(fromIndex, toIndex, a.length);
if (fromIndex == toIndex) {
return a.clone();
}
final short[] b = new short[a.length - (toIndex - fromIndex)];
if (fromIndex > 0) {
copy(a, 0, b, 0, fromIndex);
}
if (toIndex < a.length) {
copy(a, toIndex, b, fromIndex, a.length - toIndex);
}
return b;
}
/**
* Deletes the values from {@code fromIndex} to {@code toIndex}.
*
* @param a
* @param fromIndex
* @param toIndex
* @return a new array
*/
public static int[] deleteRange(final int[] a, final int fromIndex, final int toIndex) {
checkFromToIndex(fromIndex, toIndex, a.length);
if (fromIndex == toIndex) {
return a.clone();
}
final int[] b = new int[a.length - (toIndex - fromIndex)];
if (fromIndex > 0) {
copy(a, 0, b, 0, fromIndex);
}
if (toIndex < a.length) {
copy(a, toIndex, b, fromIndex, a.length - toIndex);
}
return b;
}
/**
* Deletes the values from {@code fromIndex} to {@code toIndex}.
*
* @param a
* @param fromIndex
* @param toIndex
* @return a new array
*/
public static long[] deleteRange(final long[] a, final int fromIndex, final int toIndex) {
checkFromToIndex(fromIndex, toIndex, a.length);
if (fromIndex == toIndex) {
return a.clone();
}
final long[] b = new long[a.length - (toIndex - fromIndex)];
if (fromIndex > 0) {
copy(a, 0, b, 0, fromIndex);
}
if (toIndex < a.length) {
copy(a, toIndex, b, fromIndex, a.length - toIndex);
}
return b;
}
/**
* Deletes the values from {@code fromIndex} to {@code toIndex}.
*
* @param a
* @param fromIndex
* @param toIndex
* @return a new array
*/
public static float[] deleteRange(final float[] a, final int fromIndex, final int toIndex) {
checkFromToIndex(fromIndex, toIndex, a.length);
if (fromIndex == toIndex) {
return a.clone();
}
final float[] b = new float[a.length - (toIndex - fromIndex)];
if (fromIndex > 0) {
copy(a, 0, b, 0, fromIndex);
}
if (toIndex < a.length) {
copy(a, toIndex, b, fromIndex, a.length - toIndex);
}
return b;
}
/**
* Deletes the values from {@code fromIndex} to {@code toIndex}.
*
* @param a
* @param fromIndex
* @param toIndex
* @return a new array
*/
public static double[] deleteRange(final double[] a, final int fromIndex, final int toIndex) {
checkFromToIndex(fromIndex, toIndex, a.length);
if (fromIndex == toIndex) {
return a.clone();
}
final double[] b = new double[a.length - (toIndex - fromIndex)];
if (fromIndex > 0) {
copy(a, 0, b, 0, fromIndex);
}
if (toIndex < a.length) {
copy(a, toIndex, b, fromIndex, a.length - toIndex);
}
return b;
}
/**
* Deletes the values from {@code fromIndex} to {@code toIndex}.
*
* @param
* @param a
* @param fromIndex
* @param toIndex
* @return a new array
*/
public static T[] deleteRange(final T[] a, final int fromIndex, final int toIndex) {
checkFromToIndex(fromIndex, toIndex, a.length);
if (fromIndex == toIndex) {
return a.clone();
}
final T[] b = Array.newInstance(a.getClass().getComponentType(), a.length - (toIndex - fromIndex));
if (fromIndex > 0) {
copy(a, 0, b, 0, fromIndex);
}
if (toIndex < a.length) {
copy(a, toIndex, b, fromIndex, a.length - toIndex);
}
return b;
}
/**
* Returns {@code true} if the {@code List} is updated when {@code fromIndex < toIndex}, otherwise {@code false} is returned when {@code fromIndex == toIndex}.
*
* @param
* @param c
* @param fromIndex
* @param toIndex
* @return true, if successful
*/
@SuppressWarnings("unchecked")
public static boolean deleteRange(final List c, final int fromIndex, final int toIndex) {
checkFromToIndex(fromIndex, toIndex, c.size());
if (fromIndex == toIndex) {
return false;
}
if (c instanceof LinkedList || toIndex - fromIndex <= 3) {
c.subList(fromIndex, toIndex).clear();
} else {
if (isListElementDataFieldGettable && isListElementDataFieldSettable && listElementDataField != null && c instanceof ArrayList) {
T[] array = null;
try {
array = (T[]) listElementDataField.get(c);
copy(array, toIndex, array, fromIndex, c.size() - toIndex);
listSizeField.set(c, c.size() - (toIndex - fromIndex));
// update modCount
c.add(null);
c.remove(c.size() - 1);
return true;
} catch (Throwable e) {
// ignore;
isListElementDataFieldSettable = false;
}
}
final List tmp = new ArrayList<>(c.size() - (toIndex - fromIndex));
if (fromIndex > 0) {
tmp.addAll(c.subList(0, fromIndex));
}
if (toIndex < c.size()) {
tmp.addAll(c.subList(toIndex, c.size()));
}
c.clear();
c.addAll(tmp);
}
return true;
}
/**
*
* Removes the first occurrence of the specified element from the specified
* array. All subsequent elements are shifted to the left (subtracts one
* from their indices). If the array doesn't contains such an element, no
* elements are removed from the array.
*
*
*
* This method returns a new array with the same elements of the input array
* except the first occurrence of the specified element. The component type
* of the returned array is always the same as that of the input array.
*
*
* @param a
* @param element the element to be removed
* @return A new array containing the existing elements except the first
* occurrence of the specified element.
*/
public static boolean[] remove(final boolean[] a, final boolean element) {
if (isNullOrEmpty(a)) {
return EMPTY_BOOLEAN_ARRAY;
}
int index = indexOf(a, 0, element);
return index == INDEX_NOT_FOUND ? a.clone() : delete(a, index);
}
/**
*
* Removes the first occurrence of the specified element from the specified
* array. All subsequent elements are shifted to the left (subtracts one
* from their indices). If the array doesn't contains such an element, no
* elements are removed from the array.
*
*
*
* This method returns a new array with the same elements of the input array
* except the first occurrence of the specified element. The component type
* of the returned array is always the same as that of the input array.
*
*
* @param a
* @param element the element to be removed
* @return A new array containing the existing elements except the first
* occurrence of the specified element.
*/
public static char[] remove(final char[] a, final char element) {
if (isNullOrEmpty(a)) {
return EMPTY_CHAR_ARRAY;
}
int index = indexOf(a, 0, element);
return index == INDEX_NOT_FOUND ? a.clone() : delete(a, index);
}
/**
*
* Removes the first occurrence of the specified element from the specified
* array. All subsequent elements are shifted to the left (subtracts one
* from their indices). If the array doesn't contains such an element, no
* elements are removed from the array.
*
*
*
* This method returns a new array with the same elements of the input array
* except the first occurrence of the specified element. The component type
* of the returned array is always the same as that of the input array.
*
*
* @param a
* @param element the element to be removed
* @return A new array containing the existing elements except the first
* occurrence of the specified element.
*/
public static byte[] remove(final byte[] a, final byte element) {
if (isNullOrEmpty(a)) {
return EMPTY_BYTE_ARRAY;
}
int index = indexOf(a, 0, element);
return index == INDEX_NOT_FOUND ? a.clone() : delete(a, index);
}
/**
*
* Removes the first occurrence of the specified element from the specified
* array. All subsequent elements are shifted to the left (subtracts one
* from their indices). If the array doesn't contains such an element, no
* elements are removed from the array.
*
*
*
* This method returns a new array with the same elements of the input array
* except the first occurrence of the specified element. The component type
* of the returned array is always the same as that of the input array.
*
*
* @param a
* @param element the element to be removed
* @return A new array containing the existing elements except the first
* occurrence of the specified element.
*/
public static short[] remove(final short[] a, final short element) {
if (isNullOrEmpty(a)) {
return EMPTY_SHORT_ARRAY;
}
int index = indexOf(a, 0, element);
return index == INDEX_NOT_FOUND ? a.clone() : delete(a, index);
}
/**
*
* Removes the first occurrence of the specified element from the specified
* array. All subsequent elements are shifted to the left (subtracts one
* from their indices). If the array doesn't contains such an element, no
* elements are removed from the array.
*
*
*
* This method returns a new array with the same elements of the input array
* except the first occurrence of the specified element. The component type
* of the returned array is always the same as that of the input array.
*
*
* @param a
* @param element the element to be removed
* @return A new array containing the existing elements except the first
* occurrence of the specified element.
*/
public static int[] remove(final int[] a, final int element) {
if (isNullOrEmpty(a)) {
return EMPTY_INT_ARRAY;
}
int index = indexOf(a, 0, element);
return index == INDEX_NOT_FOUND ? a.clone() : delete(a, index);
}
/**
*
* Removes the first occurrence of the specified element from the specified
* array. All subsequent elements are shifted to the left (subtracts one
* from their indices). If the array doesn't contains such an element, no
* elements are removed from the array.
*
*
*
* This method returns a new array with the same elements of the input array
* except the first occurrence of the specified element. The component type
* of the returned array is always the same as that of the input array.
*
*
* @param a
* @param element the element to be removed
* @return A new array containing the existing elements except the first
* occurrence of the specified element.
*/
public static long[] remove(final long[] a, final long element) {
if (isNullOrEmpty(a)) {
return EMPTY_LONG_ARRAY;
}
int index = indexOf(a, 0, element);
return index == INDEX_NOT_FOUND ? a.clone() : delete(a, index);
}
/**
*
* Removes the first occurrence of the specified element from the specified
* array. All subsequent elements are shifted to the left (subtracts one
* from their indices). If the array doesn't contains such an element, no
* elements are removed from the array.
*
*
*
* This method returns a new array with the same elements of the input array
* except the first occurrence of the specified element. The component type
* of the returned array is always the same as that of the input array.
*
*
* @param a
* @param element the element to be removed
* @return A new array containing the existing elements except the first
* occurrence of the specified element.
*/
public static float[] remove(final float[] a, final float element) {
if (isNullOrEmpty(a)) {
return EMPTY_FLOAT_ARRAY;
}
int index = indexOf(a, 0, element);
return index == INDEX_NOT_FOUND ? a.clone() : delete(a, index);
}
/**
*
* Removes the first occurrence of the specified element from the specified
* array. All subsequent elements are shifted to the left (subtracts one
* from their indices). If the array doesn't contains such an element, no
* elements are removed from the array.
*
*
*
* This method returns a new array with the same elements of the input array
* except the first occurrence of the specified element. The component type
* of the returned array is always the same as that of the input array.
*
*
* @param a
* @param element the element to be removed
* @return A new array containing the existing elements except the first
* occurrence of the specified element.
*/
public static double[] remove(final double[] a, final double element) {
if (isNullOrEmpty(a)) {
return EMPTY_DOUBLE_ARRAY;
}
int index = indexOf(a, 0, element);
return index == INDEX_NOT_FOUND ? a.clone() : delete(a, index);
}
/**
*
* Removes the first occurrence of the specified element from the specified
* array. All subsequent elements are shifted to the left (subtracts one
* from their indices). If the array doesn't contains such an element, no
* elements are removed from the array.
*
*
*
* This method returns a new array with the same elements of the input array
* except the first occurrence of the specified element. The component type
* of the returned array is always the same as that of the input array.
*
*
* @param
* @param a
* @param element the element to be removed
* @return A new array containing the existing elements except the first
* occurrence of the specified element.
*/
public static T[] remove(final T[] a, final T element) {
if (isNullOrEmpty(a)) {
return a;
}
int index = indexOf(a, 0, element);
return index == INDEX_NOT_FOUND ? a.clone() : delete(a, index);
}
/**
*
* Removes the first occurrence of the specified element from the specified
* collection. If the collection doesn't contains such an element, no
* elements are removed from the collection.
*
*
* @param c
* @param element the element to be removed
* @return true if this collection changed as a result of the call
*/
public static boolean remove(final Collection c, final T element) {
if (isNullOrEmpty(c)) {
return false;
}
return c.remove(element);
}
/**
* Removes all the occurrences of the specified element from the specified
* array. All subsequent elements are shifted to the left (subtracts one
* from their indices). If the array doesn't contains such an element, no
* elements are removed from the array.
*
* @param a
* @param element
* @return A new array containing the existing elements except the
* occurrences of the specified element.
*/
public static boolean[] removeAllOccurrences(final boolean[] a, final boolean element) {
if (isNullOrEmpty(a)) {
return EMPTY_BOOLEAN_ARRAY;
}
final boolean[] copy = a.clone();
int idx = 0;
for (int i = 0, len = a.length; i < len; i++) {
if (a[i] == element) {
continue;
}
copy[idx++] = a[i];
}
return idx == copy.length ? copy : copyOfRange(copy, 0, idx);
}
/**
* Removes all the occurrences of the specified element from the specified
* array. All subsequent elements are shifted to the left (subtracts one
* from their indices). If the array doesn't contains such an element, no
* elements are removed from the array.
*
* @param a
* @param element
* @return A new array containing the existing elements except the
* occurrences of the specified element.
*/
public static char[] removeAllOccurrences(final char[] a, final char element) {
if (isNullOrEmpty(a)) {
return EMPTY_CHAR_ARRAY;
}
final char[] copy = a.clone();
int idx = 0;
for (int i = 0, len = a.length; i < len; i++) {
if (a[i] == element) {
continue;
}
copy[idx++] = a[i];
}
return idx == copy.length ? copy : copyOfRange(copy, 0, idx);
}
/**
* Removes all the occurrences of the specified element from the specified
* array. All subsequent elements are shifted to the left (subtracts one
* from their indices). If the array doesn't contains such an element, no
* elements are removed from the array.
*
* @param a
* @param element
* @return A new array containing the existing elements except the
* occurrences of the specified element.
*/
public static byte[] removeAllOccurrences(final byte[] a, final byte element) {
if (isNullOrEmpty(a)) {
return EMPTY_BYTE_ARRAY;
}
final byte[] copy = a.clone();
int idx = 0;
for (int i = 0, len = a.length; i < len; i++) {
if (a[i] == element) {
continue;
}
copy[idx++] = a[i];
}
return idx == copy.length ? copy : copyOfRange(copy, 0, idx);
}
/**
* Removes all the occurrences of the specified element from the specified
* array. All subsequent elements are shifted to the left (subtracts one
* from their indices). If the array doesn't contains such an element, no
* elements are removed from the array.
*
* @param a
* @param element
* @return A new array containing the existing elements except the
* occurrences of the specified element.
*/
public static short[] removeAllOccurrences(final short[] a, final short element) {
if (isNullOrEmpty(a)) {
return EMPTY_SHORT_ARRAY;
}
final short[] copy = a.clone();
int idx = 0;
for (int i = 0, len = a.length; i < len; i++) {
if (a[i] == element) {
continue;
}
copy[idx++] = a[i];
}
return idx == copy.length ? copy : copyOfRange(copy, 0, idx);
}
/**
* Removes all the occurrences of the specified element from the specified
* array. All subsequent elements are shifted to the left (subtracts one
* from their indices). If the array doesn't contains such an element, no
* elements are removed from the array.
*
* @param a
* @param element
* @return A new array containing the existing elements except the
* occurrences of the specified element.
*/
public static int[] removeAllOccurrences(final int[] a, final int element) {
if (isNullOrEmpty(a)) {
return EMPTY_INT_ARRAY;
}
final int[] copy = a.clone();
int idx = 0;
for (int i = 0, len = a.length; i < len; i++) {
if (a[i] == element) {
continue;
}
copy[idx++] = a[i];
}
return idx == copy.length ? copy : copyOfRange(copy, 0, idx);
}
/**
* Removes all the occurrences of the specified element from the specified
* array. All subsequent elements are shifted to the left (subtracts one
* from their indices). If the array doesn't contains such an element, no
* elements are removed from the array.
*
* @param a
* @param element
* @return A new array containing the existing elements except the
* occurrences of the specified element.
*/
public static long[] removeAllOccurrences(final long[] a, final long element) {
if (isNullOrEmpty(a)) {
return EMPTY_LONG_ARRAY;
}
final long[] copy = a.clone();
int idx = 0;
for (int i = 0, len = a.length; i < len; i++) {
if (a[i] == element) {
continue;
}
copy[idx++] = a[i];
}
return idx == copy.length ? copy : copyOfRange(copy, 0, idx);
}
/**
* Removes all the occurrences of the specified element from the specified
* array. All subsequent elements are shifted to the left (subtracts one
* from their indices). If the array doesn't contains such an element, no
* elements are removed from the array.
*
* @param a
* @param element
* @return A new array containing the existing elements except the
* occurrences of the specified element.
*/
public static float[] removeAllOccurrences(final float[] a, final float element) {
if (isNullOrEmpty(a)) {
return EMPTY_FLOAT_ARRAY;
}
final float[] copy = a.clone();
int idx = 0;
for (int i = 0, len = a.length; i < len; i++) {
if (equals(a[i], element)) {
continue;
}
copy[idx++] = a[i];
}
return idx == copy.length ? copy : copyOfRange(copy, 0, idx);
}
/**
* Removes all the occurrences of the specified element from the specified
* array. All subsequent elements are shifted to the left (subtracts one
* from their indices). If the array doesn't contains such an element, no
* elements are removed from the array.
*
* @param a
* @param element
* @return A new array containing the existing elements except the
* occurrences of the specified element.
*/
public static double[] removeAllOccurrences(final double[] a, final double element) {
if (isNullOrEmpty(a)) {
return EMPTY_DOUBLE_ARRAY;
}
final double[] copy = a.clone();
int idx = 0;
for (int i = 0, len = a.length; i < len; i++) {
if (equals(a[i], element)) {
continue;
}
copy[idx++] = a[i];
}
return idx == copy.length ? copy : copyOfRange(copy, 0, idx);
}
/**
* Removes all the occurrences of the specified element from the specified
* array. All subsequent elements are shifted to the left (subtracts one
* from their indices). If the array doesn't contains such an element, no
* elements are removed from the array.
*
* @param
* @param a
* @param element
* @return A new array containing the existing elements except the
* occurrences of the specified element.
*/
public static T[] removeAllOccurrences(final T[] a, final T element) {
if (isNullOrEmpty(a)) {
return a;
}
final T[] copy = a.clone();
int idx = 0;
for (int i = 0, len = a.length; i < len; i++) {
if (equals(a[i], element)) {
continue;
}
copy[idx++] = a[i];
}
return idx == copy.length ? copy : copyOfRange(copy, 0, idx);
}
/**
* Removes the all occurrences.
*
* @param c
* @param element
* @return true, if successful
*/
public static boolean removeAllOccurrences(final Collection c, final T element) {
if (isNullOrEmpty(c)) {
return false;
}
return removeAll(c, asSet(element));
}
/**
* Returns a new array with removes all the occurrences of specified elements from a
.
*
* @param
* @param a
* @param elements
* @return
* @see Collection#removeAll(Collection)
*/
@SafeVarargs
public static T[] removeAll(final T[] a, final T... elements) {
if (isNullOrEmpty(a)) {
return a;
} else if (isNullOrEmpty(elements)) {
return a.clone();
} else if (elements.length == 1) {
return removeAllOccurrences(a, elements[0]);
}
final Set set = asSet(elements);
final List result = new ArrayList<>();
for (T e : a) {
if (!set.contains(e)) {
result.add(e);
}
}
return result.toArray((T[]) newArray(a.getClass().getComponentType(), result.size()));
}
/**
* Removes the all.
*
* @param c
* @param elements
* @return true, if successful
*/
@SafeVarargs
public static boolean removeAll(final Collection c, final T... elements) {
if (isNullOrEmpty(c) || isNullOrEmpty(elements)) {
return false;
} else {
return removeAll(c, asSet(elements));
}
}
/**
* Removes the all.
*
* @param c
* @param objsToRemove
* @return true, if successful
*/
public static boolean removeAll(final Collection c, final Collection extends T> objsToRemove) {
if (N.isNullOrEmpty(c) || N.isNullOrEmpty(objsToRemove)) {
return false;
}
if (c instanceof HashSet && !(objsToRemove instanceof Set)) {
boolean result = false;
for (Object e : objsToRemove) {
result |= c.remove(e);
if (c.size() == 0) {
break;
}
}
return result;
} else {
return c.removeAll(objsToRemove);
}
}
/**
* Removes the duplicates.
*
* @param a
* @return
*/
public static boolean[] removeDuplicates(final boolean[] a) {
if (isNullOrEmpty(a)) {
return EMPTY_BOOLEAN_ARRAY;
}
return removeDuplicates(a, false);
}
/**
* Removes the duplicates.
*
* @param a
* @param isSorted
* @return
*/
public static boolean[] removeDuplicates(final boolean[] a, final boolean isSorted) {
if (isNullOrEmpty(a)) {
return EMPTY_BOOLEAN_ARRAY;
}
return removeDuplicates(a, 0, a.length, isSorted);
}
/**
* Removes the duplicates.
*
* @param a
* @param from
* @param to
* @param isSorted
* @return
*/
public static boolean[] removeDuplicates(final boolean[] a, final int from, final int to, final boolean isSorted) {
checkFromToIndex(from, to, len(a));
if (isNullOrEmpty(a) && from == 0 && to == 0) {
return EMPTY_BOOLEAN_ARRAY;
} else if (to - from <= 1) {
return copyOfRange(a, from, to);
}
final Boolean[] b = new Boolean[2];
for (int i = from; i < to; i++) {
if (b[0] == null) {
b[0] = a[i];
} else if (b[0].booleanValue() != a[i]) {
b[1] = a[i];
break;
}
}
return b[1] == null ? new boolean[] { b[0].booleanValue() } : new boolean[] { b[0].booleanValue(), b[1].booleanValue() };
}
/**
* Removes the duplicates.
*
* @param a
* @return
*/
public static char[] removeDuplicates(final char[] a) {
if (isNullOrEmpty(a)) {
return EMPTY_CHAR_ARRAY;
}
return removeDuplicates(a, false);
}
/**
* Removes the duplicates.
*
* @param a
* @param isSorted
* @return
*/
public static char[] removeDuplicates(final char[] a, final boolean isSorted) {
if (isNullOrEmpty(a)) {
return EMPTY_CHAR_ARRAY;
}
return removeDuplicates(a, 0, a.length, isSorted);
}
/**
* Removes the duplicates.
*
* @param a
* @param from
* @param to
* @param isSorted
* @return
*/
public static char[] removeDuplicates(final char[] a, final int from, final int to, final boolean isSorted) {
checkFromToIndex(from, to, len(a));
if (isNullOrEmpty(a) && from == 0 && to == 0) {
return EMPTY_CHAR_ARRAY;
} else if (to - from <= 1) {
return copyOfRange(a, from, to);
}
if (isSorted) {
final char[] b = (from == 0 && to == a.length) ? a.clone() : copyOfRange(a, from, to);
int idx = 1;
for (int i = 1, len = b.length; i < len; i++) {
if (b[i] == b[i - 1]) {
continue;
}
b[idx++] = b[i];
}
return idx == b.length ? b : copyOfRange(b, 0, idx);
} else {
final Set set = newLinkedHashSet(initHashCapacity(a.length));
for (int i = from; i < to; i++) {
set.add(a[i]);
}
if (set.size() == to - from) {
return (from == 0 && to == a.length) ? a.clone() : copyOfRange(a, from, to);
} else {
final char[] result = new char[set.size()];
int i = 0;
for (char e : set) {
result[i++] = e;
}
return result;
}
}
}
/**
* Removes the duplicates.
*
* @param a
* @return
*/
public static byte[] removeDuplicates(final byte[] a) {
if (isNullOrEmpty(a)) {
return EMPTY_BYTE_ARRAY;
}
return removeDuplicates(a, false);
}
/**
* Removes the duplicates.
*
* @param a
* @param isSorted
* @return
*/
public static byte[] removeDuplicates(final byte[] a, final boolean isSorted) {
if (isNullOrEmpty(a)) {
return EMPTY_BYTE_ARRAY;
}
return removeDuplicates(a, 0, a.length, isSorted);
}
/**
* Removes the duplicates.
*
* @param a
* @param from
* @param to
* @param isSorted
* @return
*/
public static byte[] removeDuplicates(final byte[] a, final int from, final int to, final boolean isSorted) {
checkFromToIndex(from, to, len(a));
if (isNullOrEmpty(a) && from == 0 && to == 0) {
return EMPTY_BYTE_ARRAY;
} else if (to - from <= 1) {
return copyOfRange(a, from, to);
}
if (isSorted) {
final byte[] b = (from == 0 && to == a.length) ? a.clone() : copyOfRange(a, from, to);
int idx = 1;
for (int i = 1, len = b.length; i < len; i++) {
if (b[i] == b[i - 1]) {
continue;
}
b[idx++] = b[i];
}
return idx == b.length ? b : copyOfRange(b, 0, idx);
} else {
final Set set = newLinkedHashSet(initHashCapacity(a.length));
for (int i = from; i < to; i++) {
set.add(a[i]);
}
if (set.size() == to - from) {
return (from == 0 && to == a.length) ? a.clone() : copyOfRange(a, from, to);
} else {
final byte[] result = new byte[set.size()];
int i = 0;
for (byte e : set) {
result[i++] = e;
}
return result;
}
}
}
/**
* Removes the duplicates.
*
* @param a
* @return
*/
public static short[] removeDuplicates(final short[] a) {
if (isNullOrEmpty(a)) {
return EMPTY_SHORT_ARRAY;
}
return removeDuplicates(a, false);
}
/**
* Removes the duplicates.
*
* @param a
* @param isSorted
* @return
*/
public static short[] removeDuplicates(final short[] a, final boolean isSorted) {
if (isNullOrEmpty(a)) {
return EMPTY_SHORT_ARRAY;
}
return removeDuplicates(a, 0, a.length, isSorted);
}
/**
* Removes the duplicates.
*
* @param a
* @param from
* @param to
* @param isSorted
* @return
*/
public static short[] removeDuplicates(final short[] a, final int from, final int to, final boolean isSorted) {
checkFromToIndex(from, to, len(a));
if (isNullOrEmpty(a) && from == 0 && to == 0) {
return EMPTY_SHORT_ARRAY;
} else if (to - from <= 1) {
return copyOfRange(a, from, to);
}
if (isSorted) {
final short[] b = (from == 0 && to == a.length) ? a.clone() : copyOfRange(a, from, to);
int idx = 1;
for (int i = 1, len = b.length; i < len; i++) {
if (b[i] == b[i - 1]) {
continue;
}
b[idx++] = b[i];
}
return idx == b.length ? b : copyOfRange(b, 0, idx);
} else {
final Set set = newLinkedHashSet(initHashCapacity(a.length));
for (int i = from; i < to; i++) {
set.add(a[i]);
}
if (set.size() == to - from) {
return (from == 0 && to == a.length) ? a.clone() : copyOfRange(a, from, to);
} else {
final short[] result = new short[set.size()];
int i = 0;
for (short e : set) {
result[i++] = e;
}
return result;
}
}
}
/**
* Removes the duplicates.
*
* @param a
* @return
*/
public static int[] removeDuplicates(final int[] a) {
if (isNullOrEmpty(a)) {
return EMPTY_INT_ARRAY;
}
return removeDuplicates(a, false);
}
/**
* Removes the duplicates.
*
* @param a
* @param isSorted
* @return
*/
public static int[] removeDuplicates(final int[] a, final boolean isSorted) {
if (isNullOrEmpty(a)) {
return EMPTY_INT_ARRAY;
}
return removeDuplicates(a, 0, a.length, isSorted);
}
/**
* Removes the duplicates.
*
* @param a
* @param from
* @param to
* @param isSorted
* @return
*/
public static int[] removeDuplicates(final int[] a, final int from, final int to, final boolean isSorted) {
checkFromToIndex(from, to, len(a));
if (isNullOrEmpty(a) && from == 0 && to == 0) {
return EMPTY_INT_ARRAY;
} else if (to - from <= 1) {
return copyOfRange(a, from, to);
}
if (isSorted) {
final int[] b = (from == 0 && to == a.length) ? a.clone() : copyOfRange(a, from, to);
int idx = 1;
for (int i = 1, len = b.length; i < len; i++) {
if (b[i] == b[i - 1]) {
continue;
}
b[idx++] = b[i];
}
return idx == b.length ? b : copyOfRange(b, 0, idx);
} else {
final Set set = newLinkedHashSet(initHashCapacity(a.length));
for (int i = from; i < to; i++) {
set.add(a[i]);
}
if (set.size() == to - from) {
return (from == 0 && to == a.length) ? a.clone() : copyOfRange(a, from, to);
} else {
final int[] result = new int[set.size()];
int i = 0;
for (int e : set) {
result[i++] = e;
}
return result;
}
}
}
/**
* Removes the duplicates.
*
* @param a
* @return
*/
public static long[] removeDuplicates(final long[] a) {
if (isNullOrEmpty(a)) {
return EMPTY_LONG_ARRAY;
}
return removeDuplicates(a, false);
}
/**
* Removes the duplicates.
*
* @param a
* @param isSorted
* @return
*/
public static long[] removeDuplicates(final long[] a, final boolean isSorted) {
if (isNullOrEmpty(a)) {
return EMPTY_LONG_ARRAY;
}
return removeDuplicates(a, 0, a.length, isSorted);
}
/**
* Removes the duplicates.
*
* @param a
* @param from
* @param to
* @param isSorted
* @return
*/
public static long[] removeDuplicates(final long[] a, final int from, final int to, final boolean isSorted) {
checkFromToIndex(from, to, len(a));
if (isNullOrEmpty(a) && from == 0 && to == 0) {
return EMPTY_LONG_ARRAY;
} else if (to - from <= 1) {
return copyOfRange(a, from, to);
}
if (isSorted) {
final long[] b = (from == 0 && to == a.length) ? a.clone() : copyOfRange(a, from, to);
int idx = 1;
for (int i = 1, len = b.length; i < len; i++) {
if (b[i] == b[i - 1]) {
continue;
}
b[idx++] = b[i];
}
return idx == b.length ? b : copyOfRange(b, 0, idx);
} else {
final Set set = newLinkedHashSet(initHashCapacity(a.length));
for (int i = from; i < to; i++) {
set.add(a[i]);
}
if (set.size() == to - from) {
return (from == 0 && to == a.length) ? a.clone() : copyOfRange(a, from, to);
} else {
final long[] result = new long[set.size()];
int i = 0;
for (long e : set) {
result[i++] = e;
}
return result;
}
}
}
/**
* Removes the duplicates.
*
* @param a
* @return
*/
public static float[] removeDuplicates(final float[] a) {
if (isNullOrEmpty(a)) {
return EMPTY_FLOAT_ARRAY;
}
return removeDuplicates(a, false);
}
/**
* Removes the duplicates.
*
* @param a
* @param isSorted
* @return
*/
public static float[] removeDuplicates(final float[] a, final boolean isSorted) {
if (isNullOrEmpty(a)) {
return EMPTY_FLOAT_ARRAY;
}
return removeDuplicates(a, 0, a.length, isSorted);
}
/**
* Removes the duplicates.
*
* @param a
* @param from
* @param to
* @param isSorted
* @return
*/
public static float[] removeDuplicates(final float[] a, final int from, final int to, final boolean isSorted) {
checkFromToIndex(from, to, len(a));
if (isNullOrEmpty(a) && from == 0 && to == 0) {
return EMPTY_FLOAT_ARRAY;
} else if (to - from <= 1) {
return copyOfRange(a, from, to);
}
if (isSorted) {
final float[] b = (from == 0 && to == a.length) ? a.clone() : copyOfRange(a, from, to);
int idx = 1;
for (int i = 1, len = b.length; i < len; i++) {
if (equals(b[i], b[i - 1])) {
continue;
}
b[idx++] = b[i];
}
return idx == b.length ? b : copyOfRange(b, 0, idx);
} else {
final Set set = newLinkedHashSet(initHashCapacity(a.length));
for (int i = from; i < to; i++) {
set.add(a[i]);
}
if (set.size() == to - from) {
return (from == 0 && to == a.length) ? a.clone() : copyOfRange(a, from, to);
} else {
final float[] result = new float[set.size()];
int i = 0;
for (float e : set) {
result[i++] = e;
}
return result;
}
}
}
/**
* Removes the duplicates.
*
* @param a
* @return
*/
public static double[] removeDuplicates(final double[] a) {
if (isNullOrEmpty(a)) {
return EMPTY_DOUBLE_ARRAY;
}
return removeDuplicates(a, false);
}
/**
* Removes the duplicates.
*
* @param a
* @param isSorted
* @return
*/
public static double[] removeDuplicates(final double[] a, final boolean isSorted) {
if (isNullOrEmpty(a)) {
return EMPTY_DOUBLE_ARRAY;
}
return removeDuplicates(a, 0, a.length, isSorted);
}
/**
* Removes the duplicates.
*
* @param a
* @param from
* @param to
* @param isSorted
* @return
*/
public static double[] removeDuplicates(final double[] a, final int from, final int to, final boolean isSorted) {
checkFromToIndex(from, to, len(a));
if (isNullOrEmpty(a) && from == 0 && to == 0) {
return EMPTY_DOUBLE_ARRAY;
} else if (to - from <= 1) {
return copyOfRange(a, from, to);
}
if (isSorted) {
final double[] b = (from == 0 && to == a.length) ? a.clone() : copyOfRange(a, from, to);
int idx = 1;
for (int i = 1, len = b.length; i < len; i++) {
if (equals(b[i], b[i - 1])) {
continue;
}
b[idx++] = b[i];
}
return idx == b.length ? b : copyOfRange(b, 0, idx);
} else {
final Set set = newLinkedHashSet(initHashCapacity(a.length));
for (int i = from; i < to; i++) {
set.add(a[i]);
}
if (set.size() == to - from) {
return (from == 0 && to == a.length) ? a.clone() : copyOfRange(a, from, to);
} else {
final double[] result = new double[set.size()];
int i = 0;
for (double e : set) {
result[i++] = e;
}
return result;
}
}
}
/**
*
* Removes all duplicates elements
*
*
*
* N.removeElements(["a", "b", "a"]) = ["a", "b"]
*
*
* @param the component type of the array
* @param a
* @return A new array containing the existing elements except the duplicates
* @throws NullPointerException if the specified array a
is null.
*/
public static T[] removeDuplicates(final T[] a) {
if (isNullOrEmpty(a)) {
return a;
}
return removeDuplicates(a, false);
}
/**
* Removes the duplicates.
*
* @param
* @param a
* @param isSorted
* @return
*/
public static T[] removeDuplicates(final T[] a, final boolean isSorted) {
if (isNullOrEmpty(a)) {
return a;
}
return removeDuplicates(a, 0, a.length, isSorted);
}
/**
* Removes the duplicates.
*
* @param
* @param a
* @param from
* @param to
* @param isSorted
* @return
*/
public static T[] removeDuplicates(final T[] a, final int from, final int to, final boolean isSorted) {
checkFromToIndex(from, to, len(a));
if (isNullOrEmpty(a) && from == 0 && to == 0) {
return a;
} else if (to - from <= 1) {
return copyOfRange(a, from, to);
}
if (isSorted) {
final T[] b = (from == 0 && to == a.length) ? a.clone() : copyOfRange(a, from, to);
int idx = 1;
for (int i = 1, len = b.length; i < len; i++) {
if (equals(b[i], b[i - 1])) {
continue;
}
b[idx++] = b[i];
}
return idx == b.length ? b : copyOfRange(b, 0, idx);
} else {
final List list = distinct(a, from, to);
return list.toArray((T[]) newArray(a.getClass().getComponentType(), list.size()));
}
}
/**
* Removes the duplicates.
*
* @param c
* @return true
if there is one or more duplicated elements are removed. otherwise false
is returned.
*/
public static boolean removeDuplicates(final Collection> c) {
if (isNullOrEmpty(c) || c.size() == 1) {
return false;
}
return removeDuplicates(c, false);
}
/**
* Removes the duplicates.
*
* @param c
* @param isSorted
* @return true
if there is one or more duplicated elements are removed. otherwise false
is returned.
*/
@SuppressWarnings("rawtypes")
public static boolean removeDuplicates(final Collection> c, final boolean isSorted) {
if (isNullOrEmpty(c) || c.size() == 1) {
return false;
}
if (isSorted) {
boolean hasDuplicates = false;
final Iterator> it = c.iterator();
Object pre = it.next();
Object next = null;
while (it.hasNext()) {
next = it.next();
if (equals(next, pre)) {
it.remove();
hasDuplicates = true;
} else {
pre = next;
}
}
return hasDuplicates;
} else {
List> list = distinct(c);
final boolean hasDuplicates = list.size() != c.size();
if (hasDuplicates) {
c.clear();
c.addAll((List) list);
}
return hasDuplicates;
}
}
// Primitive/Object array converters
// ----------------------------------------------------------------------
/**
* Checks for duplicates.
*
* @param a
* @return true, if successful
*/
public static boolean hasDuplicates(final char[] a) {
return hasDuplicates(a, false);
}
/**
* Checks for duplicates.
*
* @param a
* @param isSorted
* @return true, if successful
*/
public static boolean hasDuplicates(final char[] a, final boolean isSorted) {
if (isNullOrEmpty(a)) {
return false;
}
return hasDuplicates(a, 0, a.length, isSorted);
}
/**
* Checks for duplicates.
*
* @param a
* @param fromIndex
* @param toIndex
* @param isSorted
* @return true, if successful
*/
static boolean hasDuplicates(final char[] a, final int fromIndex, final int toIndex, final boolean isSorted) {
checkFromToIndex(fromIndex, toIndex, a.length);
if (isNullOrEmpty(a) || toIndex - fromIndex < 2) {
return false;
} else if (toIndex - fromIndex == 2) {
return a[fromIndex] == a[fromIndex + 1];
} else if (toIndex - fromIndex == 3) {
return a[fromIndex] == a[fromIndex + 1] || a[fromIndex] == a[fromIndex + 2] || a[fromIndex + 1] == a[fromIndex + 2];
}
if (isSorted) {
for (int i = fromIndex + 1; i < toIndex; i++) {
if (a[i] == a[i - 1]) {
return true;
}
}
return false;
} else {
final Set set = newHashSet(initHashCapacity(toIndex - fromIndex));
for (int i = fromIndex; i < toIndex; i++) {
if (set.add(a[i]) == false) {
return true;
}
}
return false;
}
}
/**
* Checks for duplicates.
*
* @param a
* @return true, if successful
*/
public static boolean hasDuplicates(final byte[] a) {
return hasDuplicates(a, false);
}
/**
* Checks for duplicates.
*
* @param a
* @param isSorted
* @return true, if successful
*/
public static boolean hasDuplicates(final byte[] a, final boolean isSorted) {
if (isNullOrEmpty(a)) {
return false;
}
return hasDuplicates(a, 0, a.length, isSorted);
}
/**
* Checks for duplicates.
*
* @param a
* @param fromIndex
* @param toIndex
* @param isSorted
* @return true, if successful
*/
static boolean hasDuplicates(final byte[] a, final int fromIndex, final int toIndex, final boolean isSorted) {
checkFromToIndex(fromIndex, toIndex, a.length);
if (isNullOrEmpty(a) || toIndex - fromIndex < 2) {
return false;
} else if (toIndex - fromIndex == 2) {
return a[fromIndex] == a[fromIndex + 1];
} else if (toIndex - fromIndex == 3) {
return a[fromIndex] == a[fromIndex + 1] || a[fromIndex] == a[fromIndex + 2] || a[fromIndex + 1] == a[fromIndex + 2];
}
if (isSorted) {
for (int i = fromIndex + 1; i < toIndex; i++) {
if (a[i] == a[i - 1]) {
return true;
}
}
return false;
} else {
final Set set = newHashSet(initHashCapacity(toIndex - fromIndex));
for (int i = fromIndex; i < toIndex; i++) {
if (set.add(a[i]) == false) {
return true;
}
}
return false;
}
}
/**
* Checks for duplicates.
*
* @param a
* @return true, if successful
*/
public static boolean hasDuplicates(final short[] a) {
return hasDuplicates(a, false);
}
/**
* Checks for duplicates.
*
* @param a
* @param isSorted
* @return true, if successful
*/
public static boolean hasDuplicates(final short[] a, final boolean isSorted) {
if (isNullOrEmpty(a)) {
return false;
}
return hasDuplicates(a, 0, a.length, isSorted);
}
/**
* Checks for duplicates.
*
* @param a
* @param fromIndex
* @param toIndex
* @param isSorted
* @return true, if successful
*/
static boolean hasDuplicates(final short[] a, final int fromIndex, final int toIndex, final boolean isSorted) {
checkFromToIndex(fromIndex, toIndex, a.length);
if (isNullOrEmpty(a) || toIndex - fromIndex < 2) {
return false;
} else if (toIndex - fromIndex == 2) {
return a[fromIndex] == a[fromIndex + 1];
} else if (toIndex - fromIndex == 3) {
return a[fromIndex] == a[fromIndex + 1] || a[fromIndex] == a[fromIndex + 2] || a[fromIndex + 1] == a[fromIndex + 2];
}
if (isSorted) {
for (int i = fromIndex + 1; i < toIndex; i++) {
if (a[i] == a[i - 1]) {
return true;
}
}
return false;
} else {
final Set set = newHashSet(initHashCapacity(toIndex - fromIndex));
for (int i = fromIndex; i < toIndex; i++) {
if (set.add(a[i]) == false) {
return true;
}
}
return false;
}
}
/**
* Checks for duplicates.
*
* @param a
* @return true, if successful
*/
public static boolean hasDuplicates(final int[] a) {
return hasDuplicates(a, false);
}
/**
* Checks for duplicates.
*
* @param a
* @param isSorted
* @return true, if successful
*/
public static boolean hasDuplicates(final int[] a, final boolean isSorted) {
if (isNullOrEmpty(a)) {
return false;
}
return hasDuplicates(a, 0, a.length, isSorted);
}
/**
* Checks for duplicates.
*
* @param a
* @param fromIndex
* @param toIndex
* @param isSorted
* @return true, if successful
*/
static boolean hasDuplicates(final int[] a, final int fromIndex, final int toIndex, final boolean isSorted) {
checkFromToIndex(fromIndex, toIndex, a.length);
if (isNullOrEmpty(a) || toIndex - fromIndex < 2) {
return false;
} else if (toIndex - fromIndex == 2) {
return a[fromIndex] == a[fromIndex + 1];
} else if (toIndex - fromIndex == 3) {
return a[fromIndex] == a[fromIndex + 1] || a[fromIndex] == a[fromIndex + 2] || a[fromIndex + 1] == a[fromIndex + 2];
}
if (isSorted) {
for (int i = fromIndex + 1; i < toIndex; i++) {
if (a[i] == a[i - 1]) {
return true;
}
}
return false;
} else {
final Set set = newHashSet(initHashCapacity(toIndex - fromIndex));
for (int i = fromIndex; i < toIndex; i++) {
if (set.add(a[i]) == false) {
return true;
}
}
return false;
}
}
/**
* Checks for duplicates.
*
* @param a
* @return true, if successful
*/
public static boolean hasDuplicates(final long[] a) {
return hasDuplicates(a, false);
}
/**
* Checks for duplicates.
*
* @param a
* @param isSorted
* @return true, if successful
*/
public static boolean hasDuplicates(final long[] a, final boolean isSorted) {
if (isNullOrEmpty(a)) {
return false;
}
return hasDuplicates(a, 0, a.length, isSorted);
}
/**
* Checks for duplicates.
*
* @param a
* @param fromIndex
* @param toIndex
* @param isSorted
* @return true, if successful
*/
static boolean hasDuplicates(final long[] a, final int fromIndex, final int toIndex, final boolean isSorted) {
checkFromToIndex(fromIndex, toIndex, a.length);
if (isNullOrEmpty(a) || toIndex - fromIndex < 2) {
return false;
} else if (toIndex - fromIndex == 2) {
return a[fromIndex] == a[fromIndex + 1];
} else if (toIndex - fromIndex == 3) {
return a[fromIndex] == a[fromIndex + 1] || a[fromIndex] == a[fromIndex + 2] || a[fromIndex + 1] == a[fromIndex + 2];
}
if (isSorted) {
for (int i = fromIndex + 1; i < toIndex; i++) {
if (a[i] == a[i - 1]) {
return true;
}
}
return false;
} else {
final Set set = newHashSet(initHashCapacity(toIndex - fromIndex));
for (int i = fromIndex; i < toIndex; i++) {
if (set.add(a[i]) == false) {
return true;
}
}
return false;
}
}
/**
* Checks for duplicates.
*
* @param a
* @return true, if successful
*/
public static boolean hasDuplicates(final float[] a) {
return hasDuplicates(a, false);
}
/**
* Checks for duplicates.
*
* @param a
* @param isSorted
* @return true, if successful
*/
public static boolean hasDuplicates(final float[] a, final boolean isSorted) {
if (isNullOrEmpty(a)) {
return false;
}
return hasDuplicates(a, 0, a.length, isSorted);
}
/**
* Checks for duplicates.
*
* @param a
* @param fromIndex
* @param toIndex
* @param isSorted
* @return true, if successful
*/
static boolean hasDuplicates(final float[] a, final int fromIndex, final int toIndex, final boolean isSorted) {
checkFromToIndex(fromIndex, toIndex, a.length);
if (isNullOrEmpty(a) || toIndex - fromIndex < 2) {
return false;
} else if (toIndex - fromIndex == 2) {
return equals(a[fromIndex], a[fromIndex + 1]);
} else if (toIndex - fromIndex == 3) {
return equals(a[fromIndex], a[fromIndex + 1]) || equals(a[fromIndex], a[fromIndex + 2]) || equals(a[fromIndex + 1], a[fromIndex + 2]);
}
if (isSorted) {
for (int i = fromIndex + 1; i < toIndex; i++) {
if (equals(a[i], a[i - 1])) {
return true;
}
}
return false;
} else {
final Set set = newHashSet(initHashCapacity(toIndex - fromIndex));
for (int i = fromIndex; i < toIndex; i++) {
if (set.add(a[i]) == false) {
return true;
}
}
return false;
}
}
/**
* Checks for duplicates.
*
* @param a
* @return true, if successful
*/
public static boolean hasDuplicates(final double[] a) {
return hasDuplicates(a, false);
}
/**
* Checks for duplicates.
*
* @param a
* @param isSorted
* @return true, if successful
*/
public static boolean hasDuplicates(final double[] a, final boolean isSorted) {
if (isNullOrEmpty(a)) {
return false;
}
return hasDuplicates(a, 0, a.length, isSorted);
}
/**
* Checks for duplicates.
*
* @param a
* @param fromIndex
* @param toIndex
* @param isSorted
* @return true, if successful
*/
static boolean hasDuplicates(final double[] a, final int fromIndex, final int toIndex, final boolean isSorted) {
checkFromToIndex(fromIndex, toIndex, a.length);
if (isNullOrEmpty(a) || toIndex - fromIndex < 2) {
return false;
} else if (toIndex - fromIndex == 2) {
return equals(a[fromIndex], a[fromIndex + 1]);
} else if (toIndex - fromIndex == 3) {
return equals(a[fromIndex], a[fromIndex + 1]) || equals(a[fromIndex], a[fromIndex + 2]) || equals(a[fromIndex + 1], a[fromIndex + 2]);
}
if (isSorted) {
for (int i = fromIndex + 1; i < toIndex; i++) {
if (equals(a[i], a[i - 1])) {
return true;
}
}
return false;
} else {
final Set set = newHashSet(initHashCapacity(toIndex - fromIndex));
for (int i = fromIndex; i < toIndex; i++) {
if (set.add(a[i]) == false) {
return true;
}
}
return false;
}
}
/**
* Checks for duplicates.
*
* @param
* @param a
* @return true, if successful
*/
public static boolean hasDuplicates(final T[] a) {
return hasDuplicates(a, false);
}
/**
* Checks for duplicates.
*
* @param
* @param a
* @param isSorted
* @return true, if successful
*/
public static boolean hasDuplicates(final T[] a, final boolean isSorted) {
if (isNullOrEmpty(a)) {
return false;
}
return hasDuplicates(a, 0, a.length, isSorted);
}
/**
* Checks for duplicates.
*
* @param
* @param a
* @param fromIndex
* @param toIndex
* @param isSorted
* @return true, if successful
*/
static boolean hasDuplicates(final T[] a, final int fromIndex, final int toIndex, final boolean isSorted) {
checkFromToIndex(fromIndex, toIndex, a.length);
if (isNullOrEmpty(a) || toIndex - fromIndex < 2) {
return false;
} else if (toIndex - fromIndex == 2) {
return equals(a[fromIndex], a[fromIndex + 1]);
} else if (toIndex - fromIndex == 3) {
return equals(a[fromIndex], a[fromIndex + 1]) || equals(a[fromIndex], a[fromIndex + 2]) || equals(a[fromIndex + 1], a[fromIndex + 2]);
}
if (isSorted) {
for (int i = fromIndex + 1; i < toIndex; i++) {
if (equals(a[i], a[i - 1])) {
return true;
}
}
return false;
} else {
final Set set = newHashSet(initHashCapacity(toIndex - fromIndex));
for (int i = fromIndex; i < toIndex; i++) {
if (set.add(hashKey(a[i])) == false) {
return true;
}
}
return false;
}
}
/**
* Checks for duplicates.
*
* @param c
* @return true, if successful
*/
public static boolean hasDuplicates(final Collection> c) {
return hasDuplicates(c, false);
}
/**
* Checks for duplicates.
*
* @param c
* @param isSorted
* @return true, if successful
*/
public static boolean hasDuplicates(final Collection> c, final boolean isSorted) {
if (isNullOrEmpty(c) || c.size() == 1) {
return false;
}
if (isSorted) {
final Iterator> it = c.iterator();
Object pre = it.next();
Object next = null;
while (it.hasNext()) {
next = it.next();
if (equals(next, pre)) {
return true;
}
pre = next;
}
return false;
} else {
final Set set = newHashSet(initHashCapacity(c.size()));
for (Object e : c) {
if (set.add(hashKey(e)) == false) {
return true;
}
}
return false;
}
}
/**
*
* @param c
* @param objsToKeep
* @return true, if successful
*/
public static boolean retainAll(final Collection c, final Collection extends T> objsToKeep) {
if (N.isNullOrEmpty(c)) {
return false;
} else if (N.isNullOrEmpty(objsToKeep)) {
c.clear();
return true;
}
if (c instanceof HashSet && !(objsToKeep instanceof Set) && (c.size() > 9 || objsToKeep.size() > 9)) {
return c.retainAll(N.newHashSet(objsToKeep));
} else {
return c.retainAll(objsToKeep);
}
}
/**
*
* @param obj
* @return
*/
static Object hashKey(Object obj) {
return obj == null || obj.getClass().isArray() == false ? obj : Wrapper.of(obj);
}
/**
*
* @param a
* @return a long number
*/
@SafeVarargs
public static int sum(final char... a) {
if (isNullOrEmpty(a)) {
return 0;
}
return sum(a, 0, a.length);
}
/**
*
* @param a
* @param from
* @param to
* @return
*/
public static int sum(final char[] a, final int from, final int to) {
checkFromToIndex(from, to, len(a));
if (isNullOrEmpty(a)) {
if (to > 0) {
throw new IndexOutOfBoundsException();
}
return 0;
}
int sum = 0;
for (int i = from; i < to; i++) {
sum += a[i];
}
return sum;
}
/**
*
* @param a
* @return a long number
*/
@SafeVarargs
public static int sum(final byte... a) {
if (isNullOrEmpty(a)) {
return 0;
}
return sum(a, 0, a.length);
}
/**
*
* @param a
* @param from
* @param to
* @return
*/
public static int sum(final byte[] a, final int from, final int to) {
checkFromToIndex(from, to, len(a));
if (isNullOrEmpty(a)) {
if (to > 0) {
throw new IndexOutOfBoundsException();
}
return 0;
}
int sum = 0;
for (int i = from; i < to; i++) {
sum += a[i];
}
return sum;
}
/**
*
* @param a
* @return a long number
*/
@SafeVarargs
public static int sum(final short... a) {
if (isNullOrEmpty(a)) {
return 0;
}
return sum(a, 0, a.length);
}
/**
*
* @param a
* @param from
* @param to
* @return
*/
public static int sum(final short[] a, final int from, final int to) {
checkFromToIndex(from, to, len(a));
if (isNullOrEmpty(a)) {
if (to > 0) {
throw new IndexOutOfBoundsException();
}
return 0;
}
int sum = 0;
for (int i = from; i < to; i++) {
sum += a[i];
}
return sum;
}
/**
*
* @param a
* @return a long number
*/
@SafeVarargs
public static int sum(final int... a) {
if (isNullOrEmpty(a)) {
return 0;
}
return sum(a, 0, a.length);
}
/**
*
* @param a
* @param from
* @param to
* @return
*/
public static int sum(final int[] a, final int from, final int to) {
checkFromToIndex(from, to, len(a));
if (isNullOrEmpty(a)) {
if (to > 0) {
throw new IndexOutOfBoundsException();
}
return 0;
}
int sum = 0;
for (int i = from; i < to; i++) {
sum += a[i];
}
return sum;
}
/**
*
* @param a
* @return a long number
*/
@SafeVarargs
public static long sum(final long... a) {
if (isNullOrEmpty(a)) {
return 0L;
}
return sum(a, 0, a.length);
}
/**
*
* @param a
* @param from
* @param to
* @return
*/
public static long sum(final long[] a, final int from, final int to) {
checkFromToIndex(from, to, len(a));
if (isNullOrEmpty(a)) {
if (to > 0) {
throw new IndexOutOfBoundsException();
}
return 0L;
}
long sum = 0;
for (int i = from; i < to; i++) {
sum += a[i];
}
return sum;
}
/**
*
* @param a
* @return a double number
*/
@SafeVarargs
public static float sum(final float... a) {
if (isNullOrEmpty(a)) {
return 0f;
}
return sum(a, 0, a.length);
}
/**
*
* @param a
* @param from
* @param to
* @return
*/
public static float sum(final float[] a, final int from, final int to) {
checkFromToIndex(from, to, len(a));
if (isNullOrEmpty(a)) {
if (to > 0) {
throw new IndexOutOfBoundsException();
}
return 0f;
}
final KahanSummation summation = new KahanSummation();
for (int i = from; i < to; i++) {
summation.add(a[i]);
}
return (float) summation.sum();
}
/**
*
* @param a
* @return a double number
*/
@SafeVarargs
public static double sum(final double... a) {
if (isNullOrEmpty(a)) {
return 0d;
}
return sum(a, 0, a.length);
}
/**
*
* @param a
* @param from
* @param to
* @return
*/
public static double sum(final double[] a, final int from, final int to) {
checkFromToIndex(from, to, len(a));
if (isNullOrEmpty(a)) {
if (to > 0) {
throw new IndexOutOfBoundsException();
}
return 0d;
}
final KahanSummation summation = new KahanSummation();
for (int i = from; i < to; i++) {
summation.add(a[i]);
}
return summation.sum();
}
/**
*
* @param a
* @return a double number
*/
@SafeVarargs
public static double average(final char... a) {
if (isNullOrEmpty(a)) {
return 0d;
}
return average(a, 0, a.length);
}
/**
*
* @param a
* @param from
* @param to
* @return
*/
public static double average(final char[] a, final int from, final int to) {
checkFromToIndex(from, to, len(a));
if (isNullOrEmpty(a)) {
if (to > 0) {
throw new IndexOutOfBoundsException();
}
return 0d;
} else if (from == to) {
return 0d;
}
return ((double) sum(a, from, to)) / (to - from);
}
/**
*
* @param a
* @return a double number
*/
@SafeVarargs
public static double average(final byte... a) {
if (isNullOrEmpty(a)) {
return 0d;
}
return average(a, 0, a.length);
}
/**
*
* @param a
* @param from
* @param to
* @return
*/
public static double average(final byte[] a, final int from, final int to) {
checkFromToIndex(from, to, len(a));
if (isNullOrEmpty(a)) {
if (to > 0) {
throw new IndexOutOfBoundsException();
}
return 0d;
} else if (from == to) {
return 0d;
}
return ((double) sum(a, from, to)) / (to - from);
}
/**
*
* @param a
* @return a double number
*/
@SafeVarargs
public static double average(final short... a) {
if (isNullOrEmpty(a)) {
return 0d;
}
return average(a, 0, a.length);
}
/**
*
* @param a
* @param from
* @param to
* @return
*/
public static double average(final short[] a, final int from, final int to) {
checkFromToIndex(from, to, len(a));
if (isNullOrEmpty(a)) {
if (to > 0) {
throw new IndexOutOfBoundsException();
}
return 0d;
} else if (from == to) {
return 0d;
}
return ((double) sum(a, from, to)) / (to - from);
}
/**
*
* @param a
* @return a double number
*/
@SafeVarargs
public static double average(final int... a) {
if (isNullOrEmpty(a)) {
return 0d;
}
return average(a, 0, a.length);
}
/**
*
* @param a
* @param from
* @param to
* @return
*/
public static double average(final int[] a, final int from, final int to) {
checkFromToIndex(from, to, len(a));
if (isNullOrEmpty(a)) {
if (to > 0) {
throw new IndexOutOfBoundsException();
}
return 0d;
} else if (from == to) {
return 0d;
}
return ((double) sum(a, from, to)) / (to - from);
}
/**
*
* @param a
* @return a double number
*/
@SafeVarargs
public static double average(final long... a) {
if (isNullOrEmpty(a)) {
return 0d;
}
return average(a, 0, a.length);
}
/**
*
* @param a
* @param from
* @param to
* @return
*/
public static double average(final long[] a, final int from, final int to) {
checkFromToIndex(from, to, len(a));
if (isNullOrEmpty(a)) {
if (to > 0) {
throw new IndexOutOfBoundsException();
}
return 0d;
} else if (from == to) {
return 0d;
}
return ((double) sum(a, from, to)) / (to - from);
}
/**
*
* @param a
* @return a double number
*/
@SafeVarargs
public static double average(final float... a) {
if (isNullOrEmpty(a)) {
return 0d;
}
return average(a, 0, a.length);
}
/**
*
* @param a
* @param from
* @param to
* @return
*/
public static double average(final float[] a, final int from, final int to) {
checkFromToIndex(from, to, len(a));
if (isNullOrEmpty(a)) {
if (to > 0) {
throw new IndexOutOfBoundsException();
}
return 0d;
} else if (from == to) {
return 0d;
}
final KahanSummation summation = new KahanSummation();
for (int i = from; i < to; i++) {
summation.add(a[i]);
}
return summation.average().orZero();
}
/**
*
* @param a
* @return a double number
*/
@SafeVarargs
public static double average(final double... a) {
if (isNullOrEmpty(a)) {
return 0d;
}
return average(a, 0, a.length);
}
/**
*
* @param a
* @param from
* @param to
* @return
*/
public static double average(final double[] a, final int from, final int to) {
checkFromToIndex(from, to, len(a));
if (isNullOrEmpty(a)) {
if (to > 0) {
throw new IndexOutOfBoundsException();
}
return 0d;
} else if (from == to) {
return 0d;
}
final KahanSummation summation = new KahanSummation();
for (int i = from; i < to; i++) {
summation.add(a[i]);
}
return summation.average().orZero();
}
/**
*
* Gets the minimum of two char
values.
*
*
* @param a
* @param b
* @return
*/
public static char min(final char a, final char b) {
return (a <= b) ? a : b;
}
/**
*
* Gets the minimum of two byte
values.
*
*
* @param a
* @param b
* @return
*/
public static byte min(final byte a, final byte b) {
return (a <= b) ? a : b;
}
/**
*
* Gets the minimum of two short
values.
*
*
* @param a
* @param b
* @return
*/
public static short min(final short a, final short b) {
return (a <= b) ? a : b;
}
/**
*
* Gets the minimum of two int
values.
*
*
* @param a
* @param b
* @return
*/
public static int min(final int a, final int b) {
return (a <= b) ? a : b;
}
/**
*
* Gets the minimum of two long
values.
*
*
* @param a
* @param b
* @return
*/
public static long min(final long a, final long b) {
return (a <= b) ? a : b;
}
/**
*
* Gets the minimum of two float
values.
*
*
* @param a
* @param b
* @return
*/
public static float min(final float a, final float b) {
return Math.min(a, b);
}
/**
*
* Gets the minimum of two double
values.
*
*
* @param a
* @param b
* @return
*/
public static double min(final double a, final double b) {
return Math.min(a, b);
}
/**
*
* @param
* @param a
* @param b
* @return
*/
public static > T min(final T a, final T b) {
return (T) min(a, b, NULL_MAX_COMPARATOR);
}
/**
*
* @param
* @param a
* @param b
* @param cmp
* @return
*/
public static T min(final T a, final T b, final Comparator super T> cmp) {
return (cmp == null ? NULL_MAX_COMPARATOR : cmp).compare(a, b) <= 0 ? a : b;
}
/**
*
* Gets the minimum of three char
values.
*
*
* @param a
* @param b
* @param c
* @return
*/
public static char min(final char a, final char b, final char c) {
final char m = (a <= b) ? a : b;
return (m <= c) ? m : c;
}
/**
*
* Gets the minimum of three byte
values.
*
*
* @param a
* @param b
* @param c
* @return
*/
public static byte min(final byte a, final byte b, final byte c) {
final byte m = (a <= b) ? a : b;
return (m <= c) ? m : c;
}
/**
*
* Gets the minimum of three short
values.
*
*
* @param a
* @param b
* @param c
* @return
*/
public static short min(final short a, final short b, final short c) {
final short m = (a <= b) ? a : b;
return (m <= c) ? m : c;
}
/**
*
* Gets the minimum of three int
values.
*
*
* @param a
* @param b
* @param c
* @return
*/
public static int min(final int a, final int b, final int c) {
final int m = (a <= b) ? a : b;
return (m <= c) ? m : c;
}
/**
*
* Gets the minimum of three long
values.
*
*
* @param a
* @param b
* @param c
* @return
*/
public static long min(final long a, final long b, final long c) {
final long m = (a <= b) ? a : b;
return (m <= c) ? m : c;
}
/**
*
* Gets the minimum of three float
values.
*
*
* @param a
* @param b
* @param c
* @return
*/
public static float min(final float a, final float b, final float c) {
return Math.min(Math.min(a, b), c);
}
/**
*
* Gets the minimum of three double
values.
*
*
* @param a
* @param b
* @param c
* @return
*/
public static double min(final double a, final double b, final double c) {
return Math.min(Math.min(a, b), c);
}
/**
*
* @param
* @param a
* @param b
* @param c
* @return
*/
public static > T min(final T a, final T b, final T c) {
return (T) min(a, b, c, NULL_MAX_COMPARATOR);
}
/**
*
* @param
* @param a
* @param b
* @param c
* @param cmp
* @return
*/
public static T min(final T a, final T b, final T c, final Comparator super T> cmp) {
return min(min(a, b, cmp), c, cmp);
}
/**
*
* Returns the minimum value in an array.
*
*
* @param a
* an array, must not be null or empty
* @return
*/
@SafeVarargs
public static char min(final char... a) {
checkArgNotNullOrEmpty(a, "The spcified array can not be null or empty");
if (isNullOrEmpty(a)) {
throw new IllegalArgumentException("The spcified array can not be null or empty");
}
return min(a, 0, a.length);
}
/**
*
* @param a
* @param from
* @param to
* @return
*/
public static char min(final char[] a, final int from, final int to) {
if (isNullOrEmpty(a) || to - from < 1) {
throw new IllegalArgumentException("The spcified array can not be null or empty");
}
char min = a[from];
for (int i = from + 1; i < to; i++) {
if (a[i] < min) {
min = a[i];
}
}
return min;
}
/**
*
* Returns the minimum value in an array.
*
*
* @param a
* an array, must not be null or empty
* @return
*/
@SafeVarargs
public static byte min(final byte... a) {
checkArgNotNullOrEmpty(a, "The spcified array can not be null or empty");
return min(a, 0, a.length);
}
/**
*
* @param a
* @param from
* @param to
* @return
*/
public static byte min(final byte[] a, final int from, final int to) {
if (isNullOrEmpty(a) || to - from < 1) {
throw new IllegalArgumentException("The spcified array can not be null or empty");
}
byte min = a[from];
for (int i = from + 1; i < to; i++) {
if (a[i] < min) {
min = a[i];
}
}
return min;
}
/**
*
* Returns the minimum value in an array.
*
*
* @param a
* an array, must not be null or empty
* @return
*/
@SafeVarargs
public static short min(final short... a) {
checkArgNotNullOrEmpty(a, "The spcified array can not be null or empty");
return min(a, 0, a.length);
}
/**
*
* @param a
* @param from
* @param to
* @return
*/
public static short min(final short[] a, final int from, final int to) {
if (isNullOrEmpty(a) || to - from < 1) {
throw new IllegalArgumentException("The spcified array can not be null or empty");
}
short min = a[from];
for (int i = from + 1; i < to; i++) {
if (a[i] < min) {
min = a[i];
}
}
return min;
}
/**
*
* Returns the minimum value in an array.
*
*
* @param a
* an array, must not be null or empty
* @return
*/
@SafeVarargs
public static int min(final int... a) {
checkArgNotNullOrEmpty(a, "The spcified array can not be null or empty");
return min(a, 0, a.length);
}
/**
*
* @param a
* @param from
* @param to
* @return
*/
public static int min(final int[] a, final int from, final int to) {
if (isNullOrEmpty(a) || to - from < 1) {
throw new IllegalArgumentException("The spcified array can not be null or empty");
}
int min = a[from];
for (int i = from + 1; i < to; i++) {
if (a[i] < min) {
min = a[i];
}
}
return min;
}
// Min in array
// --------------------------------------------------------------------
/**
*
* Returns the minimum value in an array.
*
*
* @param a
* an array, must not be null or empty
* @return
*/
@SafeVarargs
public static long min(final long... a) {
checkArgNotNullOrEmpty(a, "The spcified array can not be null or empty");
return min(a, 0, a.length);
}
/**
*
* @param a
* @param from
* @param to
* @return
*/
public static long min(final long[] a, final int from, final int to) {
if (isNullOrEmpty(a) || to - from < 1) {
throw new IllegalArgumentException("The spcified array can not be null or empty");
}
long min = a[from];
for (int i = from + 1; i < to; i++) {
if (a[i] < min) {
min = a[i];
}
}
return min;
}
/**
*
* Returns the minimum value in an array.
*
*
* @param a
* an array, must not be null or empty
* @return
* @see IEEE754rUtil#min(float[]) IEEE754rUtils for a version of this method
* that handles NaN differently
*/
@SafeVarargs
public static float min(final float... a) {
checkArgNotNullOrEmpty(a, "The spcified array can not be null or empty");
return min(a, 0, a.length);
}
/**
*
* @param a
* @param from
* @param to
* @return
*/
public static float min(final float[] a, final int from, final int to) {
if (isNullOrEmpty(a) || to - from < 1) {
throw new IllegalArgumentException("The spcified array can not be null or empty");
}
float min = a[from];
for (int i = from + 1; i < to; i++) {
min = Math.min(min, a[i]);
if (Float.isNaN(min)) {
return min;
}
}
return min;
}
/**
*
* Returns the minimum value in an array.
*
*
* @param a
* an array, must not be null or empty
* @return
* @see IEEE754rUtil#min(double[]) IEEE754rUtils for a version of this
* method that handles NaN differently
*/
@SafeVarargs
public static double min(final double... a) {
checkArgNotNullOrEmpty(a, "The spcified array can not be null or empty");
return min(a, 0, a.length);
}
/**
*
* @param a
* @param from
* @param to
* @return
*/
public static double min(final double[] a, final int from, final int to) {
if (isNullOrEmpty(a) || to - from < 1) {
throw new IllegalArgumentException("The spcified array can not be null or empty");
}
double min = a[from];
for (int i = from + 1; i < to; i++) {
min = Math.min(min, a[i]);
if (Double.isNaN(min)) {
return min;
}
}
return min;
}
/**
* Returns the minimum element in the array.
*
* @param
* @param a an array, must not be null or empty
* @return
*/
public static > T min(final T[] a) {
checkArgNotNullOrEmpty(a, "The spcified array can not be null or empty");
return min(a, 0, a.length);
}
/**
*
* @param
* @param a
* @param from
* @param to
* @return
*/
public static > T min(final T[] a, final int from, final int to) {
return (T) min(a, from, to, NULL_MAX_COMPARATOR);
}
/**
* Returns the minimum element in the array.
*
* @param
* @param a an array, must not be null or empty
* @param cmp
* @return
*/
public static T min(final T[] a, final Comparator super T> cmp) {
checkArgNotNullOrEmpty(a, "The spcified array can not be null or empty");
return min(a, 0, a.length, cmp);
}
/**
*
* @param
* @param a
* @param from
* @param to
* @param cmp
* @return
*/
public static T min(final T[] a, final int from, final int to, Comparator super T> cmp) {
if (isNullOrEmpty(a) || to - from < 1) {
throw new IllegalArgumentException("The spcified array can not be null or empty");
}
cmp = cmp == null ? NULL_MAX_COMPARATOR : cmp;
T candidate = a[from];
for (int i = from + 1; i < to; i++) {
if (cmp.compare(a[i], candidate) < 0) {
candidate = a[i];
}
if (candidate == null && cmp == NULL_MIN_COMPARATOR) {
return null;
}
}
return candidate;
}
/**
*
* @param
* @param c
* @return
*/
public static > T min(final Collection extends T> c) {
checkArgNotNullOrEmpty(c, "The spcified collection can not be null or empty");
return min(c, 0, c.size());
}
/**
*
* @param
* @param c
* @param from
* @param to
* @return
*/
public static > T min(final Collection extends T> c, final int from, final int to) {
checkArgNotNullOrEmpty(c, "The spcified collection can not be null or empty");
return (T) min(c, from, to, NULL_MAX_COMPARATOR);
}
/**
*
* @param
* @param c
* @param cmp
* @return
*/
public static T min(final Collection extends T> c, Comparator super T> cmp) {
checkArgNotNullOrEmpty(c, "The spcified collection can not be null or empty");
return min(c, 0, c.size(), cmp);
}
/**
* Returns the minimum element in the collection.
*
* @param
* @param c
* @param from
* @param to
* @param cmp
* @return
*/
public static T min(final Collection extends T> c, final int from, final int to, Comparator super T> cmp) {
checkFromToIndex(from, to, size(c));
if (isNullOrEmpty(c) || to - from < 1 || from >= c.size()) {
throw new IllegalArgumentException("The size of collection can not be null or empty");
}
cmp = cmp == null ? NULL_MAX_COMPARATOR : cmp;
T candidate = null;
T e = null;
if (c instanceof List && c instanceof RandomAccess) {
final List list = (List) c;
candidate = list.get(from);
for (int i = from + 1; i < to; i++) {
e = list.get(i);
if (cmp.compare(e, candidate) < 0) {
candidate = e;
}
if (candidate == null && cmp == NULL_MIN_COMPARATOR) {
return null;
}
}
} else {
final Iterator extends T> it = c.iterator();
for (int i = 0; i < to; i++) {
if (i < from) {
it.next();
continue;
} else if (i == from) {
candidate = it.next();
} else {
e = it.next();
if (cmp.compare(e, candidate) < 0) {
candidate = e;
}
if (candidate == null && cmp == NULL_MIN_COMPARATOR) {
return null;
}
}
}
}
return candidate;
}
/**
*
* @param
* @param a
* @return
*/
public static > List minAll(final T[] a) {
return minAll(a, NULL_MAX_COMPARATOR);
}
/**
*
* @param
* @param a
* @param cmp
* @return
*/
public static List minAll(final T[] a, Comparator super T> cmp) {
if (isNullOrEmpty(a)) {
return new ArrayList<>();
}
cmp = cmp == null ? NULL_MAX_COMPARATOR : cmp;
final List result = new ArrayList<>();
T candicate = a[0];
int cp = 0;
result.add(candicate);
for (int i = 1, len = a.length; i < len; i++) {
cp = cmp.compare(a[i], candicate);
if (cp == 0) {
result.add(a[i]);
} else if (cp < 0) {
result.clear();
result.add(a[i]);
candicate = a[i];
}
}
return result;
}
/**
*
* @param
* @param c
* @return
*/
public static > List minAll(final Collection