|
28 | 28 |
|
29 | 29 | /** |
30 | 30 | * Loader for the Java Money JSR configuration. |
31 | | - * |
| 31 | + * |
32 | 32 | * @author Anatole Tresch |
33 | 33 | */ |
34 | | -public final class MonetaryConfig{ |
| 34 | +public final class MonetaryConfig { |
| 35 | + |
| 36 | + private static final Logger LOG = Logger |
| 37 | + .getLogger(MonetaryConfig.class.getName()); |
35 | 38 |
|
36 | | - private static final Logger LOG = Logger |
37 | | - .getLogger(MonetaryConfig.class.getName()); |
| 39 | + private static final MonetaryConfig INSTANCE = new MonetaryConfig(); |
38 | 40 |
|
39 | | - private static final MonetaryConfig INSTANCE = new MonetaryConfig(); |
40 | | - |
41 | | - private Map<String, String> config = new HashMap<>(); |
42 | | - private Map<String, Integer> priorities = new HashMap<>(); |
| 41 | + private Map<String, String> config = new HashMap<>(); |
| 42 | + private Map<String, Integer> priorities = new HashMap<>(); |
43 | 43 |
|
44 | | - private MonetaryConfig() { |
45 | | - try { |
46 | | - Enumeration<URL> urls = getClass().getClassLoader().getResources( |
47 | | - "javamoney.properties"); |
48 | | - while (urls.hasMoreElements()) { |
49 | | - URL url = urls.nextElement(); |
50 | | - try { |
51 | | - Properties props = new Properties(); |
52 | | - props.load(url.openStream()); |
53 | | - updateConfig(props); |
54 | | - } catch (Exception e) { |
55 | | - LOG.log(Level.SEVERE, |
56 | | - "Error loading javamoney.properties, ignoring " |
57 | | - + url, e); |
58 | | - } |
59 | | - } |
60 | | - } catch (IOException e) { |
61 | | - LOG.log(Level.SEVERE, "Error loading javamoney.properties.", e); |
62 | | - } |
63 | | - } |
| 44 | + private MonetaryConfig() { |
| 45 | + try { |
| 46 | + Enumeration<URL> urls = getClass().getClassLoader().getResources( |
| 47 | + "javamoney.properties"); |
| 48 | + while (urls.hasMoreElements()) { |
| 49 | + URL url = urls.nextElement(); |
| 50 | + try { |
| 51 | + Properties props = new Properties(); |
| 52 | + props.load(url.openStream()); |
| 53 | + updateConfig(props); |
| 54 | + } catch (Exception e) { |
| 55 | + LOG.log(Level.SEVERE, |
| 56 | + "Error loading javamoney.properties, ignoring " |
| 57 | + + url, e); |
| 58 | + } |
| 59 | + } |
| 60 | + } catch (IOException e) { |
| 61 | + LOG.log(Level.SEVERE, "Error loading javamoney.properties.", e); |
| 62 | + } |
| 63 | + } |
64 | 64 |
|
65 | | - private void updateConfig(Properties props) { |
66 | | - for (Map.Entry<Object, Object> en : props.entrySet()) { |
67 | | - String key = en.getKey().toString(); |
68 | | - String value = en.getValue().toString(); |
69 | | - Integer existingPrio = priorities.get(key); |
70 | | - int prio = 0; |
71 | | - if (value.startsWith("{prio=")) { |
72 | | - int index = value.indexOf('}'); |
73 | | - if (index > 0) { |
74 | | - String prioString = value.substring("{prio=".length(), |
75 | | - index); |
76 | | - value = value.substring(index + 1); |
77 | | - prio = Integer.parseInt(prioString); |
78 | | - priorities.put(key, prio); |
79 | | - } |
80 | | - } |
81 | | - if (Objects.isNull(existingPrio)) { |
82 | | - config.put(key, value); |
83 | | - } else if (existingPrio < prio) { |
84 | | - config.put(key, value); |
85 | | - } else if (existingPrio == prio) { |
86 | | - throw new IllegalStateException( |
87 | | - "AmbiguousConfiguration detected for '" + key + "'."); |
88 | | - } |
89 | | - // else ignore entry with lower prio! |
90 | | - } |
91 | | - } |
| 65 | + private void updateConfig(Properties props) { |
| 66 | + for (Map.Entry<Object, Object> en : props.entrySet()) { |
| 67 | + String key = en.getKey().toString(); |
| 68 | + String value = en.getValue().toString(); |
| 69 | + Integer existingPrio = priorities.get(key); |
| 70 | + int prio = 0; |
| 71 | + if (value.startsWith("{prio=")) { |
| 72 | + int index = value.indexOf('}'); |
| 73 | + if (index > 0) { |
| 74 | + String prioString = value.substring("{prio=".length(), |
| 75 | + index); |
| 76 | + value = value.substring(index + 1); |
| 77 | + prio = Integer.parseInt(prioString); |
| 78 | + priorities.put(key, prio); |
| 79 | + } |
| 80 | + } |
| 81 | + if (Objects.isNull(existingPrio)) { |
| 82 | + config.put(key, value); |
| 83 | + } else if (existingPrio < prio) { |
| 84 | + config.put(key, value); |
| 85 | + } else if (existingPrio == prio) { |
| 86 | + throw new IllegalStateException( |
| 87 | + "AmbiguousConfiguration detected for '" + key + "'."); |
| 88 | + } |
| 89 | + // else ignore entry with lower prio! |
| 90 | + } |
| 91 | + } |
92 | 92 |
|
93 | | - public static Map<String, String> getConfig() { |
94 | | - return Collections.unmodifiableMap(INSTANCE.config); |
95 | | - } |
| 93 | + public static Map<String, String> getConfig() { |
| 94 | + return Collections.unmodifiableMap(INSTANCE.config); |
| 95 | + } |
96 | 96 |
|
97 | 97 | } |
0 commit comments