A lot of code for a little task - ColumnRearranger


I want to create the specs for an output table and I'm not sure whether there is any shorter solution. The output table should contain two columns of the input table, only if these columns were selected by the user in the configuration dialog. Further, it should get two new double columns. The code looks like this:

protected DataTableSpec createOutSpecStats(DataTableSpec inSpec) {
        ColumnRearranger columnRearranger = new ColumnRearranger(inSpec);

        String aggColumn = ((SettingsModelString)getModelSetting(CFG_AGGR)).getStringValue();
        String refColumn = ((SettingsModelString)getModelSetting(CFG_REFCOLUMN)).getStringValue();
        boolean useRobustStats =
        String[] statColumnNames = new String[]{"mean","standard deviation"};
        DataType[] statColumnTypes = new DataType[]{DoubleCell.TYPE,DoubleCell.TYPE};

        if(aggColumn != null && refColumn != null) columnRearranger.keepOnly(aggColumn, refColumn);
        else {
            if(refColumn != null) columnRearranger.keepOnly(refColumn);
            else if(aggColumn != null) columnRearranger.keepOnly(aggColumn);
            else {
                return new DataTableSpec("Statistics", statColumnNames, statColumnTypes);

        List<DataColumnSpec> cSpecs = new ArrayList<DataColumnSpec>();

        UniqueNameGenerator uniqueNames = new UniqueNameGenerator(columnRearranger.createSpec());
        DataColumnSpec[] cSpecArray = new DataColumnSpec[2];
        DataColumnSpecCreator dcsc = uniqueNames.newCreator(statColumnNames[0], statColumnTypes[0]);
        cSpecArray[0] = dcsc.createSpec();
        dcsc = uniqueNames.newCreator(statColumnNames[1], statColumnTypes[1]);
        cSpecArray[1] = dcsc.createSpec();

        StatsCellFactory factory = new StatsCellFactory(cSpecArray);


        return columnRearranger.createSpec();