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

com.networknt.schema.annotation.JsonNodeAnnotationPredicate Maven / Gradle / Ivy

There is a newer version: 1.5.3
Show newest version
/*
 * Copyright (c) 2023 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.networknt.schema.annotation;

import java.util.function.Predicate;

import com.networknt.schema.JsonNodePath;
import com.networknt.schema.SchemaLocation;

/**
 * A predicate for filtering annotations.
 */
public class JsonNodeAnnotationPredicate implements Predicate {
    final Predicate instanceLocationPredicate;
    final Predicate evaluationPathPredicate;
    final Predicate schemaLocationPredicate;
    final Predicate keywordPredicate;
    final Predicate valuePredicate;

    /**
     * Initialize a new instance of this class.
     * 
     * @param instanceLocationPredicate for instanceLocation
     * @param evaluationPathPredicate   for evaluationPath
     * @param schemaLocationPredicate   for schemaLocation
     * @param keywordPredicate          for keyword
     * @param valuePredicate            for value
     */
    protected JsonNodeAnnotationPredicate(Predicate instanceLocationPredicate,
            Predicate evaluationPathPredicate, Predicate schemaLocationPredicate,
            Predicate keywordPredicate, Predicate valuePredicate) {
        super();
        this.instanceLocationPredicate = instanceLocationPredicate;
        this.evaluationPathPredicate = evaluationPathPredicate;
        this.schemaLocationPredicate = schemaLocationPredicate;
        this.keywordPredicate = keywordPredicate;
        this.valuePredicate = valuePredicate;
    }

    @Override
    public boolean test(JsonNodeAnnotation t) {
        return ((valuePredicate == null || valuePredicate.test(t.getValue()))
                && (keywordPredicate == null || keywordPredicate.test(t.getKeyword()))
                && (instanceLocationPredicate == null || instanceLocationPredicate.test(t.getInstanceLocation()))
                && (evaluationPathPredicate == null || evaluationPathPredicate.test(t.getEvaluationPath()))
                && (schemaLocationPredicate == null || schemaLocationPredicate.test(t.getSchemaLocation())));
    }

    /**
     * Gets the predicate to filter on instanceLocation.
     * 
     * @return the predicate
     */
    public Predicate getInstanceLocationPredicate() {
        return instanceLocationPredicate;
    }

    /**
     * Gets the predicate to filter on evaluationPath.
     * 
     * @return the predicate
     */
    public Predicate getEvaluationPathPredicate() {
        return evaluationPathPredicate;
    }

    /**
     * Gets the predicate to filter on schemaLocation.
     * 
     * @return the predicate
     */
    public Predicate getSchemaLocationPredicate() {
        return schemaLocationPredicate;
    }

    /**
     * Gets the predicate to filter on keyword.
     * 
     * @return the predicate
     */
    public Predicate getKeywordPredicate() {
        return keywordPredicate;
    }

    /**
     * Gets the predicate to filter on value.
     * 
     * @return the predicate
     */
    public Predicate getValuePredicate() {
        return valuePredicate;
    }

    /**
     * Creates a new builder to create the predicate.
     * 
     * @return the builder
     */
    public static Builder builder() {
        return new Builder();
    }

    /**
     * Builder for building a {@link JsonNodeAnnotationPredicate}.
     */
    public static class Builder {
        Predicate instanceLocationPredicate;
        Predicate evaluationPathPredicate;
        Predicate schemaLocationPredicate;
        Predicate keywordPredicate;
        Predicate valuePredicate;

        public Builder instanceLocation(Predicate instanceLocationPredicate) {
            this.instanceLocationPredicate = instanceLocationPredicate;
            return this;
        }

        public Builder evaluationPath(Predicate evaluationPathPredicate) {
            this.evaluationPathPredicate = evaluationPathPredicate;
            return this;
        }

        public Builder schema(Predicate schemaLocationPredicate) {
            this.schemaLocationPredicate = schemaLocationPredicate;
            return this;
        }

        public Builder keyword(Predicate keywordPredicate) {
            this.keywordPredicate = keywordPredicate;
            return this;
        }

        public Builder value(Predicate valuePredicate) {
            this.valuePredicate = valuePredicate;
            return this;
        }

        public JsonNodeAnnotationPredicate build() {
            return new JsonNodeAnnotationPredicate(instanceLocationPredicate, evaluationPathPredicate,
                    schemaLocationPredicate, keywordPredicate, valuePredicate);
        }
    }
}