
com.cedarsoftware.util.ArrayUtilities Maven / Gradle / Ivy
package com.cedarsoftware.util;
import java.lang.reflect.Array;
/**
* Handy utilities for working with Java arrays.
*
* @author John DeRegnaucourt ([email protected]) & Ken Partlow
*
* Copyright (c) Cedar Software LLC
*
* 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.
*/
public class ArrayUtilities
{
/**
* Immutable common arrays.
*/
public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
public static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
/**
* Private constructor to promote using as static class.
*/
private ArrayUtilities()
{
super();
}
/**
* This is a null-safe isEmpty check. It uses the Array
* static class for doing a length check. This check is actually
* .0001 ms slower than the following typed check:
*
* return array == null || array.length == 0;
*
* but gives you more flexibility, since it checks for all array
* types.
*
* @param array array to check
* @return true if empty or null
*/
public static boolean isEmpty(final Object array)
{
return array == null || Array.getLength(array) == 0;
}
/**
* This is a null-safe size check. It uses the Array
* static class for doing a length check. This check is actually
* .0001 ms slower than the following typed check:
*
* return (array == null) ? 0 : array.length;
*
* @param array array to check
* @return true if empty or null
*/
public static int size(final Object array)
{
return array == null ? 0 : Array.getLength(array);
}
/**
* Shallow copies an array of Objects
*
* The objects in the array are not cloned, thus there is no special
* handling for multi-dimensional arrays.
*
* This method returns null
if null
array input.
*
* @param array the array to shallow clone, may be null
* @return the cloned array, null
if null
input
*/
public static Object[] shallowCopy(final Object[] array)
{
if (array == null)
{
return null;
}
return array.clone();
}
/**
* Adds all the elements of the given arrays into a new array.
* The new array contains all of the element of array1
followed
* by all of the elements array2
. When an array is returned, it is always
* a new array.
*
*
* ArrayUtilities.addAll(null, null) = null
* ArrayUtilities.addAll(array1, null) = cloned copy of array1
* ArrayUtilities.addAll(null, array2) = cloned copy of array2
* ArrayUtilities.addAll([], []) = []
* ArrayUtilities.addAll([null], [null]) = [null, null]
* ArrayUtilities.addAll(["a", "b", "c"], ["1", "2", "3"]) = ["a", "b", "c", "1", "2", "3"]
*
*
* @param array1 the first array whose elements are added to the new array, may be null
* @param array2 the second array whose elements are added to the new array, may be null
* @return The new array, null
if null
array inputs.
* The type of the new array is the type of the first array.
*/
public static Object[] addAll(final Object[] array1, final Object[] array2)
{
if (array1 == null)
{
return shallowCopy(array2);
}
else if (array2 == null)
{
return shallowCopy(array1);
}
final Object[] newArray = (Object[]) Array.newInstance(array1.getClass().getComponentType(),
array1.length + array2.length);
System.arraycopy(array1, 0, newArray, 0, array1.length);
System.arraycopy(array2, 0, newArray, array1.length, array2.length);
return newArray;
}
public static Object removeItem(Object array, int pos)
{
int length = Array.getLength(array);
Object dest = Array.newInstance(array.getClass().getComponentType(), length - 1);
System.arraycopy(array, 0, dest, 0, pos);
System.arraycopy(array, pos + 1, dest, pos, length - pos - 1);
return dest;
}
public static Object[] getArraySubset(Object[] ids, int start, int end)
{
Object[] subset = new Object[end - start];
System.arraycopy(ids, start, subset, 0, end - start);
return subset;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy