Skip to content

Commit

Permalink
Minor cleanup of constant check
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Herrera <[email protected]>
  • Loading branch information
Pablete1234 committed Aug 10, 2024
1 parent 2cc41e7 commit e0fc588
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions core/src/main/java/tc/oc/pgm/map/ConditionalChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ConditionalChecker {
*
* @param ctx the map's context
* @param el The conditional element
* @return true if the conditional
* @return if the conditional passes
* @throws InvalidXMLException if the element is invalid in any way
*/
static boolean test(MapFilePreprocessor ctx, Element el) throws InvalidXMLException {
Expand Down Expand Up @@ -63,31 +63,29 @@ private static boolean constant(MapFilePreprocessor ctx, Element el, String id)
value == null ? Cmp.DEFINED : Cmp.EQUALS);

var constants = ctx.getConstants();
var isDefined = constants.containsKey(id);
var constant = isDefined ? constants.get(id) : null;

if (!cmp.requireValue) {
if (value != null)
throw new InvalidXMLException("Comparison type " + cmp + " should not have a value", value);

if (!constants.containsKey(id)) return cmp == Cmp.UNDEFINED;

return switch (cmp) {
case DEFINED -> true;
case DEFINED_DELETE -> constants.get(id) == null;
case DEFINED_VALUE -> constants.get(id) != null;
case UNDEFINED -> !isDefined;
case DEFINED -> isDefined;
case DEFINED_DELETE -> isDefined && constant == null;
case DEFINED_VALUE -> isDefined && constant != null;
// Should never happen
default -> throw new IllegalStateException(cmp + " not supported");
};
} else {
String constant = constants.get(id);
if (constant == null) {
if (!constants.containsKey(id))
throw new InvalidXMLException(
"Unknown constant '" + id + "'. Only constants before the conditional may be used.",
el);
return false;
}
if (!isDefined)
throw new InvalidXMLException(
"Unknown constant '" + id + "'. Only constants before the conditional may be used.",
el);
if (value == null)
throw new InvalidXMLException("Required attribute 'constant-value' not set", el);
if (constant == null) return false;

return switch (cmp) {
case EQUALS -> Objects.equals(value.getValue(), constant);
Expand Down

0 comments on commit e0fc588

Please sign in to comment.