|
21 | 21 | import java.util.Set; |
22 | 22 |
|
23 | 23 | @Slf4j |
24 | | -public abstract class Config implements Serializable { |
| 24 | +public class Config implements Serializable { |
25 | 25 | private Map<String, Object> data; |
26 | 26 |
|
27 | | - public static final String DEFAULT_CONFIGURATION_NAME = "bullet_defaults.yaml"; |
28 | | - private String defaultConfiguration; |
29 | | - |
30 | 27 | /** |
31 | | - * Constructor that loads specific file augmented with defaults and the name of the default configuration file. |
32 | | - * |
| 28 | + * Constructor that loads a specific file and loads the settings in that file. |
33 | 29 | * |
34 | 30 | * @param file YAML file to load. |
35 | | - * @param defaultConfigurationFile Default YAML file to load. |
36 | 31 | * @throws IOException if an error occurred with the file loading. |
37 | 32 | */ |
38 | | - public Config(String file, String defaultConfigurationFile) throws IOException { |
39 | | - this.defaultConfiguration = defaultConfigurationFile; |
40 | | - data = loadConfigResource(file); |
| 33 | + public Config(String file) throws IOException { |
| 34 | + data = readYAML(file); |
| 35 | + log.info("Configuration: {} ", data); |
41 | 36 | } |
42 | 37 |
|
43 | 38 | /** |
44 | | - * Constructor that loads specific file augmented with defaults. |
| 39 | + * Constructor that loads specific file augmented with defaults and the name of the default configuration file. |
45 | 40 | * |
46 | 41 | * @param file YAML file to load. |
| 42 | + * @param defaultConfigurationFile Default YAML file to load. |
47 | 43 | * @throws IOException if an error occurred with the file loading. |
48 | 44 | */ |
49 | | - public Config(String file) throws IOException { |
50 | | - this(file, DEFAULT_CONFIGURATION_NAME); |
51 | | - } |
52 | | - |
53 | | - /** |
54 | | - * Default constructor. |
55 | | - * |
56 | | - * @throws IOException if an error occurred with loading the default config. |
57 | | - */ |
58 | | - public Config() throws IOException { |
59 | | - this(null); |
| 45 | + public Config(String file, String defaultConfigurationFile) throws IOException { |
| 46 | + this(defaultConfigurationFile); |
| 47 | + // Override |
| 48 | + Map<String, Object> specificConf = readYAML(file); |
| 49 | + data.putAll(specificConf); |
| 50 | + log.info("Final configuration: {} ", data); |
60 | 51 | } |
61 | 52 |
|
62 | 53 | /** |
@@ -119,22 +110,24 @@ public void set(String key, Object value) { |
119 | 110 | data.put(key, value); |
120 | 111 | } |
121 | 112 |
|
| 113 | + /** |
| 114 | + * Merges another Config into this one. |
| 115 | + * |
| 116 | + * @param other The other {@link Config} to merge into this one. |
| 117 | + */ |
| 118 | + public void merge(Config other) { |
| 119 | + if (other != null) { |
| 120 | + data.putAll(other.data); |
| 121 | + } |
| 122 | + } |
| 123 | + |
122 | 124 | /** |
123 | 125 | * Clears out the configuration. |
124 | 126 | */ |
125 | 127 | public void clear() { |
126 | 128 | data.clear(); |
127 | 129 | } |
128 | 130 |
|
129 | | - private Map<String, Object> loadConfigResource(String yamlFile) throws IOException { |
130 | | - Map<String, Object> defaultconf = readYAML(defaultConfiguration); |
131 | | - Map<String, Object> specificConf = readYAML(yamlFile); |
132 | | - // Override |
133 | | - defaultconf.putAll(specificConf); |
134 | | - log.info("Final configuration: {} ", defaultconf); |
135 | | - return defaultconf; |
136 | | - } |
137 | | - |
138 | 131 | /** |
139 | 132 | * Reads a YAML file containing mappings and returns them as a {@link Map}. |
140 | 133 | * |
|
0 commit comments