Suppose I constructed the following class
public enum OptimizationAlgorithmType {
VANILLA(new HashMap<String, Double>()) {
private final static String ETA = "eta";
private Double eta = null;
public OptimizationAlgorithmType setEta(Double eta) {
this.eta = eta;
return this;
}
@Override
public Map<String, Double> getHyperarameters() {
this.map.put(ETA, eta);
return this.map;
}
},
MOMENTUM(new HashMap<String, Double>()) {
private final static String ALPHA = "alpha";
private final static String BETA = "beta";
private Double alpha = null;
private Double beta = null;
public OptimizationAlgorithmType setAlpha(Double alpha) {
this.alpha = alpha;
return this;
}
public OptimizationAlgorithmType setBeta(Double beta) {
this.beta = beta;
return this;
}
@Override
public Map<String, Double> getHyperarameters() {
this.map.put(ALPHA, alpha);
this.map.put(BETA, beta);
return this.map;
}
};
protected Map<String, Double> map;
private OptimizationAlgorithmType(Map<String, Double> map) {
this.map = map;
}
public abstract Map<String, Double> getHyperarameters();
}
My goal was to construct an API that when I select a specif Enum then different methods would be available. For example
MultiThreadBackpropagation backpropagation = new MultiThreadBackpropagation(feedForward)
.setNumberThreads(10)
.setBatch(5)
.setEpochs(100)
.setOptimizer(OptimizationAlgorithmType.VANILLA.setEta(0.001));
Or
MultiThreadBackpropagation backpropagation = new MultiThreadBackpropagation(feedForward)
.setNumberThreads(10)
.setBatch(5)
.setEpochs(100)
.setOptimizer(OptimizationAlgorithmType.MOMENTUM.setAlpha(0.01).setBeta(0.99));
Unfortunately, this is not allowed. The ide warns about the unused methods (i.e.: setEta())- and the methods are not available at all to select from the specific enum.
Is there a trick I can use to get the desired API?
Thanks
Edit Added an alternative answer below