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