001: /*
002: * Copyright 2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package com.icesoft.faces.util;
018:
019: import java.lang.reflect.Array;
020:
021: /**
022: * Utility class for managing arrays
023: *
024: * @author Anton Koinov (latest modification by $Author: grantsmith $)
025: * @version $Revision: 169655 $ $Date: 2005-05-11 12:45:06 -0400 (Wed, 11 May
026: * 2005) $
027: */
028: public class ArrayUtils {
029: public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
030: public static final String[] EMPTY_STRING_ARRAY = new String[0];
031:
032: //~ Constructors -------------------------------------------------------------------------------
033:
034: protected ArrayUtils() {
035: // hide from public access
036: }
037:
038: //~ Methods ------------------------------------------------------------------------------------
039:
040: public static Class commonClass(Class c1, Class c2) {
041: if (c1 == c2) {
042: return c1;
043: }
044:
045: if ((c1 == Object.class) || c1.isAssignableFrom(c2)) {
046: return c1;
047: }
048:
049: if (c2.isAssignableFrom(c1)) {
050: return c2;
051: }
052:
053: if (c1.isPrimitive() || c2.isPrimitive()) {
054: // REVISIT: we could try to autoconvert to Object or something appropriate
055: throw new IllegalArgumentException("incompatible types "
056: + c1 + " and " + c2);
057: }
058:
059: // REVISIT: we could try to find a common supper class or interface
060: return Object.class;
061: }
062:
063: /**
064: * Concatenates two arrays into one. If arr1 is null or empty, returns arr2.
065: * If arr2 is null or empty, returns arr1. May return null if both arrays
066: * are null, or one is empty and the other null. <br> The concatenated array
067: * has componentType which is compatible with both input arrays (or
068: * Object[])
069: *
070: * @param arr1 input array
071: * @param arr2 input array
072: * @return Object the concatenated array, elements of arr1 first
073: */
074: public static Object concat(Object arr1, Object arr2) {
075: int len1 = (arr1 == null) ? (-1) : Array.getLength(arr1);
076:
077: if (len1 <= 0) {
078: return arr2;
079: }
080:
081: int len2 = (arr2 == null) ? (-1) : Array.getLength(arr2);
082:
083: if (len2 <= 0) {
084: return arr1;
085: }
086:
087: Class commonComponentType = commonClass(arr1.getClass()
088: .getComponentType(), arr2.getClass().getComponentType());
089: Object newArray = Array.newInstance(commonComponentType, len1
090: + len2);
091: System.arraycopy(arr1, 0, newArray, 0, len1);
092: System.arraycopy(arr2, 0, newArray, len1, len2);
093:
094: return newArray;
095: }
096:
097: /**
098: * Concatenates arrays into one. Any null or empty arrays are ignored. If
099: * all arrays are null or empty, returns null. Elements will be ordered in
100: * the order in which the arrays are supplied.
101: *
102: * @param arrs array of arrays
103: * @return the concatenated array
104: */
105: public static Object concat(Object[] arrs) {
106: int totalLen = 0;
107: Class commonComponentType = null;
108: for (int i = 0, len = arrs.length; i < len; i++) {
109: // skip all null arrays
110: if (arrs[i] == null) {
111: continue;
112: }
113:
114: int arrayLen = Array.getLength(arrs[i]);
115:
116: // skip all empty arrays
117: if (arrayLen == 0) {
118: continue;
119: }
120:
121: totalLen += arrayLen;
122:
123: Class componentType = arrs[i].getClass().getComponentType();
124: commonComponentType = (commonComponentType == null) ? componentType
125: : commonClass(commonComponentType, componentType);
126: }
127:
128: if (commonComponentType == null) {
129: return null;
130: }
131:
132: return concat(Array.newInstance(commonComponentType, totalLen),
133: totalLen, arrs);
134: }
135:
136: public static Object concat(Object toArray, int totalLen,
137: Object[] arrs) {
138: if (totalLen == 0) {
139: // Should we allocate an empty array instead?
140: return toArray;
141: }
142:
143: if (totalLen > Array.getLength(toArray)) {
144: toArray = Array.newInstance(toArray.getClass()
145: .getComponentType(), totalLen);
146: }
147:
148: for (int i = 0, len = arrs.length, offset = 0; i < len; i++) {
149: final Object arr = arrs[i];
150: if (arr != null) {
151: int arrayLen = Array.getLength(arr);
152: if (arrayLen > 0) {
153: System.arraycopy(arr, 0, toArray, offset, arrayLen);
154: offset += arrayLen;
155: }
156: }
157: }
158:
159: return toArray;
160: }
161:
162: public static Object concat(Object arr1, Object arr2, Object arr3) {
163: return concat(new Object[] { arr1, arr2, arr3 });
164: }
165:
166: public static Object concat(Object arr1, Object arr2, Object arr3,
167: Object arr4) {
168: return concat(new Object[] { arr1, arr2, arr3, arr4 });
169: }
170:
171: public static Object concat(Object arr1, Object arr2, Object arr3,
172: Object arr4, Object arr5) {
173: return concat(new Object[] { arr1, arr2, arr3, arr4, arr5 });
174: }
175:
176: public static Object concatSameType(Object toArray, Object[] arrs) {
177: int totalLen = 0;
178: for (int i = 0, len = arrs.length; i < len; i++) {
179: if (arrs[i] != null) {
180: totalLen += Array.getLength(arrs[i]);
181: }
182: }
183:
184: return concat(toArray, totalLen, arrs);
185: }
186:
187: public static boolean contains(Object[] array, Object value) {
188: if (array == null || array.length == 0) {
189: return false;
190: }
191:
192: for (int i = 0; i < array.length; i++) {
193: Object o = array[i];
194: if ((o == null && value == null)
195: || (o != null && o.equals(value))) {
196: return true;
197: }
198: }
199:
200: return false;
201: }
202: }
|