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?

1

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

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
@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, 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
@SteveB You can take a look at Can I set enum start value in Java?, that'll answer your question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.