]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/Arrays.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / Arrays.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.utils.datastructures;\r
13 \r
14 import java.lang.reflect.Array;\r
15 import java.util.Collection;\r
16 \r
17 /**\r
18  * Some generic utility operations for arrays that do not exist in the standard\r
19  * library.\r
20  * \r
21  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
22  * @author Tuukka Lehtonen\r
23  */\r
24 public class Arrays {\r
25 \r
26     /**\r
27      * Checks if an object is in an unsorted array.\r
28      * @param <T>\r
29      * @param array\r
30      * @param object\r
31      * @return <code>true</code> if the array contains the object\r
32      */\r
33     public static <T> boolean contains(T[] array, T object) {\r
34         for (T o : array) {\r
35             if(o.equals(object))\r
36                 return true;\r
37         }\r
38         return false;\r
39     }\r
40 \r
41     /**\r
42      * Returns index of an object in a array.\r
43      * @param <T>\r
44      * @param array\r
45      * @param object\r
46      * @return -1 if object is not in array\r
47      */\r
48     public static <T> int indexOf(T[] array, T object) {\r
49         for (int i = 0; i < array.length; i++)\r
50             if (array[i].equals(object))\r
51                 return i;\r
52         return -1;\r
53     }\r
54 \r
55     /**\r
56      * Adds all objects in an array to a collection\r
57      * @param <T>\r
58      * @param collection\r
59      * @param array\r
60      */\r
61     public static <T> void addAll(Collection<T> collection, T[] array) {\r
62         for (T o : array)\r
63             collection.add(o);\r
64     }\r
65 \r
66     /**\r
67      * Appends a single element to the specified array.\r
68      * \r
69      * @param <T> type of the array elements\r
70      * @param src the array to append to\r
71      * @param t the element to append\r
72      * @return new array containing the specified element at the end\r
73      */\r
74     public static <T> T[] append(T[] src, T t) {\r
75         int len = src.length;\r
76         @SuppressWarnings("unchecked")\r
77         T[] result = (T[]) Array.newInstance(src.getClass().getComponentType(), len + 1);\r
78         System.arraycopy(src, 0, result, 0, len);\r
79         result[len] = t;\r
80         return result;\r
81     }\r
82 \r
83     /**\r
84      * Appends a single element to the specified array.\r
85      * \r
86      * @param <T> type of the array elements\r
87      * @param src the array to append to\r
88      * @param ts the elements to append to the array\r
89      * @return new array containing the specified elements at the end\r
90      */\r
91     public static <T> T[] append(T[] src, @SuppressWarnings("unchecked") T... ts) {\r
92         if (ts.length == 0)\r
93             return src;\r
94         int len = src.length;\r
95         @SuppressWarnings("unchecked")\r
96         T[] result = (T[]) Array.newInstance(src.getClass().getComponentType(), len + ts.length);\r
97         System.arraycopy(src, 0, result, 0, len);\r
98         System.arraycopy(ts, 0, result, len, ts.length);\r
99         return result;\r
100     }\r
101 \r
102     /**\r
103      * Removes the specified index\r
104      * \r
105      * @param <T>\r
106      * @param src\r
107      * @param index\r
108      * @return\r
109      */\r
110     public static <T> T[] remove(T[] src, int index) {\r
111         int len = src.length;\r
112         if (index < 0)\r
113             throw new ArrayIndexOutOfBoundsException("cannot remove negative index: " + index);\r
114         if (index >= len)\r
115             throw new ArrayIndexOutOfBoundsException("cannot remove array element " + index + ", array has " + len\r
116                     + " elements");\r
117         @SuppressWarnings("unchecked")\r
118         T[] result = (T[]) Array.newInstance(src.getClass().getComponentType(), len - 1);\r
119         System.arraycopy(src, 0, result, 0, index);\r
120         System.arraycopy(src, index + 1, result, index, len - index - 1);\r
121         return result;\r
122     }\r
123 \r
124     /**\r
125      * Like {@link #append(Object[], Object)} but checks that the element does\r
126      * not already exist in the array before appending. Just returns the\r
127      * specified array if the element already exists.\r
128      * \r
129      * @param <T> type of the array elements\r
130      * @param src the array to append to\r
131      * @param t the element to append\r
132      * @return new array containing the specified element at the end or\r
133      *         <code>src</code> if the element was already contained\r
134      */\r
135     public static <T> T[] appendIfMissing(T[] src, T t) {\r
136         int len = src.length;\r
137         for (int i = 0; i < len; i++) {\r
138             if (t.equals(src[i]))\r
139                 return src;\r
140         }\r
141         return append(src, t);\r
142     }\r
143 \r
144     /**\r
145      * Looks for the first occurrence of the specified element from the\r
146      * specified array. If not found, the specified array is returned.\r
147      * Equals-comparison is used to look for the element.\r
148      * \r
149      * @param <T> type of the array elements\r
150      * @param src the array to append to\r
151      * @param t the element to remove\r
152      * @return new array not containing the specified element or\r
153      *         <code>src</code> if the element was not contained\r
154      */\r
155     public static <T> T[] remove(T[] src, T t) {\r
156         int len = src.length;\r
157         for (int i = 0; i < len; i++) {\r
158             if (t.equals(src[i])) {\r
159                 return remove(src, i);\r
160             }\r
161         }\r
162         return src;\r
163     }\r
164 \r
165 }\r