01: package edu.umd.cs.findbugs.config;
02:
03: import java.util.ArrayList;
04: import java.util.Collections;
05: import java.util.Enumeration;
06: import java.util.List;
07: import java.util.Properties;
08: import java.util.Set;
09:
10: /**
11: * @author pugh
12: */
13: public final class SortedProperties extends Properties {
14: /**
15: * Overriden to be able to write properties sorted by keys to the disk
16: * @see java.util.Hashtable#keys()
17: */
18: @SuppressWarnings("unchecked")
19: @Override
20: public synchronized Enumeration<Object> keys() {
21: // sort elements based on detector (prop key) names
22: Set set = keySet();
23: return (Enumeration<Object>) sortKeys(set);
24: }
25:
26: /**
27: * To be compatible with version control systems, we need to sort properties before
28: * storing them to disk. Otherwise each change may lead to problems by diff against
29: * previous version - because Property entries are randomly distributed (it's a map).
30: *
31: * @param keySet non null set instance to sort
32: * @return non null list wich contains all given keys, sorted lexicographically.
33: * The list may be empty if given set was empty
34: */
35: static public Enumeration<?> sortKeys(Set<String> keySet) {
36: List<String> sortedList = new ArrayList<String>();
37: sortedList.addAll(keySet);
38: Collections.sort(sortedList);
39: return Collections.enumeration(sortedList);
40: }
41: }
|