1

I have the following class, which is using a switch statement and I'd like to replace it with an enum.

public class FillTable {

    private static final int NAME_INDEX = 0;
    private static final int DESCRIPTION_INDEX = 1;
    private static final int CONTRIBUTION_INDEX = 2;

    public Object getValueAt(int row, int col) {
        EmployeeData employeeData = (EmployeeData)items.get(row);

        switch (col) {
            case NAME_INDEX: {
                return employeeData.getName();
            }
            case DESCRIPTION_INDEX: {
                return employeeData.getDescription();
            }
            case ADDRESS_INDEX: {
                return employeeData.getAddress();
            }
            default: {
                return "";
            }
        }
    }

}

Here is the enum that I've come up with.

public enum EmployeeTableColumn {

    NAME_INDEX {
        @Override
        public void getData() {
            employeeData.getName();
        }
    }, DESCRIPTION_INDEX {
        @Override
        public void getData() {
            return employeeData.getDescription();
        }
    }, CONTRIBUTION_INDEX {
        @Override
        public void getData() {
            return employeeData.getAddress();
        }
    };

    public abstract void getData();

}

My problem is that I don't know how to replace the code in the getValueAt() method to make use of the enum in place of the switch statement. Can someone please show me how I can do this?

Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
Steve B
  • 557
  • 2
  • 7
  • 23
  • Give this answer a look: https://stackoverflow.com/questions/6391777/switch-on-enum-in-java Also, here is a nice example from the Java docs: https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html – Nathan Jan 30 '19 at 15:37

1 Answers1

2

If you want the EmployeeTableColumn enum to control how you extract information from an EmployeeData object, then you need a method in EmployeeTableColumn that takes your EmployeeData as a parameter and returns the extracted information.

public enum EmployeeTableColumn {
    NAME {
        @Override
        public Object getData(EmployeeData data) {
            return data.getName();
        }
    },
    DESCRIPTION {
        @Override
        public Object getData(EmployeeData data) {
            return data.getDescription();
        }
    },
    CONTRIBUTION {
        @Override
        public Object getData(EmployeeData data) {
            return data.getAddress();
        }
    };

    public abstract Object getData(EmployeeData data);
}

Then you can write a method that uses an EmployeeTableColumn to extract the correct data.

public Object getValueAt(int row, int col) {
    EmployeeData employeeData = (EmployeeData)items.get(row);

    // however you are going to pick the right column object
    EmployeeTableColumn employeeColumn = EmployeeTableColumn.values()[col];

    return employeeColumn.getData(employeeData);
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • That worked, thanks! However, I don't understand how col can be mapped to either NAME_INDEX, DESCRIPTION_INDEX or CONTRIBUTION_INDEX? For example, there's no NAME_INDEX = 0 – Steve B Jan 30 '19 at 16:47
  • @SteveB `EmployeeTableColumn.values()` returns an array, ararys start with index 0, since `NAME_INDEX` is the first value in the enum this is located in `.values()[0]`, and `DESCRIPTION_INDEX` is located in `.values()[1]` etc.. – Mark Jan 30 '19 at 17:11
  • @Mark, how would it work if for example the enum values weren't 0, 1 and 2? For example if NAME_INDEX was 246 and DESCRIPTION_INDEX was 877 – Steve B Jan 30 '19 at 19:01
  • @SteveB You can take a look at [Can I set enum start value in Java?](https://stackoverflow.com/questions/1067352/can-i-set-enum-start-value-in-java), that'll answer your question – Mark Jan 30 '19 at 20:32