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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.utils.datastructures;
\r
14 import java.lang.reflect.Array;
\r
15 import java.util.Collection;
\r
18 * Some generic utility operations for arrays that do not exist in the standard
\r
21 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
22 * @author Tuukka Lehtonen
\r
24 public class Arrays {
\r
27 * Checks if an object is in an unsorted array.
\r
31 * @return <code>true</code> if the array contains the object
\r
33 public static <T> boolean contains(T[] array, T object) {
\r
35 if(o.equals(object))
\r
42 * Returns index of an object in a array.
\r
46 * @return -1 if object is not in array
\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
56 * Adds all objects in an array to a collection
\r
61 public static <T> void addAll(Collection<T> collection, T[] array) {
\r
67 * Appends a single element to the specified array.
\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
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
84 * Appends a single element to the specified array.
\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
91 public static <T> T[] append(T[] src, @SuppressWarnings("unchecked") T... ts) {
\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
103 * Removes the specified index
\r
110 public static <T> T[] remove(T[] src, int index) {
\r
111 int len = src.length;
\r
113 throw new ArrayIndexOutOfBoundsException("cannot remove negative index: " + index);
\r
115 throw new ArrayIndexOutOfBoundsException("cannot remove array element " + index + ", array has " + len
\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
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
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
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
141 return append(src, t);
\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
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
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