I have an enum and POJO classes in JAVA. In enum class, each enum value matches with variables of POJO classes... And then I want to create a relationship between two classes.
Enum Class:
public enum MyEnum
{
FIELD1,
FIELD2,
FIELD3,
...
}
POJO Class:
public class MyPojo
{
private String field1;
private String field2_not_ref;
private String field3;
...
}
Then, when I try to match these fields of two classes, I've implemented code like:
public String matchMethod(MyEnum myenum)
{
switch(myenum)
{
case FIELD1:
return field1;
case FIELD2:
return field2_not_ref;
...
}
}
I think that it is not a good/clear solution. Is there any suggestion?
To avoid reflection, which might be slow and make things more concise, than with defining method on an enum, you can create an enum with a field that is a function consuming MyPojo and returning String:
public enum MyEnum {
FIELD1(MyPojo::getField1),
FIELD2(MyPojo::getField2),
FIELD3(MyPojo::getField3);
private final Function<MyPojo, String> getField;
MyEnum(Function<MyPojo, String> getField) {
this.getField = getField;
}
}
Then you can call it in the following way:
public static void main(String[] args) {
MyPojo myPojo = new MyPojo("f1", "f2", "f3");
System.out.println(MyEnum.FIELD2.getGetField().apply(myPojo));
}
If you don't want to use it as a Function variable with apply method, you can create a single function on en enum, that does it:
public enum MyEnum {
FIELD1(MyPojo::getField1),
FIELD2(MyPojo::getField2),
FIELD3(MyPojo::getField3);
private final Function<MyPojo, String> getField;
MyEnum(Function<MyPojo, String> getField) {
this.getField = getField;
}
String getFieldFromMyPojo(MyPojo myPojo) { return getField.apply(myPojo); }
}
And invoke it like that:
public static void main(String[] args) {
MyPojo myPojo = new MyPojo("f1", "f2", "f3");
System.out.println(MyEnum.FIELD2.getFieldFromMyPojo(myPojo));
}
Getters and setters were omitted for brevity.
There is one more way:
Define an abstract method in enum and override for every enum field:
public enum MyEnum {
FIELD1 {
@Override
public String getFromPojo(MyPojo myPojo) {
return myPojo.getField1();
}
},
FIELD2 {
@Override
public String getFromPojo(MyPojo myPojo) {
return myPojo.getField2();
}
},
FIELD3 {
@Override
public String getFromPojo(MyPojo myPojo) {
return myPojo.getField3();
}
};
public abstract String getFromPojo(MyPojo myPojo);
}
For class MyPojo define getters for fields:
Define also method matchMethod in a way that it will handle the responsibility to the enum (if you wish, this is not mandatory, since enum can resolve the field by itself).
public class MyPojo {
private String field1;
private String field2;
private String field3;
public MyPojo(String field1, String field2, String field3) {
this.field1 = field1;
this.field2 = field2;
this.field3 = field3;
}
public String getField1() {
return field1;
}
public String getField2() {
return field2;
}
public String getField3() {
return field3;
}
public String matchMethod(MyEnum myEnum) {
return myEnum.getFromPojo(this);
}
}
Now in "main" method you can use the following approach:
MyPojo p1 = new MyPojo("p1", "p2", "p3");
MyPojo p2 = new MyPojo("k1", "k2", "k3");
System.out.println(MyEnum.FIELD1.getFromPojo(p1));
System.out.println(MyEnum.FIELD2.getFromPojo(p1));
System.out.println(MyEnum.FIELD3.getFromPojo(p1));
System.out.println(MyEnum.FIELD1.getFromPojo(p2));
System.out.println(MyEnum.FIELD2.getFromPojo(p2));
System.out.println(MyEnum.FIELD3.getFromPojo(p2));
// in addition, if you've defined 'matchMethod' on POJO
System.out.println(p1.matchMethod(MyEnum.FIELD1));
System.out.println(p1.matchMethod(MyEnum.FIELD2));
System.out.println(p1.matchMethod(MyEnum.FIELD3));
System.out.println(p2.matchMethod(MyEnum.FIELD1));
System.out.println(p2.matchMethod(MyEnum.FIELD2));
System.out.println(p2.matchMethod(MyEnum.FIELD3));
This prints:
p1
p2
p3
k1
k2
k3
// and optionally...
p1
p2
p3
k1
k2
k3
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