Skip to content

Commit 8553bff

Browse files
David Waltermiredavid-waltermire
authored andcommitted
Fixed a bunch of PMD warnings. Improved some Javadocs.
1 parent 359c601 commit 8553bff

35 files changed

+261
-125
lines changed

core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/AbstractDurationAdapter.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@
2424
import edu.umd.cs.findbugs.annotations.NonNull;
2525
import edu.umd.cs.findbugs.annotations.Nullable;
2626

27+
/**
28+
* Supports working with duration-based data values.
29+
*
30+
* @param <TYPE>
31+
* the raw Java type this adapter supports
32+
* @param <ITEM_TYPE>
33+
* the metapath item type supported by the adapter
34+
*/
2735
public abstract class AbstractDurationAdapter<TYPE, ITEM_TYPE extends IDurationItem>
2836
extends AbstractDataTypeAdapter<TYPE, ITEM_TYPE> {
2937
private static final Pattern DURATION_PATTERN = Pattern.compile(
@@ -211,7 +219,7 @@ private static int parseIntegerValue(@Nullable String value) {
211219

212220
@NonNull
213221
private static BigDecimal parseDecimalValue(@Nullable String value) {
214-
return value == null ? ObjectUtils.notNull(BigDecimal.ZERO) : new BigDecimal(value, DecimalAdapter.MATH_CONTEXT);
222+
return value == null ? ObjectUtils.notNull(BigDecimal.ZERO) : new BigDecimal(value, DecimalAdapter.mathContext());
215223
}
216224

217225
private static long toWholeSeconds(@NonNull BigDecimal bigSeconds) {
@@ -226,9 +234,9 @@ private static long toWholeSeconds(@NonNull BigDecimal bigSeconds) {
226234
}
227235

228236
private static long toFractionalNanoSeconds(@NonNull BigDecimal seconds) {
229-
BigDecimal remainder = seconds.remainder(BigDecimal.ONE, DecimalAdapter.MATH_CONTEXT);
237+
BigDecimal remainder = seconds.remainder(BigDecimal.ONE, DecimalAdapter.mathContext());
230238

231-
BigDecimal result = remainder.multiply(new BigDecimal("1e9", DecimalAdapter.MATH_CONTEXT))
239+
BigDecimal result = remainder.multiply(new BigDecimal("1e9", DecimalAdapter.mathContext()))
232240
.setScale(0, RoundingMode.HALF_EVEN);
233241
try {
234242
return result.longValueExact();

core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/DecimalAdapter.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
*/
3030
public class DecimalAdapter
3131
extends AbstractDataTypeAdapter<BigDecimal, IDecimalItem> {
32-
public static final MathContext MATH_CONTEXT = MathContext.DECIMAL64;
3332
@NonNull
3433
private static final List<IEnhancedQName> NAMES = ObjectUtils.notNull(
3534
List.of(
@@ -39,6 +38,22 @@ public class DecimalAdapter
3938
super(BigDecimal.class, IDecimalItem.class, IDecimalItem::cast);
4039
}
4140

41+
/**
42+
* Provides a standardized math context to use with {@link BigDecimal}
43+
* operations.
44+
* <p>
45+
* This math context was chosen for decimal arithmetic operations, since
46+
* DECIMAL64 provides a precision of 16 digits, which is sufficient for most
47+
* business calculations while maintaining reasonable performance.
48+
*
49+
* @return the standardized math context
50+
*/
51+
@SuppressWarnings("null")
52+
@NonNull
53+
public static MathContext mathContext() {
54+
return MathContext.DECIMAL64;
55+
}
56+
4257
@Override
4358
public List<IEnhancedQName> getNames() {
4459
return NAMES;
@@ -51,7 +66,7 @@ public JsonFormatTypes getJsonRawType() {
5166

5267
@Override
5368
public BigDecimal parse(String value) {
54-
return new BigDecimal(value, MATH_CONTEXT);
69+
return new BigDecimal(value, mathContext());
5570
}
5671

5772
@Override

core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/ComparisonFunctions.java

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ public static IBooleanItem compare( // NOPMD - unavoidable
214214
dynamicContext);
215215
} else if (left instanceof ITimeItem && right instanceof ITimeItem) {
216216
retval = dateTimeCompare(
217-
IDateTimeItem.valueOf(OperationFunctions.DATE_1972_12_31, (ITimeItem) left),
217+
IDateTimeItem.valueOf(OperationFunctions.referenceDate(), (ITimeItem) left),
218218
operator,
219-
IDateTimeItem.valueOf(OperationFunctions.DATE_1972_12_31, (ITimeItem) right),
219+
IDateTimeItem.valueOf(OperationFunctions.referenceDate(), (ITimeItem) right),
220220
dynamicContext);
221221
} else if (left instanceof IDurationItem && right instanceof IDurationItem) {
222222
retval = durationCompare((IDurationItem) left, operator, (IDurationItem) right);
@@ -249,7 +249,7 @@ public static IBooleanItem stringCompare(
249249
@NonNull Operator operator,
250250
@NonNull IStringItem right) {
251251
int result = left.compareTo(right);
252-
boolean retval;
252+
Boolean retval = null;
253253
switch (operator) {
254254
case EQ:
255255
retval = result == 0;
@@ -269,10 +269,10 @@ public static IBooleanItem stringCompare(
269269
case NE:
270270
retval = result != 0;
271271
break;
272-
default:
273-
throw new IllegalArgumentException(
274-
String.format("Unsupported operator '%s'", operator.name())); // NOPMD
275272
}
273+
274+
assert retval != null : String.format("Unsupported operator '%s'", operator.name());
275+
276276
return IBooleanItem.valueOf(retval);
277277
}
278278

@@ -291,7 +291,7 @@ public static IBooleanItem stringCompare(
291291
@NonNull
292292
public static IBooleanItem numericCompare(@NonNull INumericItem left, @NonNull Operator operator,
293293
@NonNull INumericItem right) {
294-
IBooleanItem retval;
294+
IBooleanItem retval = null;
295295
switch (operator) {
296296
case EQ:
297297
retval = OperationFunctions.opNumericEqual(left, right);
@@ -317,9 +317,9 @@ public static IBooleanItem numericCompare(@NonNull INumericItem left, @NonNull O
317317
case NE:
318318
retval = FnNot.fnNot(OperationFunctions.opNumericEqual(left, right));
319319
break;
320-
default:
321-
throw new IllegalArgumentException(String.format("Unsupported operator '%s'", operator.name()));
322320
}
321+
assert retval != null : String.format("Unsupported operator '%s'", operator.name());
322+
323323
return retval;
324324
}
325325

@@ -340,7 +340,7 @@ public static IBooleanItem booleanCompare(
340340
@NonNull IBooleanItem left,
341341
@NonNull Operator operator,
342342
@NonNull IBooleanItem right) {
343-
IBooleanItem retval;
343+
IBooleanItem retval = null;
344344
switch (operator) {
345345
case EQ:
346346
retval = OperationFunctions.opBooleanEqual(left, right);
@@ -366,9 +366,9 @@ public static IBooleanItem booleanCompare(
366366
case NE:
367367
retval = FnNot.fnNot(OperationFunctions.opBooleanEqual(left, right));
368368
break;
369-
default:
370-
throw new IllegalArgumentException(String.format("Unsupported operator '%s'", operator.name()));
371369
}
370+
assert retval != null : String.format("Unsupported operator '%s'", operator.name());
371+
372372
return retval;
373373
}
374374

@@ -392,7 +392,7 @@ public static IBooleanItem dateTimeCompare(
392392
@NonNull Operator operator,
393393
@NonNull IDateTimeItem right,
394394
@Nullable DynamicContext dynamicContext) {
395-
IBooleanItem retval;
395+
IBooleanItem retval = null;
396396
switch (operator) {
397397
case EQ:
398398
retval = OperationFunctions.opDateTimeEqual(left, right, dynamicContext);
@@ -424,9 +424,9 @@ public static IBooleanItem dateTimeCompare(
424424
case NE:
425425
retval = FnNot.fnNot(OperationFunctions.opDateTimeEqual(left, right, dynamicContext));
426426
break;
427-
default:
428-
throw new IllegalArgumentException(String.format("Unsupported operator '%s'", operator.name()));
429427
}
428+
assert retval != null : String.format("Unsupported operator '%s'", operator.name());
429+
430430
return retval;
431431
}
432432

@@ -507,8 +507,6 @@ public static IBooleanItem durationCompare( // NOPMD - unavoidable
507507
case NE:
508508
retval = FnNot.fnNot(OperationFunctions.opDurationEqual(left, right));
509509
break;
510-
default:
511-
throw new IllegalArgumentException(String.format("Unsupported operator '%s'", operator.name()));
512510
}
513511

514512
if (retval == null) {
@@ -536,7 +534,7 @@ public static IBooleanItem durationCompare( // NOPMD - unavoidable
536534
@NonNull
537535
public static IBooleanItem binaryCompare(@NonNull IBase64BinaryItem left, @NonNull Operator operator,
538536
@NonNull IBase64BinaryItem right) {
539-
IBooleanItem retval;
537+
IBooleanItem retval = null;
540538
switch (operator) {
541539
case EQ:
542540
retval = OperationFunctions.opBase64BinaryEqual(left, right);
@@ -562,9 +560,9 @@ public static IBooleanItem binaryCompare(@NonNull IBase64BinaryItem left, @NonNu
562560
case NE:
563561
retval = FnNot.fnNot(OperationFunctions.opBase64BinaryEqual(left, right));
564562
break;
565-
default:
566-
throw new IllegalArgumentException(String.format("Unsupported operator '%s'", operator.name()));
567563
}
564+
assert retval != null : String.format("Unsupported operator '%s'", operator.name());
565+
568566
return retval;
569567
}
570568
}

core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/FunctionLibrary.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818
import edu.umd.cs.findbugs.annotations.NonNull;
1919
import edu.umd.cs.findbugs.annotations.Nullable;
2020

21+
/**
22+
* Provides a catalog of registered function implementations.
23+
* <p>
24+
* Functions can be registered using the {@link #registerFunction(IFunction)}
25+
* method.
26+
* <p>
27+
* Previously registered functions can be looked up using the
28+
* {@link #getFunction(IEnhancedQName, int)} method.
29+
*/
2130
public class FunctionLibrary implements IFunctionLibrary {
2231

2332
@NonNull

core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/FunctionUtils.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import gov.nist.secauto.metaschema.core.metapath.type.TypeMetapathException;
1515
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
1616

17-
import java.math.MathContext;
1817
import java.util.Collection;
1918
import java.util.HashMap;
2019
import java.util.List;
@@ -36,13 +35,6 @@
3635
// types
3736
@SuppressWarnings("PMD.CouplingBetweenObjects")
3837
public final class FunctionUtils {
39-
/**
40-
* The math context used for decimal arithmetic operations. DECIMAL64 provides a
41-
* precision of 16 digits, which is sufficient for most business calculations
42-
* while maintaining reasonable performance.
43-
*/
44-
public static final MathContext MATH_CONTEXT = MathContext.DECIMAL64;
45-
4638
private FunctionUtils() {
4739
// disable
4840
}

core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/impl/OperationFunctions.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
import edu.umd.cs.findbugs.annotations.NonNull;
4040
import edu.umd.cs.findbugs.annotations.Nullable;
41+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
4142

4243
/**
4344
* Implementations of the XPath 3.1 operation functions.
@@ -49,18 +50,41 @@
4950
"PMD.CyclomaticComplexity"
5051
})
5152
public final class OperationFunctions {
53+
@SuppressFBWarnings(value = "MS_EXPOSE_REP", justification = "Value is immutable")
5254
@NonNull
53-
public static final IDateItem DATE_1972_12_31 = IDateItem.valueOf(ObjectUtils.notNull(LocalDate.of(1972, 12, 31)));
54-
55+
private static final IDateItem DATE_1972_12_31 = IDateItem.valueOf(ObjectUtils.notNull(LocalDate.of(1972, 12, 31)));
5556
/**
5657
* Identifies the types and substypes that support aggregation.
5758
*/
59+
@SuppressFBWarnings(value = "MS_EXPOSE_REP", justification = "Value is immutable")
5860
@NonNull
59-
public static final Set<Class<? extends IAnyAtomicItem>> AGGREGATE_MATH_TYPES = ObjectUtils.notNull(Set.of(
61+
private static final Set<Class<? extends IAnyAtomicItem>> AGGREGATE_MATH_TYPES = ObjectUtils.notNull(Set.of(
6062
IDayTimeDurationItem.class,
6163
IYearMonthDurationItem.class,
6264
INumericItem.class));
6365

66+
/**
67+
* Get a reference date for use in date-based operations.
68+
*
69+
* @return the reference date
70+
*/
71+
@NonNull
72+
public static IDateItem referenceDate() {
73+
return DATE_1972_12_31;
74+
}
75+
76+
/**
77+
* Get the types supported by aggregation operations.
78+
* <p>
79+
* Any associated subtypes are also supported.
80+
*
81+
* @return the set of supported types
82+
*/
83+
@NonNull
84+
public static Set<Class<? extends IAnyAtomicItem>> aggregateMathTypes() {
85+
return AGGREGATE_MATH_TYPES;
86+
}
87+
6488
private OperationFunctions() {
6589
// disable
6690
}

core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnAvg.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public static IAnyAtomicItem average(@NonNull Collection<? extends IAnyAtomicIte
9494
// tell cpd to start ignoring code - CPD-OFF
9595

9696
Map<Class<? extends IAnyAtomicItem>, Integer> typeCounts = FunctionUtils.countTypes(
97-
OperationFunctions.AGGREGATE_MATH_TYPES,
97+
OperationFunctions.aggregateMathTypes(),
9898
ObjectUtils.notNull(items));
9999

100100
int count = items.size();
@@ -140,7 +140,7 @@ public static IAnyAtomicItem average(@NonNull Collection<? extends IAnyAtomicIte
140140
throw new InvalidArgumentFunctionException(
141141
InvalidArgumentFunctionException.INVALID_ARGUMENT_TYPE,
142142
String.format("Values must all be of type '%s'.",
143-
OperationFunctions.AGGREGATE_MATH_TYPES.stream()
143+
OperationFunctions.aggregateMathTypes().stream()
144144
.map(Class::getName)
145145
.collect(CustomCollectors.joiningWithOxfordComma(","))));
146146
}

0 commit comments

Comments
 (0)