boolean group = false;
boolean calculations = false;
boolean sort = false;
for (int i=0; i<operationList.size(); i++) {
DataSetOp op = operationList.get(i);
if (DataSetOpType.GROUP.equals(op.getType())) {
if (calculations) throw new IllegalStateException("Group not permitted after a simple function calculation operation.");
if (sort) throw new IllegalStateException("Sort operations must be applied ALWAYS AFTER GROUP.");
DataSetGroup gOp = (DataSetGroup) op;
ColumnGroup columnGroup = gOp.getColumnGroup();
if (columnGroup == null) {
// No real group requested. Only function calculations on the data set.
calculations = true;
context.lastOperation = op;
} else {
if (group(gOp, context)) {
// The group will be required if is not an interval selection
group = context.lastGroupOp.getSelectedIntervalNames().isEmpty();
context.lastOperation = op;
}
}
}
else if (DataSetOpType.FILTER.equals(op.getType())) {
if (calculations) throw new IllegalStateException("Filter not permitted after a simple function calculation operation.");
if (group) throw new IllegalStateException("Filter operations must be applied ALWAYS BEFORE GROUP.");
if (sort) throw new IllegalStateException("Sort operations must be applied ALWAYS AFTER FILTER.");
filter((DataSetFilter) op, context);
context.lastOperation = op;
}
else if (DataSetOpType.SORT.equals(op.getType())) {
if (calculations) throw new IllegalStateException("Sort not permitted after a function calculation operation.");
if (sort) throw new IllegalStateException("Sort can only be executed once.");
if (group) {
buildDataSet(context);
}
sort = true;
sort((DataSetSort) op, context);
context.lastOperation = op;
}
else {
throw new IllegalArgumentException("Unsupported operation: " + op.getClass().getName());
}
}
// Build the resulting data set
buildDataSet(context);