View Javadoc

1   /*
2    * Copyright (c) 2004, RV Test Team
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * Redistributions of source code must retain the above copyright notice, this
9    * list of conditions and the following disclaimer.
10   *
11   * Redistributions in binary form must reproduce the above copyright notice,
12   * this list of conditions and the following disclaimer in the documentation
13   * and/or other materials provided with the distribution.
14   *
15   * Neither the name of the "RV Test Team" nor the names of its contributors may
16   * be used to endorse or promote products derived from this software without
17   * specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29   * THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   */
32  /*
33   * Copyright (c) 2002, Reuters
34   * All rights reserved.
35   *
36   * Redistribution and use in source and binary forms, with or without
37   * modification, are permitted provided that the following conditions are met:
38   *
39   * Redistributions of source code must retain the above copyright notice, this
40   * list of conditions and the following disclaimer.
41   * Redistributions in binary form must reproduce the above copyright notice,
42   * this list of conditions and the following disclaimer in the documentation
43   * and/or other materials provided with the distribution.
44   * Neither the name of the Reuters nor the names of its contributors may be
45   * used to endorse or promote products derived from this software without
46   * specific prior written permission.
47   *
48   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
49   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
52   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
56   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
58   * THE POSSIBILITY OF SUCH DAMAGE.
59   *
60   */
61  package com.reuters.msgtest;
62  
63  import java.lang.reflect.Array;
64  
65  
66  /***
67   *
68   * @author <a href="mailto:mark.pollack@reuters.com">Mark Pollack </a>
69   * @version @VERSION@ change history 12/03/01 - added toArray function - B.Roy
70   *          12/13/01 - added parseArrayStringTokens function - B.Roy
71   */
72  public class ArrayUtility {
73      /***
74       * Array Manipulation java tech tips ref
75       * http://developer.java.sun.com/servlet/PrintPageServlet?url=http%3A//developer.java.sun.com/developer/TechTips/2000/tt0815.html
76       *
77       * @param array
78       *            input array of primitive values
79       * @return Array of Objects whose type corresponds to the primitive types in
80       *         the input array
81       */
82      public static Object[] toArray(Object array) {
83          if (array == null) {
84              return null;
85          }
86  
87          Class clazz = array.getClass();
88  
89          if (!clazz.isArray()) { // if not an array or elements not primitive, return
90  
91              return null;
92          }
93  
94          if (!clazz.getComponentType().isPrimitive()) {
95              return null;
96          }
97  
98          // get array length and create Object output array
99          int length = Array.getLength(array);
100         Object[] newArray = new Object[length];
101 
102         // wrap and copy elements
103         for (int i = 0; i < length; i++) {
104             newArray[i] = Array.get(array, i);
105         }
106 
107         return newArray;
108     }
109 
110     /***
111      * generic method for parsing a String , extracting a set of tokens and
112      * populating a primitive array.
113      *
114      * @param primitiveArray
115      *            array of primitive values
116      * @param values
117      *            String array containing primitive values
118      */
119     public static void buildPrimitiveArray(Object primitiveArray,
120         String[] values) {
121         for (int i = 0; i < values.length; i++) {
122             String value = values[i];
123 
124             if (primitiveArray instanceof byte[]) {
125                 byte[] array = (byte[]) primitiveArray;
126                 array[i] = Byte.parseByte(value);
127             } else if (primitiveArray instanceof short[]) {
128                 short[] array = (short[]) primitiveArray;
129                 array[i] = Short.parseShort(value);
130             } else if (primitiveArray instanceof int[]) {
131                 int[] array = (int[]) primitiveArray;
132                 array[i] = Integer.parseInt(value);
133             } else if (primitiveArray instanceof long[]) {
134                 long[] array = (long[]) primitiveArray;
135                 array[i] = Long.parseLong(value);
136             } else if (primitiveArray instanceof float[]) {
137                 float[] array = (float[]) primitiveArray;
138                 array[i] = Float.parseFloat(value);
139             } else if (primitiveArray instanceof double[]) {
140                 double[] array = (double[]) primitiveArray;
141                 array[i] = Double.parseDouble(value);
142             }
143         }
144     }
145 }