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.u.Nullable;
import com.landawn.abacus.util.u.OptionalDouble;
import com.landawn.abacus.util.function.IntFunction;
import com.landawn.abacus.util.function.Supplier;
/**
*
* 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
*/
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, 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;
}
/**
* Returns a new {@code List} with specified {@code objToExclude} excluded.
*
* @param
* @param c
* @param objToExclude
* @return a new {@code List}
*/
public static List exclude(final Collection c, final Object objToExclude) {
if (N.isNullOrEmpty(c)) {
return new ArrayList<>();
}
final List result = new ArrayList<>(c.size() - 1);
for (T e : c) {
if (!N.equals(e, objToExclude)) {
result.add(e);
}
}
return result;
}
/**
* Returns a new {@code Set} with specified {@code objToExclude} excluded.
*
* @param
* @param c
* @param objToExclude
* @return a new {@code Set}
*/
public static Set excludeToSet(final Collection c, final Object objToExclude) {
if (N.isNullOrEmpty(c)) {
return new HashSet<>();
}
final Set result = new HashSet<>(c.size() - 1);
for (T e : c) {
if (!N.equals(e, objToExclude)) {
result.add(e);
}
}
return result;
}
/**
* Returns a new {@code List} with specified {@code objsToExclude} excluded.
*
* @param c
* @param objsToExclude
* @return a new {@code List}
*/
public static List excludeAll(final Collection c, final Collection> objsToExclude) {
if (N.isNullOrEmpty(c)) {
return new ArrayList<>();
} else if (N.isNullOrEmpty(objsToExclude)) {
return new ArrayList<>(c);
} else if (objsToExclude.size() == 1) {
return exclude(c, N.firstOrNullIfEmpty(objsToExclude));
}
final Set set = objsToExclude instanceof Set ? ((Set) objsToExclude) : new HashSet(objsToExclude);
final List result = new ArrayList<>(N.max(0, c.size() - set.size()));
for (T e : c) {
if (!set.contains(e)) {
result.add(e);
}
}
return result;
}
/**
* Returns a new {@code Set} with specified {@code objsToExclude} excluded.
*
* @param c
* @param objsToExclude
* @return a new {@code Set}
*/
public static Set excludeAllToSet(final Collection c, final Collection> objsToExclude) {
if (N.isNullOrEmpty(c)) {
return new HashSet<>();
} else if (N.isNullOrEmpty(objsToExclude)) {
return new HashSet<>(c);
} else if (objsToExclude.size() == 1) {
return excludeToSet(c, N.firstOrNullIfEmpty(objsToExclude));
}
final Set set = objsToExclude instanceof Set ? ((Set) objsToExclude) : new HashSet(objsToExclude);
final Set result = new HashSet<>(N.max(0, c.size() - set.size()));
for (T e : c) {
if (!set.contains(e)) {
result.add(e);
}
}
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 IllegalArgumentException if the specified {@code Array} is null
.
*/
@SafeVarargs
public static T[] concat(final T[]... aa) throws IllegalArgumentException {
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;
}
public static int replaceIf(final boolean[] a, final Throwables.BooleanPredicate predicate, final boolean newValue) throws E {
if (N.isNullOrEmpty(a)) {
return 0;
}
int result = 0;
for (int i = 0, n = a.length; i < n; i++) {
if (predicate.test(a[i])) {
a[i] = newValue;
result++;
}
}
return result;
}
public static int replaceIf(final char[] a, final Throwables.CharPredicate predicate, final char newValue) throws E {
if (N.isNullOrEmpty(a)) {
return 0;
}
int result = 0;
for (int i = 0, n = a.length; i < n; i++) {
if (predicate.test(a[i])) {
a[i] = newValue;
result++;
}
}
return result;
}
public static int replaceIf(final byte[] a, final Throwables.BytePredicate predicate, final byte newValue) throws E {
if (N.isNullOrEmpty(a)) {
return 0;
}
int result = 0;
for (int i = 0, n = a.length; i < n; i++) {
if (predicate.test(a[i])) {
a[i] = newValue;
result++;
}
}
return result;
}
public static int replaceIf(final short[] a, final Throwables.ShortPredicate predicate, final short newValue) throws E {
if (N.isNullOrEmpty(a)) {
return 0;
}
int result = 0;
for (int i = 0, n = a.length; i < n; i++) {
if (predicate.test(a[i])) {
a[i] = newValue;
result++;
}
}
return result;
}
public static int replaceIf(final int[] a, final Throwables.IntPredicate predicate, final int newValue) throws E {
if (N.isNullOrEmpty(a)) {
return 0;
}
int result = 0;
for (int i = 0, n = a.length; i < n; i++) {
if (predicate.test(a[i])) {
a[i] = newValue;
result++;
}
}
return result;
}
public static int replaceIf(final long[] a, final Throwables.LongPredicate predicate, final long newValue) throws E {
if (N.isNullOrEmpty(a)) {
return 0;
}
int result = 0;
for (int i = 0, n = a.length; i < n; i++) {
if (predicate.test(a[i])) {
a[i] = newValue;
result++;
}
}
return result;
}
public static int replaceIf(final float[] a, final Throwables.FloatPredicate predicate, final float newValue) throws E {
if (N.isNullOrEmpty(a)) {
return 0;
}
int result = 0;
for (int i = 0, n = a.length; i < n; i++) {
if (predicate.test(a[i])) {
a[i] = newValue;
result++;
}
}
return result;
}
public static int replaceIf(final double[] a, final Throwables.DoublePredicate predicate, final double newValue) throws E {
if (N.isNullOrEmpty(a)) {
return 0;
}
int result = 0;
for (int i = 0, n = a.length; i < n; i++) {
if (predicate.test(a[i])) {
a[i] = newValue;
result++;
}
}
return result;
}
public static int replaceIf(final T[] a, final Throwables.Predicate super T, E> predicate, final T newValue) throws E {
if (N.isNullOrEmpty(a)) {
return 0;
}
int result = 0;
for (int i = 0, n = a.length; i < n; i++) {
if (predicate.test(a[i])) {
a[i] = newValue;
result++;
}
}
return result;
}
public static int replaceIf(final List c, final Throwables.Predicate super T, E> predicate, final T newValue) throws E {
if (N.isNullOrEmpty(c)) {
return 0;
}
int result = 0;
for (int i = 0, n = c.size(); i < n; i++) {
if (predicate.test(c.get(i))) {
c.set(i, newValue);
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 IllegalArgumentException if the specified {@code Array} is null
.
*/
public static T[] add(final T[] a, final T element) throws IllegalArgumentException {
checkArgNotNull(a, "a");
final int len = a.length;
final T[] newArray = (T[]) Array.newInstance(a.getClass().getComponentType(), len + 1);
if (len > 0) {
copy(a, 0, newArray, 0, len);
}
newArray[len] = 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 IllegalArgumentException if the specified {@code Array} is null
.
*/
@SafeVarargs
public static T[] addAll(final T[] a, final T... b) throws IllegalArgumentException {
checkArgNotNull(a, "a");
if (isNullOrEmpty(a)) {
return isNullOrEmpty(b) ? a.clone() : 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 IllegalArgumentException if the specified {@code Array} is null
.
*/
public static T[] insert(final T[] a, final int index, final T element) throws IllegalArgumentException {
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;
}
/**
* Returns a new String
*
* @param str
* @param index
* @param strToInsert
* @return a new String
*/
public static String insert(final String str, final int index, final String strToInsert) {
N.checkIndex(index, len(str));
if (isNullOrEmpty(strToInsert)) {
return nullToEmpty(str);
} else if (isNullOrEmpty(str)) {
return nullToEmpty(strToInsert);
} else if (index == str.length()) {
return StringUtil.concat(str + strToInsert);
}
return str;
}
/**
*
* 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 IllegalArgumentException if the specified {@code Array} is null
.
*/
@SafeVarargs
public static T[] insertAll(final T[] a, final int index, final T... b) throws IllegalArgumentException {
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.
* @throws IllegalArgumentException if the specified {@code Array} is null
.
*/
public static T[] delete(final T[] a, final int index) throws IllegalArgumentException {
checkArgNotNull(a, "a");
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 == null ? N.EMPTY_BOOLEAN_ARRAY : 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 == null ? N.EMPTY_CHAR_ARRAY : 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 == null ? N.EMPTY_BYTE_ARRAY : 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 == null ? N.EMPTY_SHORT_ARRAY : 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 == null ? N.EMPTY_INT_ARRAY : 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 == null ? N.EMPTY_LONG_ARRAY : 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 == null ? N.EMPTY_FLOAT_ARRAY : 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 == null ? N.EMPTY_DOUBLE_ARRAY : 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 IllegalArgumentException if the specified {@code Array} is null
.
*/
@SafeVarargs
public static T[] deleteAll(final T[] a, int... indices) throws IllegalArgumentException {
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;
}
/**
*
* 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.
* @throws IllegalArgumentException if the specified {@code Array} is null
.
*/
public static T[] remove(final T[] a, final T element) throws IllegalArgumentException {
checkArgNotNull(a, "a");
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> 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;
}
}
/**
* 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 == null ? N.EMPTY_BOOLEAN_ARRAY : a.clone();
}
final int len = len(a);
final boolean[] b = new boolean[len - (toIndex - fromIndex)];
if (fromIndex > 0) {
copy(a, 0, b, 0, fromIndex);
}
if (toIndex < len) {
copy(a, toIndex, b, fromIndex, len - 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 == null ? N.EMPTY_CHAR_ARRAY : a.clone();
}
final int len = len(a);
final char[] b = new char[len - (toIndex - fromIndex)];
if (fromIndex > 0) {
copy(a, 0, b, 0, fromIndex);
}
if (toIndex < len) {
copy(a, toIndex, b, fromIndex, len - 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 == null ? N.EMPTY_BYTE_ARRAY : a.clone();
}
final int len = len(a);
final byte[] b = new byte[len - (toIndex - fromIndex)];
if (fromIndex > 0) {
copy(a, 0, b, 0, fromIndex);
}
if (toIndex < len) {
copy(a, toIndex, b, fromIndex, len - 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 == null ? N.EMPTY_SHORT_ARRAY : a.clone();
}
final int len = len(a);
final short[] b = new short[len - (toIndex - fromIndex)];
if (fromIndex > 0) {
copy(a, 0, b, 0, fromIndex);
}
if (toIndex < len) {
copy(a, toIndex, b, fromIndex, len - 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 == null ? N.EMPTY_INT_ARRAY : a.clone();
}
final int len = len(a);
final int[] b = new int[len - (toIndex - fromIndex)];
if (fromIndex > 0) {
copy(a, 0, b, 0, fromIndex);
}
if (toIndex < len) {
copy(a, toIndex, b, fromIndex, len - 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 == null ? N.EMPTY_LONG_ARRAY : a.clone();
}
final int len = len(a);
final long[] b = new long[len - (toIndex - fromIndex)];
if (fromIndex > 0) {
copy(a, 0, b, 0, fromIndex);
}
if (toIndex < len) {
copy(a, toIndex, b, fromIndex, len - 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 == null ? N.EMPTY_FLOAT_ARRAY : a.clone();
}
final int len = len(a);
final float[] b = new float[len - (toIndex - fromIndex)];
if (fromIndex > 0) {
copy(a, 0, b, 0, fromIndex);
}
if (toIndex < len) {
copy(a, toIndex, b, fromIndex, len - 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 == null ? N.EMPTY_DOUBLE_ARRAY : a.clone();
}
final int len = len(a);
final double[] b = new double[len - (toIndex - fromIndex)];
if (fromIndex > 0) {
copy(a, 0, b, 0, fromIndex);
}
if (toIndex < len) {
copy(a, toIndex, b, fromIndex, len - toIndex);
}
return b;
}
/**
* Deletes the values from {@code fromIndex} to {@code toIndex}.
*
* @param
* @param a
* @param fromIndex
* @param toIndex
* @return a new array
* @throws IllegalArgumentException if the specified {@code Array} is null
.
*/
public static T[] deleteRange(final T[] a, final int fromIndex, final int toIndex) throws IllegalArgumentException {
checkArgNotNull(a, "a");
checkFromToIndex(fromIndex, toIndex, a.length);
if (fromIndex == toIndex) {
return a.clone();
}
final int len = len(a);
final T[] b = Array.newInstance(a.getClass().getComponentType(), len - (toIndex - fromIndex));
if (fromIndex > 0) {
copy(a, 0, b, 0, fromIndex);
}
if (toIndex < len) {
copy(a, toIndex, b, fromIndex, len - 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", "deprecation" })
public static boolean deleteRange(final List c, final int fromIndex, final int toIndex) {
checkFromToIndex(fromIndex, toIndex, c.size());
if (fromIndex == toIndex) {
return false;
}
final int size = c.size();
if (c instanceof LinkedList || toIndex - fromIndex <= 3) {
c.subList(fromIndex, toIndex).clear();
} else {
final T[] a = (T[]) InternalUtil.getInternalArray(c);
if (a != null) {
try {
copy(a, toIndex, a, fromIndex, size - toIndex);
N.fill(a, size - (toIndex - fromIndex), size, null);
InternalUtil.listSizeField.set(c, size - (toIndex - fromIndex));
// update modCount
c.add(a[0]);
c.remove(c.size() - 1);
return true;
} catch (Throwable e) {
// ignore;
InternalUtil.isListElementDataFieldSettable = false;
}
}
final List tmp = new ArrayList<>(size - (toIndex - fromIndex));
if (fromIndex > 0) {
tmp.addAll(c.subList(0, fromIndex));
}
if (toIndex < size) {
tmp.addAll(c.subList(toIndex, size));
}
c.clear();
c.addAll(tmp);
}
return true;
}
public static String deleteRange(String str, final int fromIndex, final int toIndex) {
final int len = len(str);
checkFromToIndex(fromIndex, toIndex, len);
if (fromIndex == toIndex || fromIndex >= len) {
return str == null ? N.EMPTY_STRING : str;
} else if (toIndex - fromIndex >= len) {
return N.EMPTY_STRING;
}
return StringUtil.concat(str.substring(0, fromIndex) + str.subSequence(toIndex, len));
}
/**
*
* @param a
* @param fromIndex
* @param toIndex
* @param newPositionStartIndex must in the range: [0, array.length - (toIndex - fromIndex)]
*/
public static void moveRange(final boolean[] a, final int fromIndex, final int toIndex, final int newPositionStartIndex) {
final int len = len(a);
checkIndexAndStartPositionForMoveRange(fromIndex, toIndex, newPositionStartIndex, len);
if (fromIndex == toIndex || fromIndex == newPositionStartIndex) {
return;
}
final boolean[] rangeTmp = N.copyOfRange(a, fromIndex, toIndex);
// move ahead
if (newPositionStartIndex < fromIndex) {
N.copy(a, newPositionStartIndex, a, toIndex - (fromIndex - newPositionStartIndex), fromIndex - newPositionStartIndex);
} else {
N.copy(a, toIndex, a, fromIndex, newPositionStartIndex - fromIndex);
}
N.copy(rangeTmp, 0, a, newPositionStartIndex, rangeTmp.length);
}
/**
*
* @param a
* @param fromIndex
* @param toIndex
* @param newPositionStartIndex must in the range: [0, array.length - (toIndex - fromIndex)]
*/
public static void moveRange(final char[] a, final int fromIndex, final int toIndex, final int newPositionStartIndex) {
final int len = len(a);
checkIndexAndStartPositionForMoveRange(fromIndex, toIndex, newPositionStartIndex, len);
if (fromIndex == toIndex || fromIndex == newPositionStartIndex) {
return;
}
final char[] rangeTmp = N.copyOfRange(a, fromIndex, toIndex);
// move ahead
if (newPositionStartIndex < fromIndex) {
N.copy(a, newPositionStartIndex, a, toIndex - (fromIndex - newPositionStartIndex), fromIndex - newPositionStartIndex);
} else {
N.copy(a, toIndex, a, fromIndex, newPositionStartIndex - fromIndex);
}
N.copy(rangeTmp, 0, a, newPositionStartIndex, rangeTmp.length);
}
/**
*
* @param a
* @param fromIndex
* @param toIndex
* @param newPositionStartIndex must in the range: [0, array.length - (toIndex - fromIndex)]
*/
public static void moveRange(final byte[] a, final int fromIndex, final int toIndex, final int newPositionStartIndex) {
final int len = len(a);
checkIndexAndStartPositionForMoveRange(fromIndex, toIndex, newPositionStartIndex, len);
if (fromIndex == toIndex || fromIndex == newPositionStartIndex) {
return;
}
final byte[] rangeTmp = N.copyOfRange(a, fromIndex, toIndex);
// move ahead
if (newPositionStartIndex < fromIndex) {
N.copy(a, newPositionStartIndex, a, toIndex - (fromIndex - newPositionStartIndex), fromIndex - newPositionStartIndex);
} else {
N.copy(a, toIndex, a, fromIndex, newPositionStartIndex - fromIndex);
}
N.copy(rangeTmp, 0, a, newPositionStartIndex, rangeTmp.length);
}
/**
*
* @param a
* @param fromIndex
* @param toIndex
* @param newPositionStartIndex must in the range: [0, array.length - (toIndex - fromIndex)]
*/
public static void moveRange(final short[] a, final int fromIndex, final int toIndex, final int newPositionStartIndex) {
final int len = len(a);
checkIndexAndStartPositionForMoveRange(fromIndex, toIndex, newPositionStartIndex, len);
if (fromIndex == toIndex || fromIndex == newPositionStartIndex) {
return;
}
final short[] rangeTmp = N.copyOfRange(a, fromIndex, toIndex);
// move ahead
if (newPositionStartIndex < fromIndex) {
N.copy(a, newPositionStartIndex, a, toIndex - (fromIndex - newPositionStartIndex), fromIndex - newPositionStartIndex);
} else {
N.copy(a, toIndex, a, fromIndex, newPositionStartIndex - fromIndex);
}
N.copy(rangeTmp, 0, a, newPositionStartIndex, rangeTmp.length);
}
/**
*
* @param a
* @param fromIndex
* @param toIndex
* @param newPositionStartIndex must in the range: [0, array.length - (toIndex - fromIndex)]
*/
public static void moveRange(final int[] a, final int fromIndex, final int toIndex, final int newPositionStartIndex) {
final int len = len(a);
checkIndexAndStartPositionForMoveRange(fromIndex, toIndex, newPositionStartIndex, len);
if (fromIndex == toIndex || fromIndex == newPositionStartIndex) {
return;
}
final int[] rangeTmp = N.copyOfRange(a, fromIndex, toIndex);
// move ahead
if (newPositionStartIndex < fromIndex) {
N.copy(a, newPositionStartIndex, a, toIndex - (fromIndex - newPositionStartIndex), fromIndex - newPositionStartIndex);
} else {
N.copy(a, toIndex, a, fromIndex, newPositionStartIndex - fromIndex);
}
N.copy(rangeTmp, 0, a, newPositionStartIndex, rangeTmp.length);
}
/**
*
* @param a
* @param fromIndex
* @param toIndex
* @param newPositionStartIndex must in the range: [0, array.length - (toIndex - fromIndex)]
*/
public static void moveRange(final long[] a, final int fromIndex, final int toIndex, final int newPositionStartIndex) {
final int len = len(a);
checkIndexAndStartPositionForMoveRange(fromIndex, toIndex, newPositionStartIndex, len);
if (fromIndex == toIndex || fromIndex == newPositionStartIndex) {
return;
}
final long[] rangeTmp = N.copyOfRange(a, fromIndex, toIndex);
// move ahead
if (newPositionStartIndex < fromIndex) {
N.copy(a, newPositionStartIndex, a, toIndex - (fromIndex - newPositionStartIndex), fromIndex - newPositionStartIndex);
} else {
N.copy(a, toIndex, a, fromIndex, newPositionStartIndex - fromIndex);
}
N.copy(rangeTmp, 0, a, newPositionStartIndex, rangeTmp.length);
}
/**
*
* @param a
* @param fromIndex
* @param toIndex
* @param newPositionStartIndex must in the range: [0, array.length - (toIndex - fromIndex)]
*/
public static void moveRange(final double[] a, final int fromIndex, final int toIndex, final int newPositionStartIndex) {
final int len = len(a);
checkIndexAndStartPositionForMoveRange(fromIndex, toIndex, newPositionStartIndex, len);
if (fromIndex == toIndex || fromIndex == newPositionStartIndex) {
return;
}
final double[] rangeTmp = N.copyOfRange(a, fromIndex, toIndex);
// move ahead
if (newPositionStartIndex < fromIndex) {
N.copy(a, newPositionStartIndex, a, toIndex - (fromIndex - newPositionStartIndex), fromIndex - newPositionStartIndex);
} else {
N.copy(a, toIndex, a, fromIndex, newPositionStartIndex - fromIndex);
}
N.copy(rangeTmp, 0, a, newPositionStartIndex, rangeTmp.length);
}
/**
*
* @param a
* @param fromIndex
* @param toIndex
* @param newPositionStartIndex must in the range: [0, array.length - (toIndex - fromIndex)]
*/
public static void moveRange(final T[] a, final int fromIndex, final int toIndex, final int newPositionStartIndex) {
final int len = len(a);
checkIndexAndStartPositionForMoveRange(fromIndex, toIndex, newPositionStartIndex, len);
if (fromIndex == toIndex || fromIndex == newPositionStartIndex) {
return;
}
final T[] rangeTmp = N.copyOfRange(a, fromIndex, toIndex);
// move ahead
if (newPositionStartIndex < fromIndex) {
N.copy(a, newPositionStartIndex, a, toIndex - (fromIndex - newPositionStartIndex), fromIndex - newPositionStartIndex);
} else {
N.copy(a, toIndex, a, fromIndex, newPositionStartIndex - fromIndex);
}
N.copy(rangeTmp, 0, a, newPositionStartIndex, rangeTmp.length);
}
/**
*
* @param c
* @param fromIndex
* @param toIndex
* @param newPositionStartIndex must in the range: [0, list.size() - (toIndex - fromIndex)]
* @return {@code true} if the specified {@code List} is updated.
*/
@SuppressWarnings("deprecation")
public static boolean moveRange(final List c, final int fromIndex, final int toIndex, final int newPositionStartIndex) {
final int size = size(c);
checkIndexAndStartPositionForMoveRange(fromIndex, toIndex, newPositionStartIndex, size);
if (fromIndex == toIndex || fromIndex == newPositionStartIndex) {
return false;
}
final T[] a = (T[]) InternalUtil.getInternalArray(c);
if (a != null) {
try {
moveRange(a, fromIndex, toIndex, newPositionStartIndex);
// update modCount
c.add(a[0]);
c.remove(c.size() - 1);
return true;
} catch (Throwable e) {
// ignore;
InternalUtil.isListElementDataFieldSettable = false;
}
}
final T[] tmp = (T[]) c.toArray();
moveRange(tmp, fromIndex, toIndex, newPositionStartIndex);
c.clear();
c.addAll(Arrays.asList(tmp));
return true;
}
/**
*
* @param str
* @param fromIndex
* @param toIndex
* @param newPositionStartIndex must in the range: [0, String.length - (toIndex - fromIndex)]
*/
@SuppressWarnings("deprecation")
public static String moveRange(final String str, final int fromIndex, final int toIndex, final int newPositionStartIndex) {
final int len = len(str);
checkIndexAndStartPositionForMoveRange(fromIndex, toIndex, newPositionStartIndex, len);
if (fromIndex == toIndex || fromIndex == newPositionStartIndex) {
return str;
}
final char[] a = str.toCharArray();
moveRange(a, fromIndex, toIndex, newPositionStartIndex);
return InternalUtil.newString(a, true);
}
// /**
// * Return a new array copy.
// *
// * @param a
// * @param fromIndex
// * @param toIndex
// * @param newPositionStartIndex must in the range: [0, array.length - (toIndex - fromIndex)]
// * @return a new array.
// */
// public static boolean[] copyThenMoveRange(final boolean[] a, final int fromIndex, final int toIndex, final int newPositionStartIndex) {
// checkIndexAndStartPositionForMoveRange(fromIndex, toIndex, newPositionStartIndex, len(a));
//
// final boolean[] copy = N.isNullOrEmpty(a) ? a : a.clone();
//
// moveRange(copy, fromIndex, toIndex, newPositionStartIndex);
//
// return copy;
// }
//
// /**
// * Return a new array copy.
// *
// * @param a
// * @param fromIndex
// * @param toIndex
// * @param newPositionStartIndex must in the range: [0, array.length - (toIndex - fromIndex)]
// * @return a new array.
// */
// public static char[] copyThenMoveRange(final char[] a, final int fromIndex, final int toIndex, final int newPositionStartIndex) {
// checkIndexAndStartPositionForMoveRange(fromIndex, toIndex, newPositionStartIndex, len(a));
//
// final char[] copy = N.isNullOrEmpty(a) ? a : a.clone();
//
// moveRange(copy, fromIndex, toIndex, newPositionStartIndex);
//
// return copy;
// }
//
// /**
// * Return a new array copy.
// *
// * @param a
// * @param fromIndex
// * @param toIndex
// * @param newPositionStartIndex must in the range: [0, array.length - (toIndex - fromIndex)]
// * @return a new array.
// */
// public static byte[] copyThenMoveRange(final byte[] a, final int fromIndex, final int toIndex, final int newPositionStartIndex) {
// checkIndexAndStartPositionForMoveRange(fromIndex, toIndex, newPositionStartIndex, len(a));
//
// final byte[] copy = N.isNullOrEmpty(a) ? a : a.clone();
//
// moveRange(copy, fromIndex, toIndex, newPositionStartIndex);
//
// return copy;
// }
//
// /**
// * Return a new array copy.
// *
// * @param a
// * @param fromIndex
// * @param toIndex
// * @param newPositionStartIndex must in the range: [0, array.length - (toIndex - fromIndex)]
// * @return a new array.
// */
// public static short[] copyThenMoveRange(final short[] a, final int fromIndex, final int toIndex, final int newPositionStartIndex) {
// checkIndexAndStartPositionForMoveRange(fromIndex, toIndex, newPositionStartIndex, len(a));
//
// final short[] copy = N.isNullOrEmpty(a) ? a : a.clone();
//
// moveRange(copy, fromIndex, toIndex, newPositionStartIndex);
//
// return copy;
// }
//
// /**
// * Return a new array copy.
// *
// * @param a
// * @param fromIndex
// * @param toIndex
// * @param newPositionStartIndex must in the range: [0, array.length - (toIndex - fromIndex)]
// * @return a new array.
// */
// public static int[] copyThenMoveRange(final int[] a, final int fromIndex, final int toIndex, final int newPositionStartIndex) {
// checkIndexAndStartPositionForMoveRange(fromIndex, toIndex, newPositionStartIndex, len(a));
//
// final int[] copy = N.isNullOrEmpty(a) ? a : a.clone();
//
// moveRange(copy, fromIndex, toIndex, newPositionStartIndex);
//
// return copy;
// }
//
// /**
// * Return a new array copy.
// *
// * @param a
// * @param fromIndex
// * @param toIndex
// * @param newPositionStartIndex must in the range: [0, array.length - (toIndex - fromIndex)]
// * @return a new array.
// */
// public static long[] copyThenMoveRange(final long[] a, final int fromIndex, final int toIndex, final int newPositionStartIndex) {
// checkIndexAndStartPositionForMoveRange(fromIndex, toIndex, newPositionStartIndex, len(a));
//
// final long[] copy = N.isNullOrEmpty(a) ? a : a.clone();
//
// moveRange(copy, fromIndex, toIndex, newPositionStartIndex);
//
// return copy;
// }
//
// /**
// * Return a new array copy.
// *
// * @param a
// * @param fromIndex
// * @param toIndex
// * @param newPositionStartIndex must in the range: [0, array.length - (toIndex - fromIndex)]
// * @return a new array.
// */
// public static double[] copyThenMoveRange(final double[] a, final int fromIndex, final int toIndex, final int newPositionStartIndex) {
// checkIndexAndStartPositionForMoveRange(fromIndex, toIndex, newPositionStartIndex, len(a));
//
// final double[] copy = N.isNullOrEmpty(a) ? a : a.clone();
//
// moveRange(copy, fromIndex, toIndex, newPositionStartIndex);
//
// return copy;
// }
//
// /**
// * Return a new array copy.
// *
// * @param a
// * @param fromIndex
// * @param toIndex
// * @param newPositionStartIndex must in the range: [0, array.length - (toIndex - fromIndex)]
// * @return a new array.
// */
// public static T[] copyThenMoveRange(final T[] a, final int fromIndex, final int toIndex, final int newPositionStartIndex) {
// checkIndexAndStartPositionForMoveRange(fromIndex, toIndex, newPositionStartIndex, len(a));
//
// final T[] copy = N.isNullOrEmpty(a) ? a : a.clone();
//
// moveRange(copy, fromIndex, toIndex, newPositionStartIndex);
//
// return copy;
// }
private static void checkIndexAndStartPositionForMoveRange(final int fromIndex, final int toIndex, final int newPositionStartIndex, final int len) {
checkFromToIndex(fromIndex, toIndex, len);
if (newPositionStartIndex < 0 || newPositionStartIndex > (len - (toIndex - fromIndex))) {
throw new IndexOutOfBoundsException("newPositionStartIndex " + newPositionStartIndex + " is out-of-bounds: [0, " + (len - (toIndex - fromIndex))
+ "=(array.length - (toIndex - fromIndex))]");
}
}
/**
* Return a new array.
*
* @param a
* @param fromIndex
* @param toIndex
* @param replacement
* @return a new array.
*/
public static boolean[] replaceRange(final boolean[] a, final int fromIndex, final int toIndex, final boolean[] replacement) {
final int len = len(a);
checkFromToIndex(fromIndex, toIndex, len);
if (N.isNullOrEmpty(a)) {
return N.isNullOrEmpty(replacement) ? N.EMPTY_BOOLEAN_ARRAY : replacement.clone();
} else if (N.isNullOrEmpty(replacement)) {
return N.deleteRange(a, fromIndex, toIndex);
}
final boolean[] result = new boolean[len - (toIndex - fromIndex) + replacement.length];
if (fromIndex > 0) {
N.copy(a, 0, result, 0, fromIndex);
}
N.copy(replacement, 0, result, fromIndex, replacement.length);
if (toIndex < len) {
N.copy(a, toIndex, result, fromIndex + replacement.length, len - toIndex);
}
return result;
}
/**
* Return a new array.
*
* @param a
* @param fromIndex
* @param toIndex
* @param replacement
* @return a new array.
*/
public static char[] replaceRange(final char[] a, final int fromIndex, final int toIndex, final char[] replacement) {
final int len = len(a);
checkFromToIndex(fromIndex, toIndex, len);
if (N.isNullOrEmpty(a)) {
return N.isNullOrEmpty(replacement) ? N.EMPTY_CHAR_ARRAY : replacement.clone();
} else if (N.isNullOrEmpty(replacement)) {
return N.deleteRange(a, fromIndex, toIndex);
}
final char[] result = new char[len - (toIndex - fromIndex) + replacement.length];
if (fromIndex > 0) {
N.copy(a, 0, result, 0, fromIndex);
}
N.copy(replacement, 0, result, fromIndex, replacement.length);
if (toIndex < len) {
N.copy(a, toIndex, result, fromIndex + replacement.length, len - toIndex);
}
return result;
}
/**
* Return a new array.
*
* @param a
* @param fromIndex
* @param toIndex
* @param replacement
* @return a new array.
*/
public static byte[] replaceRange(final byte[] a, final int fromIndex, final int toIndex, final byte[] replacement) {
final int len = len(a);
checkFromToIndex(fromIndex, toIndex, len);
if (N.isNullOrEmpty(a)) {
return N.isNullOrEmpty(replacement) ? N.EMPTY_BYTE_ARRAY : replacement.clone();
} else if (N.isNullOrEmpty(replacement)) {
return N.deleteRange(a, fromIndex, toIndex);
}
final byte[] result = new byte[len - (toIndex - fromIndex) + replacement.length];
if (fromIndex > 0) {
N.copy(a, 0, result, 0, fromIndex);
}
N.copy(replacement, 0, result, fromIndex, replacement.length);
if (toIndex < len) {
N.copy(a, toIndex, result, fromIndex + replacement.length, len - toIndex);
}
return result;
}
/**
* Return a new array.
*
* @param a
* @param fromIndex
* @param toIndex
* @param replacement
* @return a new array.
*/
public static short[] replaceRange(final short[] a, final int fromIndex, final int toIndex, final short[] replacement) {
final int len = len(a);
checkFromToIndex(fromIndex, toIndex, len);
if (N.isNullOrEmpty(a)) {
return N.isNullOrEmpty(replacement) ? N.EMPTY_SHORT_ARRAY : replacement.clone();
} else if (N.isNullOrEmpty(replacement)) {
return N.deleteRange(a, fromIndex, toIndex);
}
final short[] result = new short[len - (toIndex - fromIndex) + replacement.length];
if (fromIndex > 0) {
N.copy(a, 0, result, 0, fromIndex);
}
N.copy(replacement, 0, result, fromIndex, replacement.length);
if (toIndex < len) {
N.copy(a, toIndex, result, fromIndex + replacement.length, len - toIndex);
}
return result;
}
/**
* Return a new array.
*
* @param a
* @param fromIndex
* @param toIndex
* @param replacement
* @return a new array.
*/
public static int[] replaceRange(final int[] a, final int fromIndex, final int toIndex, final int[] replacement) {
final int len = len(a);
checkFromToIndex(fromIndex, toIndex, len);
if (N.isNullOrEmpty(a)) {
return N.isNullOrEmpty(replacement) ? N.EMPTY_INT_ARRAY : replacement.clone();
} else if (N.isNullOrEmpty(replacement)) {
return N.deleteRange(a, fromIndex, toIndex);
}
final int[] result = new int[len - (toIndex - fromIndex) + replacement.length];
if (fromIndex > 0) {
N.copy(a, 0, result, 0, fromIndex);
}
N.copy(replacement, 0, result, fromIndex, replacement.length);
if (toIndex < len) {
N.copy(a, toIndex, result, fromIndex + replacement.length, len - toIndex);
}
return result;
}
/**
* Return a new array.
*
* @param a
* @param fromIndex
* @param toIndex
* @param replacement
* @return a new array.
*/
public static long[] replaceRange(final long[] a, final int fromIndex, final int toIndex, final long[] replacement) {
final int len = len(a);
checkFromToIndex(fromIndex, toIndex, len);
if (N.isNullOrEmpty(a)) {
return N.isNullOrEmpty(replacement) ? N.EMPTY_LONG_ARRAY : replacement.clone();
} else if (N.isNullOrEmpty(replacement)) {
return N.deleteRange(a, fromIndex, toIndex);
}
final long[] result = new long[len - (toIndex - fromIndex) + replacement.length];
if (fromIndex > 0) {
N.copy(a, 0, result, 0, fromIndex);
}
N.copy(replacement, 0, result, fromIndex, replacement.length);
if (toIndex < len) {
N.copy(a, toIndex, result, fromIndex + replacement.length, len - toIndex);
}
return result;
}
/**
* Return a new array.
*
* @param a
* @param fromIndex
* @param toIndex
* @param replacement
* @return a new array.
*/
public static float[] replaceRange(final float[] a, final int fromIndex, final int toIndex, final float[] replacement) {
final int len = len(a);
checkFromToIndex(fromIndex, toIndex, len);
if (N.isNullOrEmpty(a)) {
return N.isNullOrEmpty(replacement) ? N.EMPTY_FLOAT_ARRAY : replacement.clone();
} else if (N.isNullOrEmpty(replacement)) {
return N.deleteRange(a, fromIndex, toIndex);
}
final float[] result = new float[len - (toIndex - fromIndex) + replacement.length];
if (fromIndex > 0) {
N.copy(a, 0, result, 0, fromIndex);
}
N.copy(replacement, 0, result, fromIndex, replacement.length);
if (toIndex < len) {
N.copy(a, toIndex, result, fromIndex + replacement.length, len - toIndex);
}
return result;
}
/**
* Return a new array.
*
* @param a
* @param fromIndex
* @param toIndex
* @param replacement
* @return a new array.
*/
public static double[] replaceRange(final double[] a, final int fromIndex, final int toIndex, final double[] replacement) {
final int len = len(a);
checkFromToIndex(fromIndex, toIndex, len);
if (N.isNullOrEmpty(a)) {
return N.isNullOrEmpty(replacement) ? N.EMPTY_DOUBLE_ARRAY : replacement.clone();
} else if (N.isNullOrEmpty(replacement)) {
return N.deleteRange(a, fromIndex, toIndex);
}
final double[] result = new double[len - (toIndex - fromIndex) + replacement.length];
if (fromIndex > 0) {
N.copy(a, 0, result, 0, fromIndex);
}
N.copy(replacement, 0, result, fromIndex, replacement.length);
if (toIndex < len) {
N.copy(a, toIndex, result, fromIndex + replacement.length, len - toIndex);
}
return result;
}
/**
* Return a new array.
*
* @param a
* @param fromIndex
* @param toIndex
* @param replacement
* @return a new array.
* @throws IllegalArgumentException if the specified {@code Array} is null
.
*/
public static T[] replaceRange(final T[] a, final int fromIndex, final int toIndex, final T[] replacement) throws IllegalArgumentException {
checkArgNotNull(a, "a");
final int len = len(a);
checkFromToIndex(fromIndex, toIndex, len);
if (N.isNullOrEmpty(a)) {
return N.isNullOrEmpty(replacement) ? a : replacement.clone();
} else if (N.isNullOrEmpty(replacement)) {
return N.deleteRange(a, fromIndex, toIndex);
}
final T[] result = (T[]) CommonUtil.newArray(a.getClass().getComponentType(), len - (toIndex - fromIndex) + replacement.length);
if (fromIndex > 0) {
N.copy(a, 0, result, 0, fromIndex);
}
N.copy(replacement, 0, result, fromIndex, replacement.length);
if (toIndex < len) {
N.copy(a, toIndex, result, fromIndex + replacement.length, len - toIndex);
}
return result;
}
/**
*
* @param
* @param c
* @param fromIndex
* @param toIndex
* @param replacement
* @return {@code true} if the specified {@code List} is updated.
* @throws IllegalArgumentException if the specified c
is null
.
*/
public static boolean replaceRange(final List c, final int fromIndex, final int toIndex, final Collection extends T> replacement)
throws IllegalArgumentException {
checkArgNotNull(c, "c");
final int size = size(c);
checkFromToIndex(fromIndex, toIndex, size);
if (N.isNullOrEmpty(replacement)) {
if (fromIndex == toIndex) {
return false;
}
return N.deleteRange(c, fromIndex, toIndex);
}
final List endList = toIndex < size ? new ArrayList<>(c.subList(toIndex, size)) : null;
if (fromIndex < size) {
N.deleteRange(c, fromIndex, size);
}
c.addAll(replacement);
if (N.notNullOrEmpty(endList)) {
c.addAll(endList);
}
return true;
}
/**
* Returns a new String.
*
* @param str
* @param fromIndex
* @param toIndex
* @param replacement
* @return
*/
@SuppressWarnings("deprecation")
public static String replaceRange(final String str, final int fromIndex, final int toIndex, final String replacement) {
final int len = len(str);
checkFromToIndex(fromIndex, toIndex, len);
if (N.isNullOrEmpty(str)) {
return N.isNullOrEmpty(replacement) ? str : replacement;
} else if (N.isNullOrEmpty(replacement)) {
return N.deleteRange(str, fromIndex, toIndex);
}
final char[] a = InternalUtil.getCharsForReadOnly(str);
final char[] tmp = N.replaceRange(a, fromIndex, toIndex, InternalUtil.getCharsForReadOnly(replacement));
return InternalUtil.newString(tmp, true);
}
// 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