All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.logz.sawmill.conditions.NotCondition Maven / Gradle / Ivy

The newest version!
package io.logz.sawmill.conditions;

import io.logz.sawmill.Condition;
import io.logz.sawmill.Doc;
import io.logz.sawmill.annotations.ConditionProvider;
import io.logz.sawmill.exceptions.ProcessorConfigurationException;
import io.logz.sawmill.parser.ConditionDefinition;
import io.logz.sawmill.parser.ConditionParser;
import io.logz.sawmill.utilities.JsonUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@ConditionProvider(type = "not", factory = NotCondition.Factory.class)
public class NotCondition implements Condition {

    private List conditions;

    public NotCondition(List conditions) {
        this.conditions = conditions;
    }

    @Override
    public boolean evaluate(Doc doc) {
        return conditions.stream().allMatch(condition -> !condition.evaluate(doc));
    }

    public static class Factory implements Condition.Factory {
        public Factory() {
        }

        @Override
        public Condition create(Map config, ConditionParser conditionParser) {
            Configuration configuration = JsonUtils.fromJsonMap(Configuration.class, config);
            List conditionDefinitions = configuration.getConditions();
            if (conditionDefinitions == null || conditionDefinitions.size() == 0) {
                throw new ProcessorConfigurationException("'Not' condition must contain a valid list of conditions, with at least one condition");
            }
            List conditions = conditionDefinitions.stream().map(conditionParser::parse).collect(Collectors.toList());

            return new NotCondition(conditions);
        }
    }

    public static class Configuration {
        private List conditions;

        public Configuration() {
        }

        public List getConditions() {
            return conditions;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy