You don't need polymorphism you can just use a class that combines the two values and a Map to omit the switch statement.
public class GraphType {
// made the fields public final and omitted the getters for simplicity.
// add getters if needed
public final int periodValue;
public final int numberOfDataPoints;
GraphType(int periodValue, int numberOfDataPoints) {
this.periodValue = periodValue;
this.numberOfDataPoints = numberOfDataPoints;
}
}
then build the map
Map<String, GraphType> graphTypeByName = new HashMap<>();
graphTypeByName.put("1D",new GraphType(300, 289));
graphTypeByName.put("5D",new GraphType(1800, 241));
graphTypeByName.put("1M",new GraphType(86400, 31);
graphTypeByName.put("1Y",new GraphType(259200, 123);
and use the map
public GraphType getNumberOfDataPoints(String selectedGraphType) {
GraphType graphType = graphTypeByName.get(selectedGraphType);
if(graphType == null) {
// handle graphType not registered. Default value or exception ?
}
return graphType;
}
This method is more flexible than using an enum. The default case is also easier to test. I think that the GraphType is not an enum. An enum is a set of fixed names that are unlikely to change, like NORH, SOUTH, EAST, WEST or HOUR, MINUTES, SECONDS.
If you want to ensure that a only the GraphTypes that you want exist and no others can be instantiated, implement a registry like this:
public class GraphTypeRegistry {
public class GraphType {
public final int periodValue;
public final int numberOfDataPoints;
private GraphType(int periodValue, int numberOfDataPoints) {
this.periodValue = periodValue;
this.numberOfDataPoints = numberOfDataPoints;
}
}
private Map<String, GraphType> graphTypeByName = new HashMap<>();
public GraphTypeRegistry(){
graphTypeByName.put("1D",new GraphType(300, 289));
graphTypeByName.put("5D",new GraphType(1800, 241));
graphTypeByName.put("1M",new GraphType(86400, 31);
graphTypeByName.put("1Y",new GraphType(259200, 123);
}
public GraphType getGraphType(String graphTypeName) {
GraphType graphType = graphTypeByName.get(graphTypeName);
if(graphType == null) {
// handle graphType not registered. Default value or exception ?
}
return graphType;
}
}