]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/util/ArrayUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / util / ArrayUtils.java
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/util/ArrayUtils.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/util/ArrayUtils.java
new file mode 100644 (file)
index 0000000..96ab047
--- /dev/null
@@ -0,0 +1,230 @@
+/*******************************************************************************\r
+ *  Copyright (c) 2010 Association for Decentralized Information Management in\r
+ *  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.databoard.util;
+\r
+import java.lang.reflect.Array;\r
+
+public class ArrayUtils {\r
+       \r
+    /**\r
+     * Drop elements from array \r
+     * \r
+     * @param src source array\r
+     * @param elementsToDrop\r
+     * @return src or new array \r
+     */\r
+       @SafeVarargs\r
+       public static <T> T[] dropElements(T src[], T...elementsToDrop)\r
+    {\r
+        if (src.length==0) return src;\r
+        int count = 0;\r
+        nextA:\r
+        for (T a : src) {\r
+               for (T b : elementsToDrop)\r
+                       if (a==b) continue nextA;\r
+               count++;\r
+        }\r
+        if (count==src.length) return src;\r
+        \r
+        Class<?> componentClass = src.getClass().getComponentType();\r
+        @SuppressWarnings("unchecked")\r
+        T[] result = (T[]) Array.newInstance(componentClass, count);\r
+        if (count==0) return result;\r
+        int index = 0;\r
+        nextA2:\r
+        for (T a : src) {\r
+               for (T b : elementsToDrop)\r
+                       if (a==b) continue nextA2;\r
+               result[index++] = a;\r
+        }\r
+        return result;\r
+    }          \r
+    \r
+    /**\r
+     * Replace first occurance of object with another\r
+     * \r
+     * @param src\r
+     * @param from\r
+     * @param to\r
+     */\r
+    public static void replaceFirst(Object src[], Object from, Object to) {\r
+       for (int i=0; i<src.length; i++)\r
+               if (src[i] == from) {\r
+                       src[i] = to;\r
+                       return;\r
+               }\r
+    }\r
+    \r
+    /**\r
+     * Concatenate two arrays\r
+     * @param a\r
+     * @param b\r
+     * @return A new array with elements of a followed by elements of b\r
+     */\r
+    public static <T> T[] concatenate(T[] a, T[] b) {\r
+       @SuppressWarnings("unchecked")\r
+               Class<? extends T> compType = (Class<? extends T>) getCommonBase(a.getClass().getComponentType(), b.getClass().getComponentType());\r
+       if (compType == null)\r
+               throw new RuntimeException("Could not deduce common array type for " + a.getClass().getName() + " and " + b.getClass().getName());\r
+       return concatenate(a, b, compType);\r
+    }\r
+    \r
+    /**\r
+     * Concatenate two arrays\r
+     * @param a\r
+     * @param b\r
+     * @param componentType            The actual component type of the created array\r
+     * @return A new array with elements of a followed by elements of b\r
+     */\r
+    public static <T> T[] concatenate(T[] a, T[] b, Class<? extends T> componentType) {\r
+       @SuppressWarnings("unchecked")\r
+               T[] result = (T[]) Array.newInstance(componentType, a.length + b.length);\r
+       System.arraycopy(a, 0, result, 0, a.length);\r
+       System.arraycopy(b, 0, result, a.length, b.length);\r
+       return result;\r
+    }\r
+    \r
+    /**\r
+     * Concatenate two arrays, cropping or extending the result to a given length\r
+     * @param a\r
+     * @param b\r
+     * @param length   Length of the resulting array\r
+     * @return A new array with elements of a followed by elements of b, possibly padded with nulls to reach a given length\r
+     */\r
+    public static <T> T[] concatenate(T[] a, T[] b, int length) {\r
+       @SuppressWarnings("unchecked")\r
+               Class<? extends T> compType = (Class<? extends T>)getCommonBase(a.getClass().getComponentType(), b.getClass().getComponentType());\r
+       if (compType == null)\r
+               throw new RuntimeException("Could not deduce common array type for " + a.getClass().getName() + " and " + b.getClass().getName());\r
+       return concatenate(a, b, length, compType);\r
+    }\r
+    \r
+    /**\r
+     * Concatenate two arrays, cropping or extending the result to a given length\r
+     * @param a\r
+     * @param b\r
+     * @param length   Length of the resulting array\r
+     * @param componentType            The actual component type of the created array\r
+     * @return A new array with elements of a followed by elements of b, possibly padded with nulls to reach a given length\r
+     */\r
+    public static <T> T[] concatenate(T[] a, T[] b, int length, Class<? extends T> componentType) {\r
+               @SuppressWarnings("unchecked")\r
+               T[] result = (T[]) Array.newInstance(componentType, length);\r
+       System.arraycopy(a, 0, result, 0, Math.min(length, a.length));\r
+       if (length > a.length)\r
+               System.arraycopy(b, 0, result, a.length, Math.min(b.length, length - a.length));\r
+       return result;\r
+    }\r
+    \r
+    /**\r
+     * Add new elements to the end of an array\r
+     * @param a\r
+     * @param b\r
+     * @return A new array with elements b appended at the end\r
+     */\r
+    @SafeVarargs\r
+    public static <T> T[] append(T[] a, T ... b) {\r
+       return concatenate(a, b);\r
+    }\r
+    
+    /**\r
+     * Add new elements to the end of an array\r
+     * @param a\r
+     * @param b\r
+     * @return A new array with elements b appended at the end\r
+     */\r
+    @SafeVarargs\r
+    public static <T> T[] append(Class<? extends T> compType, T[] a, T ... b) {\r
+       return concatenate(a, b, compType);\r
+    }\r
+    \r
+    /**\r
+     * Add new elements to the end of an array\r
+     * @param a\r
+     * @param b\r
+     * @param length   Length of the resulting array\r
+     * @return A new array with elements b appended at the end\r
+     */\r
+    @SafeVarargs\r
+    public static <T> T[] append(int length, T[] a,  T ... b) {\r
+       return concatenate(a, b, length);\r
+    }\r
+    \r
+    /**\r
+     * Add new elements to the end of an array\r
+     * @param a\r
+     * @param b\r
+     * @param length   Length of the resulting array\r
+     * @return A new array with elements b appended at the end\r
+     */\r
+    @SafeVarargs\r
+    public static <T> T[] append(Class<? extends T> compType, int length, T[] a, T ... b) {\r
+       return concatenate(a, b, length, compType);\r
+    }\r
+    \r
+    /**\r
+     * Get an array with elements from a source array at given indices\r
+     * @param source   A source array\r
+     * @param index            An index array\r
+     * @return A new array with element i equal to source[index[i]]\r
+     */\r
+    public static <T> T[] indirection(T[] source, int[] index) {\r
+       @SuppressWarnings("unchecked")\r
+               T[] result = (T[]) Array.newInstance(source.getClass().getComponentType(), index.length);\r
+       for (int i = 0; i < index.length; i++)\r
+               result[i] = source[index[i]];\r
+       return result;\r
+    }\r
+    \r
+    /**\r
+     * Get an array with given length with elements from a source array at given indices\r
+     * @param source   A source array\r
+     * @param index            An index array\r
+     * @return A new array with element i equal to source[index[i]]. The resulting array length is set to #length.\r
+     */\r
+    public static <T> T[] indirection(T[] source, int[] index, int length) {\r
+       @SuppressWarnings("unchecked")\r
+               T[] result = (T[]) Array.newInstance(source.getClass().getComponentType(), length);\r
+       int n = Math.min(length, index.length);\r
+       for (int i = 0; i < n; i++)\r
+               result[i] = source[index[i]];\r
+       return result;\r
+    }\r
+    \r
+    /**\r
+     * Get a nearest common base class for two types. An interface type is returned only if one of classes\r
+     * is an interface type that is implemented by a superclass of the other. Thus, the returned base class for\r
+     * String and StringBuffer is returned Object, instead of either of the possible choices of CharSequence or\r
+     * Serializable.\r
+     * @param a\r
+     * @param b\r
+     * @return\r
+     */\r
+    public static Class<?> getCommonBase(Class<?> a, Class<?> b) {\r
+       if (a == null || b == null)\r
+               return Object.class;\r
+       else if (a.isArray() && b.isArray())\r
+               return Array.newInstance(getCommonBase(a.getComponentType(), b.getComponentType()), 0).getClass();\r
+       else if (a.isAssignableFrom(b))\r
+               return a;\r
+       else if (b.isAssignableFrom(a))\r
+               return b;\r
+       else { \r
+               /* Due to interface definitions, these cases might return different results. */\r
+               Class<?> ab = getCommonBase(a.getSuperclass(), b);\r
+               Class<?> ba = getCommonBase(a, b.getSuperclass());\r
+               \r
+               /* Return the less generic one */\r
+               return ab.isAssignableFrom(ba) ? ba : ab;\r
+       }\r
+    }\r
+}