Reputation: 1417
I have an Enum that handles columns in a table. It is in production.
public enum Column {
NAME("m_Name", "Name", "TEXT"), ADDRESS("m_Address", "Address", "TEXT");
private String value;
private String label;
private String sortType;
private Column(String value, String label, String sortType) {
this.setValue(value);
this.setLabel(label);
this.setSortType(sortType);
}
}
I now need to add custom columns that I will need to be dynamic. I realise that I can't make enums dynamic. What is the alternative?
Upvotes: 3
Views: 11150
Reputation: 65889
If you adjust Column
to implement an interface
and instead of using Column
elsewhere use the interface
you can then grow dynamic columns using an ordinary class.
interface DbColumn {
String getLabel();
String getValue();
}
public enum Column implements DbColumn {
NAME("m_Name", "Name"), ADDRESS("m_Address", "Address");
private final String value;
private final String label;
private Column(String value, String label) {
this.value = value;
this.label = label;
}
@Override
public String getLabel() {
return label;
}
@Override
public String getValue() {
return value;
}
}
public class DynamicColumn implements DbColumn {
private final String value;
private final String label;
private DynamicColumn(String value, String label) {
this.value = value;
this.label = label;
}
@Override
public String getLabel() {
return label;
}
@Override
public String getValue() {
return value;
}
}
Upvotes: 6