/*
 * oxCore is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */

package org.xdi.util;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * Utility methods to help work with arrays
 * 
 * @author Yuriy Movchan Date: 10.21.2010
 * @see org.apache.commons.lang.ArrayUtils
 */
public final class ArrayHelper {

	private ArrayHelper() {
	}

	@SuppressWarnings("unchecked")
	public static <T> T[] arrayMerge(T[]... arrays) {
		// Determine required size of new array
		int count = 0;
		for (T[] array : arrays) {
			count += array.length;
		}

		if (count == 0) {
			return (T[]) Array.newInstance(arrays.getClass().getComponentType().getComponentType(), 0);
		}

		// create new array of required class
		T[] mergedArray = (T[]) Array.newInstance(arrays[0][0].getClass(), count);

		// Merge each array into new array
		int start = 0;
		for (T[] array : arrays) {
			System.arraycopy(array, 0, mergedArray, start, array.length);
			start += array.length;
		}

		return (T[]) mergedArray;
	}

	public static <T> boolean isEmpty(T[] objects) {
		return (objects == null) || (objects.length == 0);
	}

	public static <T> boolean isNotEmpty(T[] objects) {
		return (objects != null) && (objects.length > 0);
	}

	@SuppressWarnings("unchecked")
	public static <T> T[] arrayClone(T[] array) {
		if (array == null) {
			return array;
		}
		if (array.length == 0) {
			return (T[]) Array.newInstance(array.getClass().getComponentType(), 0);
		}

		T[] clonedArray = (T[]) Array.newInstance(array[0].getClass(), array.length);
		System.arraycopy(array, 0, clonedArray, 0, array.length);

		return clonedArray;
	}

	public static String[] sortAndClone(String[] array) {
		if (array == null) {
			return array;
		}

		String[] clonedArray = arrayClone(array);
		Arrays.sort(clonedArray);

		return clonedArray;
	}

	public static boolean equalsIgnoreOrder(String[] values1, String[] values2) {
		String[] valuesSorted1 = sortAndClone(values1);
		String[] valuesSorted2 = sortAndClone(values2);

		return Arrays.equals(valuesSorted1, valuesSorted2);
	}

}

@@ -2125,10 +2125,219 @@
 2);%0A%09%7D%0A%0A
+%09public static String%5B%5D addItemToStringArray(String%5B%5D array, String item) %7B%0A%09%09String%5B%5D array2 = new String%5B%5D %7B item %7D; %0A%09%09if (array == null) %7B%0A%09%09%09return array2;%0A%09%09%7D%0A%09%09%0A%0A%09%09return arrayMerge(array, array2);%0A%09%7D%0A%0A
 %7D%0A

Add helper method to add item to String array