I'm currently writing a genetic algorithm solving the traveling salesman problem. There are some "constants" I use at multiple places. Those values, however, need to be precalculated, thus, I can't store them into a private static final variable. Hence, I decided to use an Enum.
public enum Constants {
NODE_COUNT(0),
SEQUENCE_LENGTH(0),
POPULATION_SIZE(0),
MAX_EDGE_WEIGHT(0),
MUTATION_RATE(0);
private int value;
private boolean alreadySet = false;
Constants(int value) {
this.value = value;
}
public void setValue(int value) {
if (!alreadySet) {
this.value = value;
this.alreadySet = true;
} else {
throw new AssertionError("Value is already set.");
}
}
public int get() {
return value;
}
}
My question is, do you consider this a good approach? I'm not sure whether this lowers the cohesion of each class using the enum. Thanks in advance.