2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.util;
21 import java.io.Serializable;
22 import java.lang.reflect.Array;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.List;
28 /* ------------------------------------------------------------ */
31 public class ArrayUtil
32 implements Cloneable, Serializable
35 /* ------------------------------------------------------------ */
36 public static<T> T[] removeFromArray(T[] array, Object item)
38 if (item==null || array==null)
40 for (int i=array.length;i-->0;)
42 if (item.equals(array[i]))
44 Class<?> c = array==null?item.getClass():array.getClass().getComponentType();
45 @SuppressWarnings("unchecked")
46 T[] na = (T[])Array.newInstance(c, Array.getLength(array)-1);
48 System.arraycopy(array, 0, na, 0, i);
50 System.arraycopy(array, i+1, na, i, array.length-(i+1));
57 /* ------------------------------------------------------------ */
58 /** Add element to an array
59 * @param array The array to add to (or null)
60 * @param item The item to add
61 * @param type The type of the array (in case of null array)
62 * @return new array with contents of array plus item
64 public static<T> T[] addToArray(T[] array, T item, Class<?> type)
68 if (type==null && item!=null)
69 type= item.getClass();
70 @SuppressWarnings("unchecked")
71 T[] na = (T[])Array.newInstance(type, 1);
77 T[] na = Arrays.copyOf(array,array.length+1);
78 na[array.length]=item;
83 /* ------------------------------------------------------------ */
84 /** Add element to the start of an array
85 * @param array The array to add to (or null)
86 * @param item The item to add
87 * @param type The type of the array (in case of null array)
88 * @return new array with contents of array plus item
90 public static<T> T[] prependToArray(T item, T[] array, Class<?> type)
94 if (type==null && item!=null)
95 type= item.getClass();
96 @SuppressWarnings("unchecked")
97 T[] na = (T[])Array.newInstance(type, 1);
103 Class<?> c = array.getClass().getComponentType();
104 @SuppressWarnings("unchecked")
105 T[] na = (T[])Array.newInstance(c, Array.getLength(array)+1);
106 System.arraycopy(array, 0, na, 1, array.length);
112 /* ------------------------------------------------------------ */
114 * @param array Any array of object
115 * @return A new <i>modifiable</i> list initialised with the elements from <code>array</code>.
117 public static<E> List<E> asMutableList(E[] array)
119 if (array==null || array.length==0)
120 return new ArrayList<E>();
121 return new ArrayList<E>(Arrays.asList(array));
124 /* ------------------------------------------------------------ */
125 public static <T> T[] removeNulls(T[] array)
131 List<T> list = new ArrayList<>();
135 return list.toArray(Arrays.copyOf(array,list.size()));