001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.shared;
034:
035: import com.flexive.shared.exceptions.FxInvalidParameterException;
036:
037: import java.lang.reflect.Array;
038: import java.util.*;
039:
040: /**
041: * Utility functions for arrays.
042: *
043: * @author Gregor Schober (gregor.schober@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
044: * @version $Rev: 181 $
045: */
046: public class FxArrayUtils {
047:
048: /**
049: * Converts a SelectableObjectWithLabel array to a string list containing the id's.
050: *
051: * @param values the array
052: * @param separator the separator to use between the id's
053: * @return the array as string list
054: */
055: public static String toSeparatedList(
056: SelectableObjectWithLabel[] values, char separator) {
057: if (values == null)
058: return "";
059: StringBuilder res = new StringBuilder(values.length * 5);
060: for (int i = 0; i < values.length; i++) {
061: if (i > 0)
062: res.append(separator);
063: res.append(values[i].getId());
064: }
065: return res.toString();
066: }
067:
068: /**
069: * Converts a int array to a string list.
070: *
071: * @param values the array
072: * @param separator the separator to use between the numbers
073: * @return the array as string list
074: */
075: public static String toSeparatedList(int[] values, char separator) {
076: if (values == null)
077: return "";
078: StringBuilder res = new StringBuilder(values.length * 5);
079: for (int i = 0; i < values.length; i++) {
080: if (i > 0)
081: res.append(separator);
082: res.append(values[i]);
083: }
084: return res.toString();
085: }
086:
087: /**
088: * Converts a byte array to a string list.
089: *
090: * @param values the array
091: * @param separator the separator to use between the numbers
092: * @return the array as string list
093: */
094: public static String toSeparatedList(byte[] values, char separator) {
095: if (values == null)
096: return "";
097: StringBuilder res = new StringBuilder(values.length * 5);
098: for (int i = 0; i < values.length; i++) {
099: if (i > 0)
100: res.append(separator);
101: res.append(values[i]);
102: }
103: return res.toString();
104: }
105:
106: /**
107: * Converts a long array to a string list.
108: *
109: * @param values the array
110: * @param separator the separator to use between the numbers
111: * @return the array as string list
112: */
113: public static String toSeparatedList(long[] values, char separator) {
114: if (values == null)
115: return "";
116: StringBuilder res = new StringBuilder(values.length * 5);
117: for (int i = 0; i < values.length; i++) {
118: if (i > 0)
119: res.append(separator);
120: res.append(values[i]);
121: }
122: return res.toString();
123: }
124:
125: /**
126: * Join a list of elements using the given delimiter.
127: *
128: * @param elements Elements to be joined
129: * @param delim Delimiter between elements
130: * @return Joined string
131: */
132: public static String toSeparatedList(List elements, String delim) {
133: StringBuilder out = new StringBuilder();
134: boolean first = true;
135: for (Object s : elements) {
136: out.append(((first ? "" : delim) + s));
137: first = false;
138: }
139: return out.toString();
140: }
141:
142: /**
143: * Join an array of elements using the given delimiter.
144: *
145: * @param elements Elements to be joined
146: * @param delim Delimiter between elements
147: * @return Joined string
148: */
149: public static String toSeparatedList(String[] elements, String delim) {
150: StringBuilder out = new StringBuilder();
151: for (int i = 0; i < elements.length; i++)
152: out.append(((i > 0 ? delim : "") + elements[i]));
153: return out.toString();
154: }
155:
156: /**
157: * Converts a list with items separated by a specific delimeter to a array.
158: * <p/>
159: * A empty array will be returned if the list is null or a empty string.
160: *
161: * @param list the list
162: * @param separator the separator character
163: * @return the array
164: */
165: public static String[] toArray(String list, char separator) {
166: if (list == null || list.length() == 0)
167: return new String[0];
168: StringTokenizer st = new StringTokenizer(list,
169: ("" + separator), false);
170: ArrayList<String> al = new ArrayList<String>(100);
171: while (st.hasMoreTokens()) {
172: al.add(st.nextToken());
173: }
174: return al.toArray(new String[al.size()]);
175: }
176:
177: /**
178: * Converts a list with integer items separated by a specific delimeter to a array.
179: * <p/>
180: * A empty array will be returned if the list is null or a empty string.
181: *
182: * @param list the list
183: * @param separator the separator character
184: * @return the array
185: * @throws FxInvalidParameterException if the list can not be converted, the exception's getParameterName()
186: * function will return the token that caused the exception
187: */
188: public static int[] toIntArray(String list, char separator)
189: throws FxInvalidParameterException {
190: if (list == null || list.length() == 0)
191: return new int[0];
192: String sInts[] = toArray(list, separator);
193: int iInts[] = new int[sInts.length];
194: for (int i = 0; i < sInts.length; i++) {
195: try {
196: iInts[i] = Integer.parseInt(sInts[i]);
197: } catch (Exception exc) {
198: throw new FxInvalidParameterException(
199: "'"
200: + list
201: + "' can not be converted to a int[] array using separator '"
202: + separator + "'", sInts[i]);
203: }
204: }
205: return iInts;
206: }
207:
208: public static int[] toIntArray(Integer list[]) {
209: if (list == null || list.length == 0) {
210: return new int[0];
211: }
212: int result[] = new int[list.length];
213: int pos = 0;
214: for (Integer ele : list) {
215: result[pos++] = ele;
216: }
217: return result;
218: }
219:
220: /**
221: * Converts a list with long items separated by a specific delimeter to a array.
222: * <p/>
223: * A empty array will be returned if the list is null or a empty string.
224: *
225: * @param list the list
226: * @param separator the separator character
227: * @return the array
228: * @throws FxInvalidParameterException if the list can not be converted, the exception's getParameterName()
229: * function will return the token that caused the exception
230: */
231: public static long[] toLongArray(String list, char separator)
232: throws FxInvalidParameterException {
233: if (list == null || list.length() == 0)
234: return new long[0];
235: String sInts[] = toArray(list, separator);
236: long iInts[] = new long[sInts.length];
237: for (int i = 0; i < sInts.length; i++) {
238: try {
239: iInts[i] = new Long(sInts[i]).intValue();
240: } catch (Exception exc) {
241: throw new FxInvalidParameterException(
242: "'"
243: + list
244: + "' can not be converted to a long[] array using separator '"
245: + separator + "'", sInts[i]);
246: }
247: }
248: return iInts;
249: }
250:
251: /**
252: * Removes the given element from the list.
253: *
254: * @param list the list the element should be removed from
255: * @param element the element to remove
256: * @return the list without the given element
257: */
258: public static int[] removeElementFromList(int[] list,
259: final int element) {
260:
261: if (list != null) {
262: int elementFound = 0;
263: while (elementFound != -1) {
264: // Look for the elemenet
265: elementFound = -1;
266: for (int i = 0; i < list.length; i++) {
267: if (list[i] == element) {
268: elementFound = i;
269: break;
270: }
271: }
272: // Delete the element
273: if (elementFound != -1) {
274: int tmp[] = new int[list.length - 1];
275: int pos = 0;
276: for (int i = 0; i < list.length; i++) {
277: if (i != elementFound)
278: tmp[pos++] = list[i];
279: }
280: list = tmp;
281: }
282: }
283: }
284: return list;
285:
286: }
287:
288: /**
289: * Removes the given elements from the list.
290: *
291: * @param list the list the element should be removed from
292: * @param elements the elements to remove
293: * @return the list without the given element
294: */
295: public static int[] removeElementsFromList(int[] list,
296: final int elements[]) {
297: for (int ele : elements) {
298: list = removeElementFromList(list, ele);
299: }
300: return list;
301: }
302:
303: /**
304: * Removes dupicated entries from the list.
305: *
306: * @param list the list
307: * @return the list without any duplicated entries
308: */
309: public static int[] removeDuplicates(int[] list) {
310: if (list == null || list.length == 0) {
311: return new int[0];
312: }
313: Hashtable<Integer, Boolean> tbl = new Hashtable<Integer, Boolean>(
314: list.length);
315: for (int ele : list) {
316: tbl.put(ele, Boolean.FALSE);
317: }
318: int[] result = new int[tbl.size()];
319: int pos = 0;
320: for (Enumeration e = tbl.keys(); e.hasMoreElements();) {
321: result[pos++] = (Integer) e.nextElement();
322: }
323: return result;
324: }
325:
326: /**
327: * Removes dupicated entries from the list.
328: *
329: * @param list the list
330: * @return the list without any duplicated entries
331: */
332: public static long[] removeDuplicates(long[] list) {
333: if (list == null || list.length == 0) {
334: return new long[0];
335: }
336: Hashtable<Long, Boolean> tbl = new Hashtable<Long, Boolean>(
337: list.length);
338: for (long ele : list) {
339: tbl.put(ele, Boolean.FALSE);
340: }
341: long[] result = new long[tbl.size()];
342: int pos = 0;
343: for (long element : Collections.list(tbl.keys())) {
344: result[pos++] = element;
345: }
346: return result;
347: }
348:
349: /**
350: * Adds a element to the end of the array, if it is not already contained.
351: *
352: * @param list original list
353: * @param element the element to add
354: * @return the new list
355: */
356: public static int[] addElement(int[] list, int element) {
357: // Avoid null pointer exception
358: if (list == null)
359: list = new int[0];
360:
361: // Determine if the element is already part of the list.
362: // If so do nothing
363: for (int aList : list)
364: if (aList == element)
365: return list;
366:
367: // Add the element to the end of the list
368: int[] result = new int[list.length + 1];
369: System.arraycopy(list, 0, result, 0, list.length);
370: result[result.length - 1] = element;
371: return result;
372: }
373:
374: /**
375: * Adds a element to the end of the array, if it is not already contained.
376: *
377: * @param list original list
378: * @param element the element to add
379: * @return the new list
380: */
381: public static long[] addElement(long[] list, long element) {
382: // Avoid null pointer exception
383: if (list == null)
384: list = new long[0];
385:
386: // Determine if the element is already part of the list.
387: // If so do nothing
388: for (long aList : list)
389: if (aList == element)
390: return list;
391:
392: // Add the element to the end of the list
393: long[] result = new long[list.length + 1];
394: System.arraycopy(list, 0, result, 0, list.length);
395: result[result.length - 1] = element;
396: return result;
397: }
398:
399: /**
400: * Adds a element to the end of the array.
401: *
402: * @param list original list
403: * @param element the element to add
404: * @param unique true if the element should only be added if it is not already contained
405: * @return the new list
406: */
407: @SuppressWarnings("unchecked")
408: public static <T> T[] addElement(T[] list, T element, boolean unique) {
409: // Avoid null pointer exception
410: if (list == null) {
411: if (element == null) {
412: return null;
413: }
414: list = (T[]) Array.newInstance(element.getClass(), 1);
415: list[0] = element;
416: return list;
417: }
418:
419: if (unique) {
420: // Determine if the element is already part of the list.
421: // If so do nothing
422: for (T aList : list)
423: if (aList == element)
424: return list;
425: }
426:
427: // Add the element to the end of the list
428: T[] result = (T[]) Array.newInstance(element.getClass(),
429: list.length + 1);
430: System.arraycopy(list, 0, result, 0, list.length);
431: result[result.length - 1] = element;
432: return result;
433: }
434:
435: /**
436: * Adds a element to the end of the array.
437: *
438: * @param list original list
439: * @param element the element to add
440: * @return the new list
441: */
442: public static <T> T[] addElement(T[] list, T element) {
443: return addElement(list, element, false);
444: }
445:
446: /**
447: * Adds every element in a list to the end of the array, if it is not already contained.
448: *
449: * @param elements the elements to add
450: * @param list the list to add the element to
451: * @return the new list
452: */
453: public static int[] addElements(int[] list, int[] elements) {
454: if (elements == null)
455: return list;
456: for (int element : elements)
457: list = addElement(list, element);
458: return list;
459: }
460:
461: /**
462: * Return true if the list contains the given element.
463: *
464: * @param list the list
465: * @param element the element to look for
466: * @return true if the list contains the given element
467: */
468: public static boolean containsElement(int[] list, int element) {
469: if (list == null)
470: return false;
471: for (int aList : list)
472: if (aList == element)
473: return true;
474: return false;
475: }
476:
477: /**
478: * Return true if the list contains the given element.
479: *
480: * @param list the list
481: * @param element the element to look for
482: * @return true if the list contains the given element
483: */
484: public static boolean containsElement(SelectableObject[] list,
485: SelectableObject element) {
486: if (list == null)
487: return false;
488: for (SelectableObject aList : list)
489: if (aList.getId() == element.getId())
490: return true;
491: return false;
492: }
493:
494: /**
495: * Return true if the list contains the given element.
496: *
497: * @param list the list
498: * @param element the element to look for
499: * @return true if the list contains the given element
500: */
501: public static boolean containsElement(byte[] list, byte element) {
502: if (list == null)
503: return false;
504: for (byte aList : list)
505: if (aList == element)
506: return true;
507: return false;
508: }
509:
510: /**
511: * Return true if the list contains the given element.
512: *
513: * @param list the list
514: * @param element the element to look for
515: * @param ignoreCase if set to false the compare is done case sensitive
516: * @return true if the list contains the given element
517: */
518: public static boolean containsElement(String[] list,
519: String element, boolean ignoreCase) {
520: if (list == null)
521: return false;
522: for (String aList : list) {
523: if (aList == null && element == null)
524: return true;
525: if (aList == null)
526: return false;
527: if (element == null)
528: return false;
529: if (ignoreCase) {
530: if (aList.equalsIgnoreCase(element))
531: return true;
532: } else {
533: if (aList.equals(element))
534: return true;
535: }
536: }
537: return false;
538: }
539:
540: /**
541: * Returns the first occurence of the given element.
542: *
543: * @param list the list
544: * @param element the element to look for
545: * @param ignoreCase if set to false the compare is done case sensitive
546: * @return true the first position, or -1 if the element was not found
547: */
548: public static int indexOf(String[] list, String element,
549: boolean ignoreCase) {
550: if (list == null)
551: return -1;
552: int pos = 0;
553: for (String aList : list) {
554: if (aList == null && element == null)
555: return pos;
556: if (aList == null)
557: return -1;
558: if (element == null)
559: return -1;
560: if (ignoreCase) {
561: if (aList.equalsIgnoreCase(element))
562: return pos;
563: } else {
564: if (aList.equals(element))
565: return pos;
566: }
567: pos++;
568: }
569: return -1;
570: }
571:
572: /**
573: * Return true if the list contains the given element.
574: *
575: * @param list the list
576: * @param element the element to look for
577: * @return true if the list contains the given element
578: */
579: public static boolean containsElement(long[] list, long element) {
580: for (long aList : list)
581: if (aList == element)
582: return true;
583: return false;
584: }
585:
586: /**
587: * Generic shallow array clone function (until commons ArrayUtils is generified)
588: *
589: * @param <T> array type
590: * @param array the array to be cloned (shallow copy)
591: * @return the cloned array (shallow copy)
592: */
593: public static <T> T[] clone(T[] array) {
594: if (array == null) {
595: return null;
596: }
597: return array.clone();
598: }
599: }
|