I’ve been doing heavy refactoring over the last 48 hours to cope with epic VMware fail. One part of that has been the necessity to do something pretty tricky, and as it turns out, Apache Commons has a massive shortcut I thought I’d share with the rest of you.
Imagine there are multiple tree branch paths and let KX equal the terminating value:
I needed to merge these paths so that I ended up with one hierarchy:
A | |--B |-- K2 | |--C | |--D |--K1 |--K3
I knew how to do this merge with linear performance, but I was trying to come up with a solution that was more optimal. It turns out Apache already has one, and it’s called Hierarchical Configuration. Basically you just do the following:
import org.apache.commons.configuration.HierarchicalConfiguration; HierarchicalConfiguration hc = new HierarchicalConfiguration(); hc.addProperty(“A.B.C.D”, “K1”); hc.addProperty(“A.B”, “K2”); hc.addProperty(“D”, “K3”);
The object will create new branches if the paths do not yet exist, but if they do, the values will simply be inserted into the appropriate location. This has been a massive timesaver and hopefully you all will find it useful as well.