Config enum pattern
Posted 2009-05-04.This is something I stumbled across while browsing the source for Hive (Hadoop).
I normally define my properties and default values using something like this:
private static final String CLASSIFIER_MODEL_LOC_PROP = "models.classifier"; private static final String CLASSIFIER_MODEL_LOC_DEFAULT = "config/classifier.ser";
It works well enough, but it's a little long, and it is sort of repetition -- maybe in one place in the code, I read the value with a different default. Here's what someone did in Hive:
public static enum ConfVars { // QL execution stuff SCRIPTWRAPPER("hive.exec.script.wrapper", null), PLAN("hive.exec.plan", null), SCRATCHDIR("hive.exec.scratchdir", "/tmp/"+System.getProperty("user.name")+"/hive"), SUBMITVIACHILD("hive.exec.submitviachild", false), SCRIPTERRORLIMIT("hive.exec.script.maxerrsize", 100000), ... }
I generally like enums. Maybe because at a previous company someone thought objects were too hard and everything should be stored in hashmaps, and if you needed a value, you need to know the name of it, and god help you if you need to search for all uses. So I think I will use this next time I do anything dealing with configuration.