/** * Returns a formatted string for the given <code>Integer</code> array. The array is first sorted * and then formatted like "5, 1, 7". Ranges of consecutive numbers are also consolidated, so the * array "1,3,4,5" would be turned into the string "1, 3-5". * * @param array the <code>Integer</code> array to format into a string * @return a string representation of the <code>Integer</code> array * @aribaapi documented */ public static String getStringValue(Integer[] array) { // return empty string if array is null or empty if (ArrayUtil.nullOrEmptyArray(array)) { return (""); } FastStringBuffer buf = new FastStringBuffer(); // copy the array int count = array.length; Integer[] integers = new Integer[count]; System.arraycopy(array, 0, integers, 0, count); // sort the ints Compare compare = Formatter.getFormatterForType(Constants.IntegerType); Sort.objects(integers, compare); // turn the array into a string boolean inRange = false; Integer previous = null; for (int i = 0; i < count; i++) { Integer current = integers[i]; if (i > 0) { // start a range if the current value is one more than the // previous value if (current.intValue() == previous.intValue() + 1) { inRange = true; previous = current; // allow code below to handle range at end of array if (i < count - 1) { continue; } } // either the current value is not one more than the // previous, or we're at the end of the array if (inRange) { buf.append("-"); buf.append(previous.toString()); inRange = false; // bail out if we're closing a range at the end of the // array, otherwise fall through to the code below if (previous == current) { break; } } } // add the current value to the buffer, with a comma if // it's not the first value in the array if (i != 0) { buf.append(", "); } buf.append(current.toString()); // keep track of the current value for next time... previous = current; } return buf.toString(); }
/** * Returns a formatted string for the given <code>int</code> array. The array is first sorted and * then formatted like "5, 1, 7". Ranges of consecutive numbers are also consolidated, so the * array "1,3,4,5" would be turned into the string "1, 3-5". * * @param array the <code>int</code> array to format into a string * @return a string representation of the <code>int</code> array * @aribaapi documented */ public static String getStringValue(int[] array) { // return empty string if array is null or empty if (ArrayUtil.nullOrEmptyIntArray(array)) { return (""); } // convert ints to Integers int count = array.length; Integer[] integers = new Integer[count]; for (int i = 0; i < count; ++i) { integers[i] = Constants.getInteger(array[i]); } return (getStringValue(integers)); }