View Javadoc
1   /*
2    * $Source$
3    * $Revision$
4    *
5    * Copyright (C) 2000 William Chesters
6    *
7    * Part of Melati (http://melati.org), a framework for the rapid
8    * development of clean, maintainable web applications.
9    *
10   * Melati is free software; Permission is granted to copy, distribute
11   * and/or modify this software under the terms either:
12   *
13   * a) the GNU General Public License as published by the Free Software
14   *    Foundation; either version 2 of the License, or (at your option)
15   *    any later version,
16   *
17   *    or
18   *
19   * b) any version of the Melati Software License, as published
20   *    at http://melati.org
21   *
22   * You should have received a copy of the GNU General Public License and
23   * the Melati Software License along with this program;
24   * if not, write to the Free Software Foundation, Inc.,
25   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
26   * GNU General Public License and visit http://melati.org to obtain the
27   * Melati Software License.
28   *
29   * Feel free to contact the Developers of Melati (http://melati.org),
30   * if you would like to work out a different arrangement than the options
31   * outlined here.  It is our intention to allow Melati to be used by as
32   * wide an audience as possible.
33   *
34   * This program is distributed in the hope that it will be useful,
35   * but WITHOUT ANY WARRANTY; without even the implied warranty of
36   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37   * GNU General Public License for more details.
38   *
39   * Contact details for copyright holder:
40   *
41   *     William Chesters <williamc@paneris.org>
42   *     http://paneris.org/~williamc
43   *     Obrechtstraat 114, 2517VX Den Haag, The Netherlands
44   */
45  
46  package org.melati.poem.util;
47  
48  import java.util.Vector;
49  import java.util.Enumeration;
50  
51  /**
52   * A collection of useful operations on <code>Array</code>s.
53   */
54  public final class ArrayUtils {
55  
56    private ArrayUtils() {}
57  
58    /**
59     * Create an Array from a Vector.
60     * @param v the Vector
61     * @return the Array
62     */
63    @SuppressWarnings("unchecked")
64    public  static <T> T[] arrayOf(Vector<T> v) {
65      T[] arr;
66      synchronized (v) {
67        arr = (T[])new Object[v.size()];
68        v.copyInto(arr);
69      }
70      return arr;
71    }
72  
73    /**
74     * Create an Array from an Enumeration.
75     * @param e the Enumeration
76     * @return the Array
77     */
78    public static Object[] arrayOf(Enumeration<Object> e) {
79      Vector<Object> v = EnumUtils.vectorOf(e);
80      return arrayOf(v);
81    }
82  
83    /**
84     * Add an Object to an Array.
85     * 
86     * @param xs the array to add to
87     * @param y  the object to add
88     * @return the enlarged array
89     */
90    public static Object[] added(Object[] xs, Object y) {
91      Object[] xsx = (Object[])java.lang.reflect.Array.newInstance(
92                         xs.getClass().getComponentType(), xs.length + 1);
93      System.arraycopy(xs, 0, xsx, 0, xs.length);
94      xsx[xs.length] = y;
95      return xsx;
96    }
97    
98    /**
99     * Remove an Object from an Array.
100    * 
101    * @param xs the array to remove from
102    * @param y  the object to remove
103    * @return the reduced array
104    */
105   public static Object[] removed(Object[] xs, Object y) {
106     Object[] xsx = (Object[])java.lang.reflect.Array.newInstance(
107                        xs.getClass().getComponentType(), xs.length - 1);
108     int j = 0;
109     for (int i = 0; i < xs.length; i++) { 
110       if (xs[i] != y) { 
111         xsx[j] = xs[i];
112         j++;
113       }
114     }
115     return xsx;
116   }
117 
118   /**
119    * Create a new Array from two Arrays.
120    * @param xs  first Array 
121    * @param ys  second Array
122    * @return a new Array with the elements of the second appended to the first
123    */
124   public static Object[] concatenated(Object[] xs, Object[] ys) {
125     Object[] xsys =
126         (Object[])java.lang.reflect.Array.newInstance(
127             xs.getClass().getComponentType(), xs.length + ys.length);
128     System.arraycopy(xs, 0, xsys, 0, xs.length);
129     System.arraycopy(ys, 0, xsys, xs.length, ys.length);
130     return xsys;
131   }
132 
133   /**
134    * Extract a subsection of an Array.
135    * 
136    * @param xs the input Array
137    * @param start the index in the original array to start our section, inclusive
138    * @param limit the index in the original array to stop at, inclusive
139    * @return the new Array
140    */
141   public static Object[] section(Object[] xs, int start, int limit) {
142     Object[] xs_ = (Object[])java.lang.reflect.Array.newInstance(
143                        xs.getClass().getComponentType(), limit - start);
144     System.arraycopy(xs, start, xs_, 0, xs_.length);
145     return xs_;
146   }
147 
148   /**
149    * Lookup the first instance of an Object in an Array.
150    * 
151    * @param xs the Array to search
152    * @param x the Object to check
153    * @return the index of the first <code>equal</code> object
154    */
155   public static int indexOf(Object[] xs, Object x) {
156     for (int i = 0; i < xs.length; ++i)
157       if (xs[i].equals(x)) return i;
158     return -1;
159   }
160 
161   /**
162    * Whether an Array contains an Object.
163    * 
164    * @param xs the Array to search
165    * @param x the Object to check
166    * @return whether it is there or not
167    */
168   public static boolean contains(Object[] xs, Object x) {
169     return indexOf(xs, x) != -1;
170   }
171 }