I had to subclass ResourceBundle due to our specific needs.
However, to override getKeys(), I am a bit in trouble. This getKeys needs to somehow concatenate from a map of underlying ResourceBundles. How can I do that?
Thanks
EDIT: While submitting I came across an idea. Basically we have for each of our Module a ResourceBundle, so my code looks like this so far:
public Enumeration<String> getKeys() {
ArrayList<String> keys = new ArrayList<String>();
for (Map.Entry<Module, ResourceBundle> entry : internalMap.entrySet()) {
Enumeration<String> tmp = entry.getValue().getKeys();
while (tmp.hasMoreElements()) {
String key = tmp.nextElement();
keys.add(key);
}
}
return Collections.enumeration(keys);
}
The simplest approach is just to manually enumerate over the enumerations, dump them into a collection, then return an enumeration of that collection.
Failing that, you could combine Google Guava operations to do this, something like this:
// the map of enumerations
Map<?, Enumeration<String>> map = ...
// a function that turns enumerations into iterators
Function<Enumeration<String>, Iterator<String>> eumerationToIteratorFunction = new Function<Enumeration<String>, Iterator<String>>() {
public Iterator<String> apply(Enumeration<String> enumeration) {
return Iterators.forEnumeration(enumeration);
}
};
// transform the enumerations into iterators, using the function
Collection<Iterator<String>> iterators = Collections2.transform(map.values(), eumerationToIteratorFunction);
// combine the iterators
Iterator<String> combinedIterator = Iterators.concat(iterators);
// convert the combined iterator back into an enumeration
Enumeration<String> combinedEnumeration = Iterators.asEnumeration(combinedIterator);
It's pretty cumbersome, but Enumeration is old and rather poorly supported in modern APIs. It can be slimmed down a bit by judicious use of static imports. You can even do the whole thing in a single function-style statement:
Map<?, Enumeration<String>> map = ...
Enumeration<String> combinedEnumeration = asEnumeration(concat(
transform(map.values(), new Function<Enumeration<String>, Iterator<String>>() {
public Iterator<String> apply(Enumeration<String> enumeration) {
return forEnumeration(enumeration);
}
})
));
This approach has the benefit of being efficient - it does everything lazily, and won't iterate until you ask it to. Efficiency may not matter in your case, though, in which case just do it the simple way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With