34
34
import static io .mapsmessaging .logging .LogMessages .PROPERTY_MANAGER_ENTRY_LOOKUP ;
35
35
import static io .mapsmessaging .logging .LogMessages .PROPERTY_MANAGER_ENTRY_LOOKUP_FAILED ;
36
36
37
+ @ SuppressWarnings ("java:S3740" )
37
38
public class ConfigurationProperties {
38
39
39
40
private final Logger logger = LoggerFactory .getLogger (ConfigurationProperties .class );
@@ -54,8 +55,8 @@ public ConfigurationProperties(Map<String, Object> inMap) {
54
55
putAll (inMap );
55
56
56
57
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 ;
59
60
}
60
61
}
61
62
@@ -64,9 +65,9 @@ public Object get(String key) {
64
65
if (val == null && global != null ) {
65
66
val = global .get (key );
66
67
}
67
- if (val instanceof JsonObject ) {
68
+ if (val instanceof JsonObject jsonObject ) {
68
69
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 );
70
71
return new ConfigurationProperties (map1 );
71
72
}
72
73
return val ;
@@ -94,6 +95,46 @@ public String getProperty(String key, String defaultValue) {
94
95
return null ;
95
96
}
96
97
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
+
97
138
public boolean getBooleanProperty (String key , boolean defaultValue ) {
98
139
return asBoolean (get (key , defaultValue ));
99
140
}
@@ -147,31 +188,31 @@ private Object get(String key, Object defaultValue) {
147
188
}
148
189
149
190
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" )) {
154
195
return true ;
155
196
}
156
- if ((( String ) value ) .equalsIgnoreCase ("disable" )) {
197
+ if (sval .equalsIgnoreCase ("disable" )) {
157
198
return false ;
158
199
}
159
- return Boolean .parseBoolean ((( String ) value ) .trim ());
200
+ return Boolean .parseBoolean (sval .trim ());
160
201
}
161
202
return false ;
162
203
}
163
204
164
205
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 );
168
209
}
169
- if (entry instanceof Double ) {
170
- return Math .round (( double ) entry );
210
+ if (eNum instanceof Double db ) {
211
+ return Math .round (db );
171
212
}
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 ();
175
216
if (value .contains ("." )) {
176
217
double d = asDouble (value );
177
218
return Math .round (d );
@@ -220,10 +261,10 @@ private long parseTime(String value) {
220
261
}
221
262
222
263
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 ());
227
268
}
228
269
throw new NumberFormatException ("Unknown number format detected [" + entry + "]" );
229
270
}
@@ -247,10 +288,10 @@ public ConfigurationProperties getGlobal() {
247
288
248
289
@ Override
249
290
public boolean equals (Object object ) {
250
- if (object instanceof ConfigurationProperties ) {
291
+ if (object instanceof ConfigurationProperties cfg ) {
251
292
boolean listEquals = super .equals (object );
252
293
if (global != null ) {
253
- return listEquals && global .equals ((( ConfigurationProperties ) object ) .global );
294
+ return listEquals && global .equals (cfg .global );
254
295
}
255
296
return listEquals ;
256
297
}
@@ -290,22 +331,22 @@ public Set<String> keySet() {
290
331
}
291
332
292
333
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 );
296
336
}
297
337
}
298
338
339
+ @ SuppressWarnings ("java:S3740" )
299
340
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 );
302
343
props .setGlobal (global );
303
344
map .put (key , props );
304
- } else if (val instanceof List ) {
345
+ } else if (val instanceof List list1 ) {
305
346
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 );
309
350
props .setGlobal (global );
310
351
parsedList .add (props );
311
352
}
@@ -342,18 +383,17 @@ public Map<String, Object> getMap() {
342
383
private Map <String , Object > packMap (Map <String , Object > map ) {
343
384
Map <String , Object > response = new LinkedHashMap <>();
344
385
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 ) {
351
391
List <Object > replacement = new ArrayList <>();
352
392
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 ()));
357
397
} else {
358
398
replacement .add (obj );
359
399
}
0 commit comments