// 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();
}