org.databene.commons.ArrayUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of databene-commons Show documentation
Show all versions of databene-commons Show documentation
'databene commons' is an open source Java library by Volker Bergmann.
It provides extensions to the Java core library by utility classes, abstract concepts
and concrete implementations.
/*
* Copyright (C) 2004-2014 Volker Bergmann ([email protected]).
* All rights reserved.
*
* 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 org.databene.commons;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.databene.commons.iterator.ArrayIterator;
/**
* Provides array-related operations.
*
* Created: 09.06.2006 21:31:49
* @since 0.1
* @author Volker Bergmann
*/
public final class ArrayUtil {
public static T[] copyOfRange(T[] array, int offset, int length) {
return copyOfRange(array, offset, length, componentType(array));
}
@SuppressWarnings("unchecked")
public static Class componentType(T[] array) {
Class resultType = (Class) array.getClass();
Class componentType = (Class) resultType.getComponentType();
return componentType;
}
@SuppressWarnings("unchecked")
public static T[] copyOfRange(Object[] array, int offset, int length, Class componentType) {
T[] result = (T[]) Array.newInstance(componentType, length);
System.arraycopy(array, offset, result, 0, length);
return result;
}
public static T[] removeElement(T item, T[] array) {
int index = indexOf(item, array);
return remove(index, array);
}
@SuppressWarnings("unchecked")
public static T[] remove(int indexToRemove, T[] array) {
Class componentType = componentType(array);
T[] result = (T[]) Array.newInstance(componentType, array.length - 1);
if (indexToRemove > 0)
System.arraycopy(array, 0, result, 0, indexToRemove);
System.arraycopy(array, indexToRemove + 1, result, indexToRemove, array.length - indexToRemove - 1);
return result;
}
public static T[] removeAll(T[] toRemove, T[] target) {
Class componentType = componentType(target);
ArrayBuilder builder = new ArrayBuilder(componentType);
for (T element : target)
if (!contains(element, toRemove))
builder.add(element);
return builder.toArray();
}
// containment check -----------------------------------------------------------------------------------------------
/**
* Tells if an array contains a specific element
* @param element the element to search
* @param array the array to scan
* @return true if the element was found, else false
*/
public static boolean contains(Object element, Object array) {
int length = Array.getLength(array);
for (int i = 0; i < length; i++) {
Object o = Array.get(array, i);
if (NullSafeComparator.equals(o, element))
return true;
}
return false;
}
public static boolean containsAll(T[] subArray, T[] superArray) {
for (T t : subArray)
if (!contains(t, superArray))
return false;
return true;
}
public static int indexOf(byte[] subArray, byte[] array) {
return indexOf(subArray, 0, array);
}
public static int indexOf(byte[] subArray, int fromIndex, byte[] array) {
for (int i = fromIndex; i <= array.length - subArray.length; i++) {
boolean match = true;
for (int j = 0; j < subArray.length; j++) {
if (array[i + j] != subArray[j])
match = false;
}
if (match)
return i;
}
return -1;
}
/**
* Tells if an array ends with a specified sub array
* @param candidates the array to scan
* @param searched the sub array that is searched
* @return true if the array ands with or equals the searched sub array
*/
public static boolean endsWithSequence(T[] candidates, T[] searched) {
if (searched.length > candidates.length)
return false;
for (int i = 0; i < searched.length; i++) {
if (!candidates[candidates.length - searched.length + i].equals(searched[i]))
return false;
}
return true;
}
@SuppressWarnings("unchecked")
public static T[] commonElements(T[]... sources) {
Class componentType = null;
for (int arrayNumber = 0; arrayNumber < sources.length && componentType == null; arrayNumber++) {
T[] source = sources[arrayNumber];
for (int index = 0; index < source.length && componentType == null; index++)
if (source[index] != null)
componentType = (Class) source[index].getClass();
}
return commonElements(componentType, sources);
}
public static T[] commonElements(Class componentType, T[]... sources) {
ArrayBuilder builder = new ArrayBuilder(componentType);
T[] firstArray = sources[0];
for (T element : firstArray) {
boolean common = true;
for (int i = 1; i < sources.length; i++)
if (!ArrayUtil.contains(element, sources[i])) {
common = false;
break;
}
if (common)
builder.add(element);
}
return builder.toArray();
}
// identity checks -------------------------------------------------------------------------------------------------
/**
* Tells if two arrays have the same content, independent of the ordering
* @param a1 the first array to compare
* @param a2 the first array to compare
* @return true if the array have the same content, independent of the ordering
*/
public static boolean equalsIgnoreOrder(T[] a1, T[] a2) {
if (a1 == a2)
return true;
if (a1 == null)
return false;
if (a1.length != a2.length)
return false;
List l1 = new ArrayList(a1.length);
for (T item : a1)
l1.add(item);
for (int i = a1.length - 1; i >= 0; i--)
if (contains(a1[i], a2))
l1.remove(i);
else
return false;
return l1.size() == 0;
}
public static boolean equals(Object a1, Object a2) {
if (a1 == a2)
return true;
if (a1 == null || !(a1.getClass().isArray()) || !(a2.getClass().isArray()))
return false;
int length = Array.getLength(a1);
if (length != Array.getLength(a2))
return false;
List