/**********************************************************************
Copyright (c) 2009-2010 Alexander Kerner. 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 net.sf.kerner.utils;

/**
 *  Utility class for array stuff.
 *
 * @author <a href="mailto:alex.kerner.24@googlemail.com">Alexander Kerner</a>
 * @version 2010-11-21
 *
 */
public class ArrayUtils {
	
	private ArrayUtils(){
		
	}
	
	/**
	 * 
	 * Trim an array to given size. All elements with {@code index > size} will be discarded.
	 *
	 * @param array array that is trimmed
	 * @param size the new length of the array
	 * @return the trimmed array
	 */
	public static char[] trim(char[] array, int size) {
		if (size < 0) {
			return new char[0];
		}
		if (array.length <= size)
			return array;
		final char[] result = new char[size];
		System.arraycopy(array, 0, result, 0, size);
		return result;
	}
	
	/**
	 * 
	 * Trim an array to given size. All elements with {@code index > size} will be discarded.
	 *
	 * @param array array that is trimmed
	 * @param size the new length of the array
	 * @return the trimmed array
	 */
	public static byte[] trim(byte[] array, int size) {
		if (size < 0) {
			return new byte[0];
		}
		if (array.length <= size)
			return array;
		final byte[] result = new byte[size];
		System.arraycopy(array, 0, result, 0, size);
		return result;
	}
	
	/**
	 * 
	 * Trim an array to given size. All elements with {@code index > size} will be discarded.
	 *
	 * @param array array that is trimmed
	 * @param size the new length of the array
	 * @return the trimmed array
	 */
	public static int[] trim(int[] array, int size) {
		if (size < 0) {
			return new int[0];
		}
		if (array.length <= size)
			return array;
		final int[] result = new int[size];
		System.arraycopy(array, 0, result, 0, size);
		return result;
	}
	
	/**
	 * 
	 * Check weather an array is {@code null} or contains no elements.
	 *
	 * @param arr array to check
	 * @return true, if {@code arr} is {@code null} or contains no elements
	 */
	public static boolean emptyArray(Object[] arr){
		if(arr == null)
			return true;
		if(arr.length < 1)
			return true;
		return false;
	}

}

@@ -2715,12 +2715,372 @@
 alse;%0A%09%7D
+%0A%09%0A%09public static double%5B%5D toPrimitive(Double%5B%5D arr)%7B%0A%09%09final double%5B%5D result = new double%5Barr.length%5D;%0A%09%09for(int i = 0; i %3C arr.length; i++)%0A%09%09%09result%5Bi%5D = arr%5Bi%5D;%0A%09%09return result;%0A%09%7D%0A%09%0A%09public static int%5B%5D toPrimitive(Integer%5B%5D arr)%7B%0A%09%09final int%5B%5D result = new int%5Barr.length%5D;%0A%09%09for(int i = 0; i %3C arr.length; i++)%0A%09%09%09result%5Bi%5D = arr%5Bi%5D;%0A%09%09return result;%0A%09%7D
 %0A%0A%7D%0A

introduce toPrimitive().