Respackopts/src/main/java/io/gitlab/jfronny/respackopts/model/condition/RpoBooleanCondition.java

42 lines
1.6 KiB
Java

package io.gitlab.jfronny.respackopts.model.condition;
import com.google.gson.JsonParseException;
import io.gitlab.jfronny.respackopts.util.MetaCache;
import io.gitlab.jfronny.respackopts.model.ThrowingBiConsumer;
import io.gitlab.jfronny.respackopts.model.tree.ConfigBranch;
import io.gitlab.jfronny.respackopts.util.RpoFormatException;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
public record RpoBooleanCondition(String name) implements Condition {
public RpoBooleanCondition {
if (name == null)
throw new JsonParseException("Condition must not be null");
}
@Override
public boolean evaluate(String packId) throws RpoFormatException {
String condition = name;
if (!condition.contains(":")) {
condition = packId + ":" + condition;
}
String sourcePack = condition.split(":")[0];
condition = condition.substring(condition.indexOf(':') + 1);
final String finalCondition = condition;
AtomicReference<Boolean> result = new AtomicReference<>(null);
MetaCache.forEach((ThrowingBiConsumer<String, ConfigBranch, ? extends RpoFormatException>) (id, branch) -> {
if (Objects.equals(id, sourcePack)) {
try {
result.set(branch.getBoolean(finalCondition));
} catch (RpoFormatException ex) {
throw new RpoFormatException("Could not get value", ex);
}
}
});
if (result.get() == null)
throw new RpoFormatException("Could not find pack with specified ID: " + packId);
return result.get();
}
}