I am trying to build a generic method which gets an enum
as an argument and processes some information.
But unfortunately, it doesn't work as I would like. Maybe you have some suggestions to improve my code and prevent the errors.
private static ProductSearchColumnRange extractAllColumnRangeFromEnum(ProductSearchColumn column,
Enum someEnum) {
return ProductSearchColumnRange.builder()
.column(column)
.range(Arrays.stream(someEnum.values())
.map(element -> new RangeOption(element.getValue(), element.name()))
.collect(Collectors.toList()))
.build();
}
I get the following error on the someEnum.values()
call:
Cannot resolve method
values()
inEnum
I get the following errors on the element.getValue()
and element.name()
calls:
Cannot resolve method
getValue()
Cannot resolve method
name()
The enums I need to use in the method extractAllColumnRangeFromEnum()
above all have the same structure.
For example, one of them look like this:
public enum DeploymentModelType {
PRIVATE("Private"),
HYBRID("Hybrid"),
PUBLIC("Public");
String value;
DeploymentModelType(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
I know that I can prevent the error by using
private static ProductSearchColumnRange extractAllColumnRangeFromEnum(ProductSearchColumn column,
DeploymentModelType someEnum) {
...
}
But then my method would not be generic. I want to use this method for any of my enums.