Let's say I have a structure like this:
Is it possible to create an enum that will return the string value of selected cell? For example:
enum.GROUP_MAIN1.SUBGROUP1.COL1
will return value "COL1".
I was looking for nested enums but didn't find the solution to this situation.
You can do this with such trick:
public interface GROUPMAIN1 {
enum SUBGROUP1 implements GROUPMAIN1 {
COL1,
COL2,
COL3
}
enum SUBGROUP2 implements GROUPMAIN1 {
COL3,
COL4
}
}
So to get enum you will need to use GROUPMAIN1.SUBGROUP1.COL1
.
It can also be done in another way if all you need is just a string constants:
public interface GROUPMAIN1 {
interface SUBGROUP1 {
String COL1 = "COL1";
String COL2 = "COL2";
}
interface SUBGROUP2 {
String COL3 = "COL3";
String COL4 = "COL4";
}
}
You cannot have everything you ask for. Here’s one way of getting some of it:
enum MainGroup { GROUP_MAIN1, GROUP_MAIN2 };
enum Subgroup {
SUBGROUP1(MainGroup.GROUP_MAIN1), SUBGROUP2(MainGroup.GROUP_MAIN1),
SUBGROUP3(MainGroup.GROUP_MAIN2), SUBGROUP4(MainGroup.GROUP_MAIN2);
MainGroup main;
private Subgroup(MainGroup main) {
this.main = main;
}
public MainGroup getMainGroup() {
return main;
}
}
enum Col {
COL1(Subgroup.SUBGROUP1), COL2(Subgroup.SUBGROUP1), COL3(Subgroup.SUBGROUP2), COL4(Subgroup.SUBGROUP2),
COL5(Subgroup.SUBGROUP3), COL6(Subgroup.SUBGROUP3), COL7(Subgroup.SUBGROUP4), COL8(Subgroup.SUBGROUP4);
Subgroup sub;
private Col(Subgroup sub) {
this.sub = sub;
}
public MainGroup getMainGroup() {
return sub.getMainGroup();
}
public Subgroup getSubgroup() {
return sub;
}
}
You may also implement a method in MainGroup
to find all subgroups under that main group, and the same for subgroups and cols.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With