Lets sat we have following hierarchy of constants that I would like to hard code..
FRUIT->APPLE->GREEN_APPLE->"Fruit is apple of green color"
FRUIT->APPLE->RED_APPLE->"Fruit is apple of red color"
FRUIT->Cherry->RED_CHERRY->"Fruit is cherry of red color"
FLOWER->ROSE->RED_ROSE->"Flower is rose of red color"
FLOWER->ROSE->PINK_ROSE->"Flower is rose of pink color"
Given an input, say FRUIT,APPLE,RED_APPLE, I would like to get "Fruit is apple of red color" as output.
How can I maintain this hierarchy and support the look up? Can it be done using Enums?? or something else?
CodePudding user response:
You can use a nested enum
to String
map.
enum E { FRUIT, FLOWER, VEGETABLE}
enum F { APPLE, Cherry, ROSE}
enum G { GREEN_APPLE, RED_APPLE, RED_CHERRY, RED_ROSE, PINK_CHERRY}
static void add(Map<E, Map<F, Map<G, String>>> map, E e, F f, G g, String s) {
map.computeIfAbsent(e, k -> new HashMap<>())
.computeIfAbsent(f, k -> new HashMap<>())
.put(g, s);
}
static String get(Map<E, Map<F, Map<G, String>>> map, E e, F f, G g) {
return Optional.of(map.get(e))
.map(m -> m.get(f))
.map(m -> m.get(g))
.orElse(null);
}
and
Map<E, Map<F, Map<G, String>>> map = new HashMap<>();
add(map, E.FRUIT, F.APPLE, G.GREEN_APPLE, "Fruit is apple of green color");
add(map, E.FRUIT, F.APPLE, G.RED_APPLE, "Fruit is apple of red color");
add(map, E.FRUIT, F.Cherry, G.RED_CHERRY, "Fruit is cherry of red color");
add(map, E.FLOWER, F.ROSE, G.RED_ROSE, "Flower is rose of red color");
add(map, E.VEGETABLE, F.ROSE, G.PINK_CHERRY, "Flower is rose of pink color");
System.out.println(get(map, E.FRUIT, F.APPLE, G.GREEN_APPLE));
output:
Fruit is apple of green color
CodePudding user response:
This can be done by using enum
and interface
.
public interface Product {
String getDescription();
}
public interface Fruit extends Product{ }
public interface Flower extends Product{}
public enum Apple implements Fruit{
GREEN_APPLE("Fruit is apple of green color"),
RED_APPLE("Fruit is apple of red color");
private final String description;
Apple(String description) {
this.description = description;
}
@Override
public String getDescription() {
return description;
}
}
public enum Cherry implements Fruit{
RED_CHERRY("Fruit is cherry of red color");
private final String description;
Cherry(String description) {
this.description = description;
}
@Override
public String getDescription() {
return description;
}
}
public enum Rose implements Flower{
RED_ROSE("Flower is rose of red color"),
PINK_CHERRY("Flower is rose of pink color");
private final String description;
Rose(String description) {
this.description=description;
}
@Override
public String getDescription() {
return description;
}
}
public class Main {
public static void main(String[] args) {
print(Apple.GREEN_APPLE);
print(Apple.RED_APPLE);
print(Cherry.RED_CHERRY);
print(Rose.RED_ROSE);
print(Rose.PINK_CHERRY);
}
public static void print(Product product){
System.out.println(product.getDescription());
}
}
Output
Fruit is apple of green color
Fruit is apple of red color
Fruit is cherry of red color
Flower is rose of red color
Flower is rose of pink color
Reference - https://www.baeldung.com/java-extending-enums#emulate-extensible-enums-with-interfaces