+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.utils.datastructures;\r
+\r
+import java.lang.reflect.Array;\r
+import java.util.Collection;\r
+\r
+/**\r
+ * Some generic utility operations for arrays that do not exist in the standard\r
+ * library.\r
+ * \r
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class Arrays {\r
+\r
+ /**\r
+ * Checks if an object is in an unsorted array.\r
+ * @param <T>\r
+ * @param array\r
+ * @param object\r
+ * @return <code>true</code> if the array contains the object\r
+ */\r
+ public static <T> boolean contains(T[] array, T object) {\r
+ for (T o : array) {\r
+ if(o.equals(object))\r
+ return true;\r
+ }\r
+ return false;\r
+ }\r
+\r
+ /**\r
+ * Returns index of an object in a array.\r
+ * @param <T>\r
+ * @param array\r
+ * @param object\r
+ * @return -1 if object is not in array\r
+ */\r
+ public static <T> int indexOf(T[] array, T object) {\r
+ for (int i = 0; i < array.length; i++)\r
+ if (array[i].equals(object))\r
+ return i;\r
+ return -1;\r
+ }\r
+\r
+ /**\r
+ * Adds all objects in an array to a collection\r
+ * @param <T>\r
+ * @param collection\r
+ * @param array\r
+ */\r
+ public static <T> void addAll(Collection<T> collection, T[] array) {\r
+ for (T o : array)\r
+ collection.add(o);\r
+ }\r
+\r
+ /**\r
+ * Appends a single element to the specified array.\r
+ * \r
+ * @param <T> type of the array elements\r
+ * @param src the array to append to\r
+ * @param t the element to append\r
+ * @return new array containing the specified element at the end\r
+ */\r
+ public static <T> T[] append(T[] src, T t) {\r
+ int len = src.length;\r
+ @SuppressWarnings("unchecked")\r
+ T[] result = (T[]) Array.newInstance(src.getClass().getComponentType(), len + 1);\r
+ System.arraycopy(src, 0, result, 0, len);\r
+ result[len] = t;\r
+ return result;\r
+ }\r
+\r
+ /**\r
+ * Appends a single element to the specified array.\r
+ * \r
+ * @param <T> type of the array elements\r
+ * @param src the array to append to\r
+ * @param ts the elements to append to the array\r
+ * @return new array containing the specified elements at the end\r
+ */\r
+ public static <T> T[] append(T[] src, @SuppressWarnings("unchecked") T... ts) {\r
+ if (ts.length == 0)\r
+ return src;\r
+ int len = src.length;\r
+ @SuppressWarnings("unchecked")\r
+ T[] result = (T[]) Array.newInstance(src.getClass().getComponentType(), len + ts.length);\r
+ System.arraycopy(src, 0, result, 0, len);\r
+ System.arraycopy(ts, 0, result, len, ts.length);\r
+ return result;\r
+ }\r
+\r
+ /**\r
+ * Removes the specified index\r
+ * \r
+ * @param <T>\r
+ * @param src\r
+ * @param index\r
+ * @return\r
+ */\r
+ public static <T> T[] remove(T[] src, int index) {\r
+ int len = src.length;\r
+ if (index < 0)\r
+ throw new ArrayIndexOutOfBoundsException("cannot remove negative index: " + index);\r
+ if (index >= len)\r
+ throw new ArrayIndexOutOfBoundsException("cannot remove array element " + index + ", array has " + len\r
+ + " elements");\r
+ @SuppressWarnings("unchecked")\r
+ T[] result = (T[]) Array.newInstance(src.getClass().getComponentType(), len - 1);\r
+ System.arraycopy(src, 0, result, 0, index);\r
+ System.arraycopy(src, index + 1, result, index, len - index - 1);\r
+ return result;\r
+ }\r
+\r
+ /**\r
+ * Like {@link #append(Object[], Object)} but checks that the element does\r
+ * not already exist in the array before appending. Just returns the\r
+ * specified array if the element already exists.\r
+ * \r
+ * @param <T> type of the array elements\r
+ * @param src the array to append to\r
+ * @param t the element to append\r
+ * @return new array containing the specified element at the end or\r
+ * <code>src</code> if the element was already contained\r
+ */\r
+ public static <T> T[] appendIfMissing(T[] src, T t) {\r
+ int len = src.length;\r
+ for (int i = 0; i < len; i++) {\r
+ if (t.equals(src[i]))\r
+ return src;\r
+ }\r
+ return append(src, t);\r
+ }\r
+\r
+ /**\r
+ * Looks for the first occurrence of the specified element from the\r
+ * specified array. If not found, the specified array is returned.\r
+ * Equals-comparison is used to look for the element.\r
+ * \r
+ * @param <T> type of the array elements\r
+ * @param src the array to append to\r
+ * @param t the element to remove\r
+ * @return new array not containing the specified element or\r
+ * <code>src</code> if the element was not contained\r
+ */\r
+ public static <T> T[] remove(T[] src, T t) {\r
+ int len = src.length;\r
+ for (int i = 0; i < len; i++) {\r
+ if (t.equals(src[i])) {\r
+ return remove(src, i);\r
+ }\r
+ }\r
+ return src;\r
+ }\r
+\r
+}\r