* if sorting is not supported for the input array.
*/
public void fire() throws IllegalActionException {
super.fire();
if (input.hasToken(0)) {
ArrayToken token = (ArrayToken) input.get(0);
Type inputElementType = token.getElementType();
if (token.length() == 0) {
output.send(0, token);
return;
}
boolean ascendingValue = ((BooleanToken) ascending.getToken())
.booleanValue();
ArrayToken result = null;
try {
if (ascendingValue) {
result = UtilityFunctions.sort(token);
} else {
result = UtilityFunctions.sortDescending(token);
}
} catch (ClassCastException ex) {
// The call to sort() above throws ClassCastException if
// sorting is not supported. This is not ideal, so we
// remap this to IllegalActionException.
throw new IllegalActionException(this, ex.getMessage());
}
boolean allowDuplicatesValue = ((BooleanToken) allowDuplicates
.getToken()).booleanValue();
if (!allowDuplicatesValue) {
// Strip out duplicates.
ArrayList list = new ArrayList();
Token previous = result.getElement(0);
list.add(previous);
for (int i = 1; i < result.length(); i++) {
Token next = result.getElement(i);
if (!next.isEqualTo(previous).booleanValue()) {
list.add(next);
previous = next;
}
}
// Dummy array to give the run-time type to toArray().
Token[] dummy = new Token[0];
result = new ArrayToken(inputElementType, (Token[]) list
.toArray(dummy));
}
output.send(0, result);
}