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