001: /*
002: * Convenience methods for working with Java arrays.
003: * Copyright (C) 2005 Stephen Ostermiller
004: * http://ostermiller.org/contact.pl?regarding=Java+Utilities
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * See COPYING.TXT for details.
017: */
018: package com.Ostermiller.util;
019:
020: import java.io.IOException;
021: import java.lang.reflect.Array;
022:
023: /**
024: * Convenience methods for working with Java arrays.
025: * More information about this class is available from <a target="_top" href=
026: * "http://ostermiller.org/utils/ArrayHelper.html">ostermiller.org</a>.
027: *
028: * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
029: * @since ostermillerutils 1.06.00
030: */
031: public final class ArrayHelper {
032:
033: /**
034: * Concatenates the given arrays into a single long array.
035: * The returned array will be of the common super type of the
036: * two given arrays.
037: * <p>
038: * For example it can be called like this:<br>
039: * <pre>
040: * String[] arr = (String[])ArrayHelper.cat(new String[]{"one","two"}, new String[]{"three","four"});
041: * </pre>
042: *
043: * @param arr1 first array
044: * @param arr2 second array
045: * @return an array whose length is the sum of the given array's lengths and contains all the elements in the given arrays.
046: * @since ostermillerutils 1.06.00
047: */
048: public static Object[] cat(Object[] arr1, Object[] arr2) {
049: // Use reflection to find the super class of both arrays
050: Class<?> commonSuperClass = Object.class;
051: boolean foundcommonSuperClass = false;
052: for (Class<?> c1 = arr1.getClass().getComponentType(); !foundcommonSuperClass
053: && !c1.equals(Object.class); c1 = c1.getSuperclass()) {
054: for (Class<?> c2 = arr2.getClass().getComponentType(); !foundcommonSuperClass
055: && !c2.equals(Object.class); c2 = c2
056: .getSuperclass()) {
057: if (c2.equals(c1)) {
058: foundcommonSuperClass = true;
059: commonSuperClass = c1;
060: }
061: }
062: }
063: // Create a new array of the correct type
064: Object[] result = (Object[]) Array.newInstance(
065: commonSuperClass, arr1.length + arr2.length);
066: // Copy the two arrays into the large array
067: System.arraycopy(arr1, 0, result, 0, arr1.length);
068: System.arraycopy(arr2, 0, result, arr1.length, arr2.length);
069: return result;
070: }
071:
072: /**
073: * Prints out a comma separated list of all the objects
074: * in the array on a single line in CSV format.
075: *
076: * @param arr Array to print in comma separated value format
077: * @since ostermillerutils 1.06.00
078: */
079: public static void print(Object[] arr) {
080: try {
081: CSVPrinter csvp = new CSVPrinter(System.out);
082: for (Object element : arr) {
083: csvp.write(element.toString());
084: }
085: csvp.writeln();
086: csvp.flush();
087: } catch (IOException iox) {
088: iox.printStackTrace(System.err);
089: }
090: }
091:
092: /**
093: * Tests two arrays to see if the arrays are equal.
094: * Two arrays will be equal only if they are the same length
095: * and contain objects that are equal in the same order.
096: *
097: * @param arr1 first array
098: * @param arr2 second array
099: * @return true iff two arguments are equal
100: * @since ostermillerutils 1.06.00
101: */
102: public static boolean equal(Object[] arr1, Object[] arr2) {
103: if (arr1 == null && arr2 == null)
104: return true;
105: if (arr1 == null || arr2 == null)
106: return false;
107: if (arr1.length != arr2.length)
108: return false;
109: for (int i = 0; i < arr1.length; i++) {
110: if (!equalObjects(arr1[i], arr2[i]))
111: return false;
112: }
113: return true;
114: }
115:
116: /**
117: * Tests if the two objects are equal
118: *
119: * @param o1 first object
120: * @param o2 second object
121: * @since ostermillerutils 1.06.00
122: */
123: private static boolean equalObjects(Object o1, Object o2) {
124: if (o1 == null && o2 == null)
125: return true;
126: if (o1 == null || o2 == null)
127: return false;
128: return o1.equals(o2);
129: }
130: }
|