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.
// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file *
// * to you 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 org.apache.juneau.internal;
import static org.apache.juneau.internal.StringUtils.*;
import static org.apache.juneau.internal.ThrowableUtils.*;
import java.lang.reflect.*;
import java.util.*;
/**
* Quick and dirty utilities for working with arrays.
*/
public final class ArrayUtils {
/**
* Appends one or more elements to an array.
*
* @param The element type.
* @param array The array to append to.
* @param newElements The new elements to append to the array.
* @return A new array with the specified elements appended.
*/
@SuppressWarnings("unchecked")
public static T[] append(T[] array, T...newElements) {
if (array == null)
return newElements;
if (newElements.length == 0)
return array;
T[] a = (T[])Array.newInstance(array.getClass().getComponentType(), array.length + newElements.length);
for (int i = 0; i < array.length; i++)
a[i] = array[i];
for (int i = 0; i < newElements.length; i++)
a[i+array.length] = newElements[i];
return a;
}
/**
* Appends one or more elements to an array.
*
* @param The element type.
* @param array The array to append to.
* @param newElements The new elements to append to the array.
* @return A new array with the specified elements appended.
*/
@SuppressWarnings("unchecked")
public static T[] append(T[] array, Collection newElements) {
assertFieldNotNull(array, "array");
if (newElements.size() == 0)
return array;
T[] a = (T[])Array.newInstance(array.getClass().getComponentType(), array.length + newElements.size());
for (int i = 0; i < array.length; i++)
a[i] = array[i];
int l = array.length;
for (T t : newElements)
a[l++] = t;
return a;
}
/**
* Combine an arbitrary number of arrays into a single array.
*
* @param arrays Collection of arrays to combine.
* @return A new combined array, or null if all arrays are null.
*/
@SuppressWarnings("unchecked")
public static T[] combine(T[]...arrays) {
assertFieldNotNull(arrays, "arrays");
int l = 0;
T[] a1 = null;
for (T[] a : arrays) {
if (a1 == null && a != null)
a1 = a;
l += (a == null ? 0 : a.length);
}
if (a1 == null)
return null;
T[] a = (T[])Array.newInstance(a1.getClass().getComponentType(), l);
int i = 0;
for (T[] aa : arrays)
if (aa != null)
for (T t : aa)
a[i++] = t;
return a;
}
/**
* Creates a new array with reversed entries.
*
* @param The class type of the array.
* @param array The array to reverse.
* @return A new array with reversed entries, or null if the array was null.
*/
@SuppressWarnings("unchecked")
public static T[] reverse(T[] array) {
if (array == null)
return null;
Class c = (Class)array.getClass().getComponentType();
T[] a2 = (T[])Array.newInstance(c, array.length);
for (int i = 0; i < array.length; i++)
a2[a2.length-i-1] = array[i];
return a2;
}
/**
* Sorts the elements in an array without creating a new array.
*
* @param array The array to sort.
* @return The same array.
*/
public static T[] reverseInline(T[] array) {
if (array == null)
return null;
T t;
for (int i = 0, j = array.length-1; i < j; i++, j--) {
t = array[i];
array[i] = array[j];
array[j] = t;
}
return array;
}
/**
* Converts the specified array to a Set.
*
*
* The order of the entries in the set are the same as the array.
*
* @param The entry type of the array.
* @param array The array being wrapped in a Set interface.
* @return The new set.
*/
public static Set asSet(final T[] array) {
assertFieldNotNull(array, "array");
return new AbstractSet() {
@Override /* Set */
public Iterator iterator() {
return new Iterator() {
int i = 0;
@Override /* Iterator */
public boolean hasNext() {
return i < array.length;
}
@Override /* Iterator */
public T next() {
if (i >= array.length)
throw new NoSuchElementException();
T t = array[i];
i++;
return t;
}
@Override /* Iterator */
public void remove() {
throw new UnsupportedOperationException();
}
};
}
@Override /* Set */
public int size() {
return array.length;
}
};
}
/**
* Returns an iterator against an array.
*
*
* This works with any array type (e.g. String[], Object[],
* int[], etc...).
*
* @param array The array to create an iterator over.
* @return An iterator over the specified array.
*/
public static Iterator