See #6219 - Optimize the code, fix formatting issue

This commit is contained in:
Milamber 2025-06-18 08:36:41 +03:00
parent 177a53073c
commit 92d29d94a8
No known key found for this signature in database
GPG Key ID: AC214CAA0612B399
1 changed files with 21 additions and 14 deletions

View File

@ -46,6 +46,8 @@ public class SizeAssertion extends AbstractScopedAssertion implements Serializab
GREATERTHANEQUAL(5, "size_assertion_comparator_error_greaterequal"), GREATERTHANEQUAL(5, "size_assertion_comparator_error_greaterequal"),
LESSTHANEQUAL(6, "size_assertion_comparator_error_lessequal"); LESSTHANEQUAL(6, "size_assertion_comparator_error_lessequal");
private static final ComparisonOperator[] VALUES = values();
private final int value; private final int value;
private final String errorMessageKey; private final String errorMessageKey;
@ -63,23 +65,30 @@ public class SizeAssertion extends AbstractScopedAssertion implements Serializab
} }
public static ComparisonOperator fromValue(int value) { public static ComparisonOperator fromValue(int value) {
for (ComparisonOperator op : values()) { for (ComparisonOperator op : VALUES) {
if (op.value == value) { if (op.value == value) {
return op; return op;
} }
} }
throw new IllegalArgumentException("Invalid comparison operator value: " + value); return null;
} }
public boolean evaluate(long actual, long expected) { public boolean evaluate(long actual, long expected) {
return switch (this) { switch (this) {
case EQUAL -> actual == expected; case EQUAL:
case NOTEQUAL -> actual != expected; return actual == expected;
case GREATERTHAN -> actual > expected; case NOTEQUAL:
case LESSTHAN -> actual < expected; return actual != expected;
case GREATERTHANEQUAL -> actual >= expected; case GREATERTHAN:
case LESSTHANEQUAL -> actual <= expected; return actual > expected;
}; case LESSTHAN:
return actual < expected;
case GREATERTHANEQUAL:
return actual >= expected;
case LESSTHANEQUAL:
return actual <= expected;
}
throw new IllegalStateException("Unexpected value: " + this);
} }
} }
@ -236,10 +245,8 @@ public class SizeAssertion extends AbstractScopedAssertion implements Serializab
*/ */
private String compareSize(long resultSize) { private String compareSize(long resultSize) {
long allowedSize = Long.parseLong(getAllowedSize()); long allowedSize = Long.parseLong(getAllowedSize());
ComparisonOperator operator; ComparisonOperator operator = ComparisonOperator.fromValue(getCompOper());
try { if (operator == null) {
operator = ComparisonOperator.fromValue(getCompOper());
} catch (IllegalArgumentException e) {
return "ERROR - invalid condition"; return "ERROR - invalid condition";
} }