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