You may create class abstract class IntervalAction
:
public abstract class IntervalAction {
private final double minBound;
private final double maxBound;
public IntervalAction(double minBound, double maxBound) {
if (minBound < 0) throw new IllegalArgumentException("minBound >= 0");
if (maxBound > 1) throw new IllegalArgumentException("maxBound <= 0");
if (minBound > maxBound) throw new IllegalArgumentException("maxBound >= minBound");
this.minBound = minBound;
this.maxBound = maxBound;
}
public abstract void execute();
@Override
public String toString() {
return "IntervalAction{" +
"minBound=" + minBound +
", maxBound=" + maxBound +
'}';
}
}
and registry class which will have all available actions:
public class ActionRegistry {
public void register(IntervalAction action) {
if (intersectsWithExistingRange(action))
throw new IllegalArgumentException("Already have action in that range: " + action);
// todo registration in internal data structure
}
private boolean intersectsWithExistingRange(IntervalAction action) {
// todo
return true;
}
public void execute(double input) {
IntervalAction action = find(input);
if (action == null) throw new IllegalArgumentException("No action found for " + input);
action.execute();
}
private IntervalAction find(double input) {
// todo
return null;
}
}
And a driver class:
public class ActionDriver {
private final ActionRegistry registry;
private final Random random;
public ActionDriver() {
random = new Random(System.currentTimeMillis());
registry = new ActionRegistry();
registry.register(new IntervalAction(0, 0.1) {
@Override
public void execute() {
System.out.println(this);
}
});
registry.register(new IntervalAction(0.1, 0.2) {
@Override
public void execute() {
System.out.println(this);
}
});
// so on
}
public void act() {
registry.execute(random.nextDouble());
}
}
Now, the most interesting part is how can you implement internal data structure in ActionRegistry
to hold (and search for) your actions -- all of these todos
in above code sample. For that I'll refer you to this SO question.