Skip to content

Commit ee162a8

Browse files
authored
Merge pull request #4 from Maps-Messaging/development
expose the number of processors so it can be used as a configuration
2 parents ff5ef46 + 56490d7 commit ee162a8

File tree

9 files changed

+137
-72
lines changed

9 files changed

+137
-72
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<modelVersion>4.0.0</modelVersion>
2525
<groupId>io.mapsmessaging</groupId>
2626
<artifactId>configuration_library</artifactId>
27-
<version>1.1.0</version>
27+
<version>1.1.1-SNAPSHOT</version>
2828
<packaging>jar</packaging>
2929

3030
<name>Configuration API</name>

src/main/java/io/mapsmessaging/configuration/ConfigurationProperties.java

Lines changed: 83 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import static io.mapsmessaging.logging.LogMessages.PROPERTY_MANAGER_ENTRY_LOOKUP;
3535
import static io.mapsmessaging.logging.LogMessages.PROPERTY_MANAGER_ENTRY_LOOKUP_FAILED;
3636

37+
@SuppressWarnings("java:S3740")
3738
public class ConfigurationProperties {
3839

3940
private final Logger logger = LoggerFactory.getLogger(ConfigurationProperties.class);
@@ -54,8 +55,8 @@ public ConfigurationProperties(Map<String, Object> inMap) {
5455
putAll(inMap);
5556

5657
Object globalObject = inMap.get("global");
57-
if (globalObject instanceof ConfigurationProperties) {
58-
this.global = (ConfigurationProperties) globalObject;
58+
if (globalObject instanceof ConfigurationProperties globConf) {
59+
this.global = globConf;
5960
}
6061
}
6162

@@ -64,9 +65,9 @@ public Object get(String key) {
6465
if (val == null && global != null) {
6566
val = global.get(key);
6667
}
67-
if (val instanceof JsonObject) {
68+
if (val instanceof JsonObject jsonObject) {
6869
Type type = new TypeToken<Map<String, Object>>() {}.getType();
69-
Map<String, Object> map1 = SystemProperties.getInstance().getGson().fromJson((JsonObject) val, type);
70+
Map<String, Object> map1 = SystemProperties.getInstance().getGson().fromJson(jsonObject, type);
7071
return new ConfigurationProperties(map1);
7172
}
7273
return val;
@@ -94,6 +95,46 @@ public String getProperty(String key, String defaultValue) {
9495
return null;
9596
}
9697

98+
public int getThreadCount(String key, int defaultValue) {
99+
String value = getProperty(key, String.valueOf(defaultValue)).trim();
100+
101+
if (value.toLowerCase().contains("{processors}")) {
102+
int threads = Runtime.getRuntime().availableProcessors();
103+
104+
int plus = value.lastIndexOf('+');
105+
int minus = value.lastIndexOf('-');
106+
int mult = value.lastIndexOf('*');
107+
int div = value.lastIndexOf('/');
108+
109+
int operatorIndex = Math.max(Math.max(plus, minus), Math.max(mult, div));
110+
111+
if (operatorIndex > -1 && operatorIndex < value.length() - 1) {
112+
char operator = value.charAt(operatorIndex);
113+
int operand = Integer.parseInt(value.substring(operatorIndex + 1).trim());
114+
115+
switch (operator) {
116+
case '/': threads = threads / operand; break;
117+
case '*': threads = threads * operand; break;
118+
case '+': threads = threads + operand; break;
119+
case '-': threads = threads - operand; break;
120+
default: break;
121+
}
122+
}
123+
124+
if (threads < 1) {
125+
threads = 1;
126+
}
127+
return threads;
128+
}
129+
130+
int dot = value.indexOf('.');
131+
if (dot >= 0) {
132+
value = value.substring(0, dot).trim();
133+
}
134+
return Integer.parseInt(value);
135+
}
136+
137+
97138
public boolean getBooleanProperty(String key, boolean defaultValue) {
98139
return asBoolean(get(key, defaultValue));
99140
}
@@ -147,31 +188,31 @@ private Object get(String key, Object defaultValue) {
147188
}
148189

149190
private boolean asBoolean(Object value) {
150-
if (value instanceof Boolean) {
151-
return (Boolean) value;
152-
} else if (value instanceof String) {
153-
if (((String) value).equalsIgnoreCase("enable")) {
191+
if (value instanceof Boolean b) {
192+
return b;
193+
} else if (value instanceof String sval) {
194+
if (sval.equalsIgnoreCase("enable")) {
154195
return true;
155196
}
156-
if (((String) value).equalsIgnoreCase("disable")) {
197+
if (sval.equalsIgnoreCase("disable")) {
157198
return false;
158199
}
159-
return Boolean.parseBoolean(((String) value).trim());
200+
return Boolean.parseBoolean(sval.trim());
160201
}
161202
return false;
162203
}
163204

164205
private long asLong(Object entry) {
165-
if (entry instanceof Number) {
166-
if (entry instanceof Float) {
167-
return Math.round((float) entry);
206+
if (entry instanceof Number eNum) {
207+
if (eNum instanceof Float fl) {
208+
return Math.round(fl);
168209
}
169-
if (entry instanceof Double) {
170-
return Math.round((double) entry);
210+
if (eNum instanceof Double db) {
211+
return Math.round(db);
171212
}
172-
return ((Number) entry).longValue();
173-
} else if (entry instanceof String) {
174-
String value = ((String) entry).trim();
213+
return eNum.longValue();
214+
} else if (entry instanceof String sval) {
215+
String value = sval.trim();
175216
if (value.contains(".")) {
176217
double d = asDouble(value);
177218
return Math.round(d);
@@ -220,10 +261,10 @@ private long parseTime(String value) {
220261
}
221262

222263
private double asDouble(Object entry) {
223-
if (entry instanceof Number) {
224-
return ((Number) entry).doubleValue();
225-
} else if (entry instanceof String) {
226-
return Double.parseDouble(((String) entry).trim());
264+
if (entry instanceof Number num) {
265+
return num.doubleValue();
266+
} else if (entry instanceof String str) {
267+
return Double.parseDouble(str.trim());
227268
}
228269
throw new NumberFormatException("Unknown number format detected [" + entry + "]");
229270
}
@@ -247,10 +288,10 @@ public ConfigurationProperties getGlobal() {
247288

248289
@Override
249290
public boolean equals(Object object) {
250-
if (object instanceof ConfigurationProperties) {
291+
if (object instanceof ConfigurationProperties cfg) {
251292
boolean listEquals = super.equals(object);
252293
if (global != null) {
253-
return listEquals && global.equals(((ConfigurationProperties) object).global);
294+
return listEquals && global.equals(cfg.global);
254295
}
255296
return listEquals;
256297
}
@@ -290,22 +331,22 @@ public Set<String> keySet() {
290331
}
291332

292333
public void replace(String key, Object val) {
293-
if(val instanceof ConfigurationProperties) {
294-
ConfigurationProperties props = (ConfigurationProperties) val;
295-
map.replace(key, props);
334+
if(val instanceof ConfigurationProperties cfg) {
335+
map.replace(key, cfg);
296336
}
297337
}
298338

339+
@SuppressWarnings("java:S3740")
299340
public void put(String key, Object val) {
300-
if (val instanceof Map) {
301-
ConfigurationProperties props = new ConfigurationProperties((Map<String, Object>) val);
341+
if (val instanceof Map map1) {
342+
ConfigurationProperties props = new ConfigurationProperties(map1);
302343
props.setGlobal(global);
303344
map.put(key, props);
304-
} else if (val instanceof List) {
345+
} else if (val instanceof List list1) {
305346
List<Object> parsedList = new ArrayList<>();
306-
for (Object list : (List<Object>) val) {
307-
if (list instanceof Map) {
308-
ConfigurationProperties props = new ConfigurationProperties((Map<String, Object>) list);
347+
for (Object list : list1) {
348+
if (list instanceof Map map2) {
349+
ConfigurationProperties props = new ConfigurationProperties(map2);
309350
props.setGlobal(global);
310351
parsedList.add(props);
311352
}
@@ -342,18 +383,17 @@ public Map<String, Object> getMap() {
342383
private Map<String, Object> packMap(Map<String, Object> map) {
343384
Map<String, Object> response = new LinkedHashMap<>();
344385
for (Entry<String, Object> entry : map.entrySet()) {
345-
if (entry.getValue() instanceof ConfigurationProperties) {
346-
response.put(entry.getKey(), packMap(((ConfigurationProperties) entry.getValue()).map));
347-
} else if (entry.getValue() instanceof Map) {
348-
response.put(entry.getKey(), packMap((Map<String, Object>) entry.getValue()));
349-
} else if (entry.getValue() instanceof List) {
350-
List<Object> list = (List<Object>) entry.getValue();
386+
if (entry.getValue() instanceof ConfigurationProperties cfg) {
387+
response.put(entry.getKey(), packMap(cfg.map));
388+
} else if (entry.getValue() instanceof Map mapEntry) {
389+
response.put(entry.getKey(), packMap(mapEntry));
390+
} else if (entry.getValue() instanceof List list) {
351391
List<Object> replacement = new ArrayList<>();
352392
for (Object obj : list) {
353-
if (obj instanceof Map) {
354-
replacement.add(packMap((Map<String, Object>) obj));
355-
} else if (obj instanceof ConfigurationProperties) {
356-
replacement.add(packMap(((ConfigurationProperties) obj).getMap()));
393+
if (obj instanceof Map mapEntry) {
394+
replacement.add(packMap(mapEntry));
395+
} else if (obj instanceof ConfigurationProperties cfg) {
396+
replacement.add(packMap(cfg.getMap()));
357397
} else {
358398
replacement.add(obj);
359399
}

src/main/java/io/mapsmessaging/configuration/PropertyManager.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
package io.mapsmessaging.configuration;
2121

22-
import com.google.gson.Gson;
23-
import com.google.gson.GsonBuilder;
2422
import com.google.gson.JsonElement;
2523
import com.google.gson.JsonObject;
2624
import lombok.Getter;
@@ -35,6 +33,7 @@
3533
import java.util.Map.Entry;
3634

3735
@Getter
36+
@SuppressWarnings("java:S3740")
3837
public abstract class PropertyManager {
3938

4039
protected final ConfigurationProperties properties;
@@ -79,17 +78,17 @@ public String scanForDefaultConfig(String namespace) {
7978
return "";
8079
}
8180

82-
81+
@SuppressWarnings("unchecked")
8382
public @NonNull @NotNull JsonObject getPropertiesJSON(@NonNull @NotNull String name) {
8483
JsonObject jsonObject = new JsonObject();
8584
Object config = properties.get(name);
8685

87-
if (config instanceof ConfigurationProperties) {
88-
config = ((ConfigurationProperties) config).getMap();
86+
if (config instanceof ConfigurationProperties cfg) {
87+
config = cfg.getMap();
8988
}
9089

91-
if (config instanceof Map) {
92-
Map<String, Object> map = pack((Map<String, Object>) config);
90+
if (config instanceof Map mapCfg) {
91+
Map<String, Object> map = pack(mapCfg);
9392
JsonElement element = SystemProperties.getInstance().getGson().toJsonTree(map);
9493
jsonObject.add(name, element);
9594

@@ -106,16 +105,15 @@ public String scanForDefaultConfig(String namespace) {
106105

107106

108107
private void pack(Map<String, Object> map, String key, Object obj) {
109-
if (obj instanceof ConfigurationProperties) {
110-
pack(map, key, ((ConfigurationProperties) obj).getMap());
111-
} else if (obj instanceof Map) {
112-
map.put(key, pack((Map<String, Object>) obj));
113-
} else if (obj instanceof List) {
114-
List<Object> list = (List<Object>) obj;
108+
if (obj instanceof ConfigurationProperties cfg) {
109+
pack(map, key, cfg.getMap());
110+
} else if (obj instanceof Map mapCfg) {
111+
map.put(key, pack(mapCfg));
112+
} else if (obj instanceof List list) {
115113
List<Object> translated = new ArrayList<>();
116114
for (Object tmp : list) {
117-
if (tmp instanceof ConfigurationProperties) {
118-
translated.add(pack(((ConfigurationProperties) tmp).getMap()));
115+
if (tmp instanceof ConfigurationProperties cfg) {
116+
translated.add(pack(cfg.getMap()));
119117
} else {
120118
translated.add(tmp);
121119
}
@@ -142,8 +140,8 @@ public boolean contains(String name) {
142140

143141
public @NonNull @NotNull ConfigurationProperties getProperties(String name) {
144142
Object obj = properties.get(name);
145-
if (obj instanceof ConfigurationProperties) {
146-
return (ConfigurationProperties) obj;
143+
if (obj instanceof ConfigurationProperties cfg) {
144+
return cfg;
147145
}
148146
return new ConfigurationProperties();
149147
}

src/main/java/io/mapsmessaging/configuration/aws/AwsSsmApi.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import java.io.IOException;
2626
import java.util.List;
27-
import java.util.stream.Collectors;
2827

2928
public class AwsSsmApi {
3029

@@ -47,10 +46,10 @@ public List<String> getKeys(String path) throws IOException {
4746
.path(buildKey(path))
4847
.recursive(true)
4948
.build());
50-
List<String> keys = response.parameters().stream().map(Parameter::name).collect(Collectors.toList());
49+
List<String> keys = response.parameters().stream().map(Parameter::name).toList();
5150
return keys.stream()
5251
.map(this::processKey)
53-
.collect(Collectors.toList());
52+
.toList();
5453
} catch (SsmException e) {
5554
throw new IOException("Error retrieving keys from Parameter Store", e);
5655
}

src/main/java/io/mapsmessaging/configuration/parsers/JsonParser.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@
1919

2020
package io.mapsmessaging.configuration.parsers;
2121

22-
import com.fasterxml.jackson.databind.ObjectMapper;
2322
import com.google.gson.JsonObject;
2423
import com.google.gson.reflect.TypeToken;
2524
import io.mapsmessaging.configuration.SystemProperties;
2625

27-
import java.io.IOException;
2826
import java.lang.reflect.Type;
2927
import java.util.LinkedHashMap;
3028
import java.util.List;

src/main/java/io/mapsmessaging/configuration/yaml/YamlParser.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import io.mapsmessaging.configuration.SystemProperties;
2626
import io.mapsmessaging.configuration.parsers.JsonParser;
2727

28-
2928
import java.lang.reflect.Type;
3029
import java.util.Map;
3130

src/main/java/io/mapsmessaging/configuration/yaml/YamlPropertyManager.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,23 @@
3030
import java.io.IOException;
3131
import java.io.PrintWriter;
3232
import java.time.LocalDate;
33-
import java.util.Date;
3433
import java.util.HashMap;
3534
import java.util.LinkedHashMap;
3635
import java.util.Map;
3736
import java.util.Map.Entry;
3837

38+
@SuppressWarnings("java:S3740")
3939
public abstract class YamlPropertyManager extends PropertyManager {
4040

4141
private static final String GLOBAL = "global";
4242

43-
protected void parseAndLoadYaml(String propertyName, String yamlString) throws IOException {
43+
protected void parseAndLoadYaml(String propertyName, String yamlString) {
4444
Yaml yaml = new Yaml();
4545
JsonParser parser = new YamlParser(yaml.load(yamlString));
4646
Map<String, Object> response = parser.parse();
4747
Object topLevel = response.get(propertyName);
48-
if (topLevel instanceof Map) {
49-
Map<String, Object> root = (Map<String, Object>) topLevel;
48+
if (topLevel instanceof Map map) {
49+
Map<String, Object> root = map;
5050
root.put("loaded", System.currentTimeMillis());
5151
}
5252
ConfigurationProperties configurationProperties = new ConfigurationProperties();

0 commit comments

Comments
 (0)