]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/util/ArrayUtil.java
Importing upstream Jetty jetty-9.2.1.v20140609
[gigi.git] / lib / jetty / org / eclipse / jetty / util / ArrayUtil.java
1 //
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.
8 //
9 //      The Eclipse Public License is available at
10 //      http://www.eclipse.org/legal/epl-v10.html
11 //
12 //      The Apache License v2.0 is available at
13 //      http://www.opensource.org/licenses/apache2.0.php
14 //
15 //  You may elect to redistribute this code under either of these licenses.
16 //  ========================================================================
17 //
18
19 package org.eclipse.jetty.util;
20
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;
26
27
28 /* ------------------------------------------------------------ */
29 /**
30  */
31 public class ArrayUtil
32     implements Cloneable, Serializable
33 {
34
35     /* ------------------------------------------------------------ */
36     public static<T> T[] removeFromArray(T[] array, Object item)
37     {
38         if (item==null || array==null)
39             return array;
40         for (int i=array.length;i-->0;)
41         {
42             if (item.equals(array[i]))
43             {
44                 Class<?> c = array==null?item.getClass():array.getClass().getComponentType();
45                 @SuppressWarnings("unchecked")
46                 T[] na = (T[])Array.newInstance(c, Array.getLength(array)-1);
47                 if (i>0)
48                     System.arraycopy(array, 0, na, 0, i);
49                 if (i+1<array.length)
50                     System.arraycopy(array, i+1, na, i, array.length-(i+1));
51                 return na;
52             }
53         }
54         return array;
55     }
56
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
63      */
64     public static<T> T[] addToArray(T[] array, T item, Class<?> type)
65     {
66         if (array==null)
67         {
68             if (type==null && item!=null)
69                 type= item.getClass();
70             @SuppressWarnings("unchecked")
71             T[] na = (T[])Array.newInstance(type, 1);
72             na[0]=item;
73             return na;
74         }
75         else
76         {
77             T[] na = Arrays.copyOf(array,array.length+1);
78             na[array.length]=item;
79             return na;
80         }
81     }
82     
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
89      */
90     public static<T> T[] prependToArray(T item, T[] array, Class<?> type)
91     {
92         if (array==null)
93         {
94             if (type==null && item!=null)
95                 type= item.getClass();
96             @SuppressWarnings("unchecked")
97             T[] na = (T[])Array.newInstance(type, 1);
98             na[0]=item;
99             return na;
100         }
101         else
102         {
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);
107             na[0]=item;
108             return na;
109         }
110     }
111
112     /* ------------------------------------------------------------ */
113     /**
114      * @param array Any array of object
115      * @return A new <i>modifiable</i> list initialised with the elements from <code>array</code>.
116      */
117     public static<E> List<E> asMutableList(E[] array)
118     {   
119         if (array==null || array.length==0)
120             return new ArrayList<E>();
121         return new ArrayList<E>(Arrays.asList(array));
122     }
123
124     /* ------------------------------------------------------------ */
125     public static <T> T[] removeNulls(T[] array)
126     {
127         for (T t : array)
128         {
129             if (t==null)
130             {
131                 List<T> list = new ArrayList<>();
132                 for (T t2:array)
133                     if (t2!=null)
134                         list.add(t2);
135                 return list.toArray(Arrays.copyOf(array,list.size()));
136             }
137         }
138         return array;
139     }
140     
141 }
142