I have an object formatted like so
public static class ActionTypes { private Punch punch; private Kick kick; public static class Punch { private Integer force; } public static class Kick { private Integer force; } }
and given a String parameter like punch
or kick
, I want to grab its corresponding force
. What’s the best way to implement this?
Advertisement
Answer
public static class ActionTypes { private final Map<String, Force> map = new HashMap<>(); public void setPunch(Punch punch) { map.put("punch", punch) } public void setKick(Kick kick) { map.put("kick", kick); } public Force getForce(String id) { return map.get(id.toLowerCase()); } public static interface Force { Integer getForce(); } public static class Punch implements Punch { private Integer force; public Integer getForce() { return force; } } public static class Kick implements Punch { private Integer force; public Integer getForce() { return force; } } }