Skip to content

Commit aac5d46

Browse files
committed
Trying to fix Spotbugs warnings
1 parent 809a188 commit aac5d46

4 files changed

Lines changed: 52 additions & 46 deletions

File tree

src/main/java/com/imsweb/validation/InitializationStats.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.Map;
99
import java.util.concurrent.ConcurrentHashMap;
1010
import java.util.concurrent.atomic.AtomicInteger;
11+
import java.util.concurrent.atomic.AtomicLong;
1112
import java.util.stream.Collectors;
1213

1314
public class InitializationStats {
@@ -16,7 +17,7 @@ public class InitializationStats {
1617
public static final String REASON_DIFFERENT_VERSION = "pre-compiled validator has version {0} but application expected {1}";
1718
public static final String REASON_DISABLED = "pre-compiled edits are disabled";
1819

19-
private long _initializationDuration;
20+
private final AtomicLong _initializationDuration;
2021

2122
private final AtomicInteger _numEditsLoaded;
2223

@@ -27,19 +28,19 @@ public class InitializationStats {
2728
private final Map<String, InitializationStatsPerValidator> _validatorStats;
2829

2930
public InitializationStats() {
30-
_initializationDuration = 0L;
31+
_initializationDuration = new AtomicLong();
3132
_numEditsLoaded = new AtomicInteger();
3233
_numEditsCompiled = new AtomicInteger();
3334
_numEditsPreCompiled = new AtomicInteger();
3435
_validatorStats = new ConcurrentHashMap<>();
3536
}
3637

3738
public long getInitializationDuration() {
38-
return _initializationDuration;
39+
return _initializationDuration.get();
3940
}
4041

4142
public void setInitializationDuration(long initializationDuration) {
42-
_initializationDuration = initializationDuration;
43+
_initializationDuration.addAndGet(initializationDuration);
4344
}
4445

4546
public int getNumEditsLoaded() {

src/main/java/com/imsweb/validation/ValidationContextFunctions.java

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.Map;
1313
import java.util.Set;
1414
import java.util.concurrent.ConcurrentHashMap;
15+
import java.util.concurrent.atomic.AtomicInteger;
1516
import java.util.concurrent.atomic.AtomicLong;
1617
import java.util.regex.Pattern;
1718

@@ -141,7 +142,7 @@ public static List<ContextFunctionDocDto> getMethodsDocumentation() {
141142
private Map<String, Pattern> _regexCache;
142143

143144
// maximum size of the regex cache (-1 for no limit)
144-
private int _regexCacheSize;
145+
private AtomicInteger _regexCacheSize;
145146

146147
// stats for the cached regular expressions
147148
private AtomicLong _numRegexCacheHit;
@@ -255,25 +256,28 @@ public Object getContext(String validatorId, String contextKey) throws Validatio
255256
* @return a <code>ValidationLookup</code>, never null
256257
* @throws ValidationException if provided lookup ID is null or invalid
257258
*/
258-
@ContextFunctionDocAnnotation(paramName1 = "id", param1 = "Lookup ID", desc = "Returns the lookup corresponding to the requested ID.\n\n" +
259-
"The returned object is a ValidationLookup on which the following methods are available:\n" +
260-
" String getId()\n" +
261-
" String getByKey(String key)\n" +
262-
" String getByKeyWithCase(String key)\n" +
263-
" Set<String> getAllByKey(String key)\n" +
264-
" Set<String> getAllByKeyWithCase(String key)\n" +
265-
" Set<String> getAllKeys()\n" +
266-
" String getByValue(String value)\n" +
267-
" String getByValueWithCase(String value)\n" +
268-
" Set<String> getAllByValue(String value)\n" +
269-
" Set<String> getAllByValueWithCase(String value)\n" +
270-
" Set<String> getAllValues()\n" +
271-
" boolean containsKey(Object key)\n" +
272-
" boolean containsKeyWithCase(Object key)\n" +
273-
" boolean containsValue(Object value)\n" +
274-
" boolean containsValueWithCase(Object value)\n" +
275-
" boolean containsPair(String key, String value)\n" +
276-
" boolean containsPairWithCase(String key, String value)\n",
259+
@ContextFunctionDocAnnotation(paramName1 = "id", param1 = "Lookup ID", desc = """
260+
Returns the lookup corresponding to the requested ID.
261+
262+
The returned object is a ValidationLookup on which the following methods are available:
263+
String getId()
264+
String getByKey(String key)
265+
String getByKeyWithCase(String key)
266+
Set<String> getAllByKey(String key)
267+
Set<String> getAllByKeyWithCase(String key)
268+
Set<String> getAllKeys()
269+
String getByValue(String value)
270+
String getByValueWithCase(String value)
271+
Set<String> getAllByValue(String value)
272+
Set<String> getAllByValueWithCase(String value)
273+
Set<String> getAllValues()
274+
boolean containsKey(Object key)
275+
boolean containsKeyWithCase(Object key)
276+
boolean containsValue(Object value)
277+
boolean containsValueWithCase(Object value)
278+
boolean containsPair(String key, String value)
279+
boolean containsPairWithCase(String key, String value)
280+
""",
277281
example = "Functions.fetchLookup('lookup_id').containsKey(value)")
278282
public ValidationLookup fetchLookup(String id) throws ValidationException {
279283
if (id == null)
@@ -382,8 +386,7 @@ public boolean between(Object value, Object low, Object high) {
382386
if (value == null || low == null || high == null)
383387
return false;
384388

385-
if (value instanceof String) {
386-
String val = (String)value;
389+
if (value instanceof String val) {
387390
String l = low.toString();
388391
String h = high.toString();
389392

@@ -461,7 +464,7 @@ public void enableRegexCaching(int cacheSize) {
461464
if (cacheSize < 0)
462465
throw new IllegalStateException("Cache size must be greater than 0!");
463466
_regexCache = new ConcurrentHashMap<>();
464-
_regexCacheSize = cacheSize;
467+
_regexCacheSize = new AtomicInteger(cacheSize);
465468
_numRegexCacheHit = new AtomicLong();
466469
_numRegexCacheMiss = new AtomicLong();
467470
}
@@ -473,7 +476,7 @@ public void enableRegexCaching(int cacheSize) {
473476
*/
474477
public void disableRegexCaching() {
475478
_regexCache = null;
476-
_regexCacheSize = Integer.MAX_VALUE;
479+
_regexCacheSize = new AtomicInteger(Integer.MAX_VALUE);
477480
_numRegexCacheHit = null;
478481
_numRegexCacheMiss = null;
479482
}
@@ -501,7 +504,7 @@ public boolean matches(Object value, Object regex) {
501504
_numRegexCacheMiss.incrementAndGet();
502505
pattern = Pattern.compile(reg);
503506
// in a multi-threaded environment, it's possible that the cache will add a few more values than the max cache size, and that's OK
504-
if (_regexCache.size() < _regexCacheSize)
507+
if (_regexCache.size() < _regexCacheSize.get())
505508
_regexCache.put(reg, pattern);
506509
}
507510
else

src/main/java/com/imsweb/validation/ValidationEngine.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.concurrent.Executors;
2020
import java.util.concurrent.Future;
2121
import java.util.concurrent.TimeUnit;
22+
import java.util.concurrent.atomic.AtomicBoolean;
2223
import java.util.concurrent.atomic.AtomicInteger;
2324
import java.util.concurrent.locks.ReentrantReadWriteLock;
2425
import java.util.stream.Collectors;
@@ -249,7 +250,7 @@ private enum ValidationEngineStatus {
249250
/**
250251
* Whether or not the edits statistics should be computed (initial value is based on the initialization options, but can be changed later)
251252
*/
252-
protected boolean _computeEditsStats = false;
253+
protected AtomicBoolean _computeEditsStats = new AtomicBoolean(false);
253254

254255
/**
255256
* Private lock controlling access to the stats; those are "written" every time new stats are reported (which is constantly), and so using a global engine lock is not good enough.
@@ -359,7 +360,7 @@ public InitializationStats initialize(InitializationOptions options, List<Valida
359360

360361
_options = options == null ? new InitializationOptions() : options;
361362

362-
_computeEditsStats = _options.isEngineStatsEnabled();
363+
_computeEditsStats.set(_options.isEngineStatsEnabled());
363364

364365
if (validators != null) {
365366
checkValidatorConstraints(validators);
@@ -667,7 +668,7 @@ public Collection<RuleFailure> validate(Validatable validatable) throws Validati
667668
_lock.readLock().lock();
668669
try {
669670
ValidatingContext vContext = new ValidatingContext();
670-
vContext.setComputeEditsStats(_computeEditsStats);
671+
vContext.setComputeEditsStats(_computeEditsStats.get());
671672
return internalValidate(validatable, vContext);
672673
}
673674
finally {
@@ -696,7 +697,7 @@ public Collection<RuleFailure> validate(Validatable validatable, Collection<Stri
696697
try {
697698
ValidatingContext vContext = new ValidatingContext();
698699
vContext.setToIgnore(ruleIdsToIgnore);
699-
vContext.setComputeEditsStats(_computeEditsStats);
700+
vContext.setComputeEditsStats(_computeEditsStats.get());
700701
return internalValidate(validatable, vContext);
701702
}
702703
finally {
@@ -727,7 +728,7 @@ public Collection<RuleFailure> validate(Validatable validatable, Collection<Stri
727728
ValidatingContext vContext = new ValidatingContext();
728729
vContext.setToIgnore(ruleIdsToIgnore);
729730
vContext.setToExecute(ruleIdsToExecute);
730-
vContext.setComputeEditsStats(_computeEditsStats);
731+
vContext.setComputeEditsStats(_computeEditsStats.get());
731732
return internalValidate(validatable, vContext);
732733
}
733734
finally {
@@ -759,7 +760,7 @@ public Collection<RuleFailure> validate(Validatable validatable, String ruleId)
759760
throw new IllegalStateException("Unknown rule ID: " + ruleId);
760761
ValidatingContext vContext = new ValidatingContext();
761762
vContext.setToForce(rule);
762-
vContext.setComputeEditsStats(_computeEditsStats);
763+
vContext.setComputeEditsStats(_computeEditsStats.get());
763764
return internalValidate(validatable, vContext);
764765
}
765766
finally {
@@ -794,7 +795,7 @@ public Collection<RuleFailure> validate(Validatable validatable, Rule rule) thro
794795

795796
ValidatingContext vContext = new ValidatingContext();
796797
vContext.setToForce(rule);
797-
vContext.setComputeEditsStats(_computeEditsStats);
798+
vContext.setComputeEditsStats(_computeEditsStats.get());
798799
return internalValidate(validatable, vContext);
799800
}
800801
finally {
@@ -818,7 +819,7 @@ public Collection<RuleFailure> validate(Validatable validatable, Rule rule) thro
818819
public Collection<RuleFailure> validate(Validatable validatable, ValidatingContext vContext) throws ValidationException {
819820
_lock.readLock().lock();
820821
try {
821-
vContext.setComputeEditsStats(_computeEditsStats);
822+
vContext.setComputeEditsStats(_computeEditsStats.get());
822823
return internalValidate(validatable, vContext);
823824
}
824825
finally {
@@ -1843,14 +1844,14 @@ public void resetStats() {
18431844
* Dynamically enables/disabled computing the edits statistics on this engine.
18441845
*/
18451846
public void setEditsStatsEnabled(boolean enabled) {
1846-
_computeEditsStats = enabled;
1847+
_computeEditsStats.set(enabled);
18471848
}
18481849

18491850
/**
18501851
* Returns true if the edits statistics are on (that can be done via the initialization or dynamically via the engine itself).
18511852
*/
18521853
public boolean isEditsStatsEnabled() {
1853-
return _computeEditsStats;
1854+
return _computeEditsStats.get();
18541855
}
18551856

18561857
/**
@@ -2042,7 +2043,7 @@ private Collection<RuleFailure> internalValidate(Validatable validatable, Valida
20422043
Collection<RuleFailure> failures = processor.process(validatable, vContext);
20432044

20442045
// report the stats if we have to
2045-
if (_computeEditsStats) {
2046+
if (_computeEditsStats.get()) {
20462047
_statsLock.writeLock().lock();
20472048
try {
20482049
for (Entry<String, Long> entry : vContext.getEditDurations().entrySet())

src/test/java/com/imsweb/validation/ValidationEngineTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,13 @@ public void testValidate() throws Exception {
248248
ValidationEngine.getInstance().setEditsStatsEnabled(false);
249249

250250
// let's make the second level fail, that rule overrides the returned properties
251-
((List<Map<String, Object>>)entity.get("level2")).get(0).put("prop", "1");
251+
((List<Map<String, Object>>)entity.get("level2")).getFirst().put("prop", "1");
252252
validatable = new SimpleMapValidatable("ID", "level1", entity);
253253
TestingUtils.assertEditFailure(ValidationEngine.getInstance().validate(validatable), "fv-rule2");
254254
TestingUtils.assertEditFailure(ValidationEngine.getInstance().validate(validatable, "fv-rule2"), "fv-rule2");
255255
Assert.assertEquals(1, ValidationEngine.getInstance().validate(validatable).iterator().next().getProperties().size());
256256
Assert.assertTrue(ValidationEngine.getInstance().validate(validatable).iterator().next().getProperties().contains("level1.level2[0].otherProp"));
257-
((List<Map<String, Object>>)entity.get("level2")).get(0).put("prop", "0");
257+
((List<Map<String, Object>>)entity.get("level2")).getFirst().put("prop", "0");
258258

259259
Assert.assertTrue(ValidationEngine.getInstance().getStats().isEmpty());
260260
ValidationEngine.getInstance().setEditsStatsEnabled(true);
@@ -354,11 +354,12 @@ public void testRuntimeValidation() throws IOException, ConstructionException, V
354354
Assert.assertFalse(normalValidator.getRule("fvrt-rule1").getUsedLookupIds().contains("fake-lookup"));
355355
ValidationEngine normalEngine = new ValidationEngine();
356356
InitializationStats stats = normalEngine.initialize(normalValidator);
357+
Assert.assertTrue(stats.getInitializationDuration() > 0);
357358
Assert.assertEquals(2, stats.getNumEditsLoaded());
358359
Assert.assertEquals(0, stats.getNumEditsPreCompiled());
359360
Assert.assertEquals(2, stats.getNumEditsCompiled());
360361
Assert.assertEquals(1, stats.getValidatorStats().size());
361-
InitializationStatsPerValidator valStats = stats.getValidatorStats().get(0);
362+
InitializationStatsPerValidator valStats = stats.getValidatorStats().getFirst();
362363
Assert.assertEquals("fake-validator-runtime", valStats.getValidatorId());
363364
Assert.assertEquals(2, valStats.getNumEditsLoaded());
364365
Assert.assertEquals(0, valStats.getNumEditsPreCompiled());
@@ -373,7 +374,7 @@ public void testRuntimeValidation() throws IOException, ConstructionException, V
373374
Assert.assertEquals(2, stats.getNumEditsLoaded());
374375
Assert.assertEquals(2, stats.getNumEditsPreCompiled());
375376
Assert.assertEquals(0, stats.getNumEditsCompiled());
376-
valStats = stats.getValidatorStats().get(0);
377+
valStats = stats.getValidatorStats().getFirst();
377378
Assert.assertEquals("fake-validator-runtime", valStats.getValidatorId());
378379
Assert.assertEquals(2, valStats.getNumEditsLoaded());
379380
Assert.assertEquals(2, valStats.getNumEditsPreCompiled());
@@ -388,7 +389,7 @@ public void testRuntimeValidation() throws IOException, ConstructionException, V
388389
Assert.assertEquals(2, stats.getNumEditsLoaded());
389390
Assert.assertEquals(2, stats.getNumEditsPreCompiled());
390391
Assert.assertEquals(0, stats.getNumEditsCompiled());
391-
valStats = stats.getValidatorStats().get(0);
392+
valStats = stats.getValidatorStats().getFirst();
392393
Assert.assertEquals("fake-validator-runtime", valStats.getValidatorId());
393394
Assert.assertEquals(2, valStats.getNumEditsLoaded());
394395
Assert.assertEquals(2, valStats.getNumEditsPreCompiled());
@@ -402,7 +403,7 @@ public void testRuntimeValidation() throws IOException, ConstructionException, V
402403
Assert.assertEquals(2, stats.getNumEditsLoaded());
403404
Assert.assertEquals(0, stats.getNumEditsPreCompiled());
404405
Assert.assertEquals(2, stats.getNumEditsCompiled());
405-
Assert.assertEquals(InitializationStats.REASON_DISABLED, stats.getValidatorStats().get(0).getReasonNotPreCompiled());
406+
Assert.assertEquals(InitializationStats.REASON_DISABLED, stats.getValidatorStats().getFirst().getReasonNotPreCompiled());
406407

407408
// the global cached engine should not now about these validators
408409
Assert.assertNull(ValidationEngine.getInstance().getValidator("fake-validator-runtime"));

0 commit comments

Comments
 (0)