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

com.github.fge.jsonschema.library.KeywordBuilder Maven / Gradle / Ivy

There is a newer version: 2.2.6
Show newest version
/*
 * Copyright (c) 2013, Francis Galiegue 
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the Lesser GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * Lesser GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */

package com.github.fge.jsonschema.library;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jsonschema.keyword.digest.helpers.IdentityDigester;
import com.github.fge.jsonschema.keyword.digest.helpers.SimpleDigester;
import com.github.fge.jsonschema.keyword.syntax.SyntaxChecker;
import com.github.fge.jsonschema.keyword.validator.KeywordValidator;
import com.github.fge.jsonschema.util.Digester;
import com.github.fge.jsonschema.util.NodeType;
import com.github.fge.jsonschema.util.Thawed;
import com.google.common.base.Preconditions;

import java.lang.reflect.Constructor;

public final class KeywordBuilder
    implements Thawed
{
    final String name;
    SyntaxChecker syntaxChecker;
    Digester digester;
    Constructor constructor;

    KeywordBuilder(final String name)
    {
        this.name = Preconditions.checkNotNull(name,
            "a keyword must have a name");
    }

    KeywordBuilder(final Keyword keyword)
    {
        name = keyword.name;
        syntaxChecker = keyword.syntaxChecker;
        digester = keyword.digester;
        constructor = keyword.constructor;
    }

    KeywordBuilder withSyntaxChecker(final SyntaxChecker syntaxChecker)
    {
        this.syntaxChecker = Preconditions.checkNotNull(syntaxChecker,
            "syntax checker must not be null");
        return this;
    }

    KeywordBuilder withDigester(final Digester digester)
    {
        this.digester = Preconditions.checkNotNull(digester,
            "digester must not be null");
        return this;
    }

    KeywordBuilder withIdentityDigester(final NodeType first,
        final NodeType... other)
    {
        digester = new IdentityDigester(name, first, other);
        return this;
    }

    KeywordBuilder withSimpleDigester(final NodeType first,
        final NodeType... other)
    {
        digester = new SimpleDigester(name, first, other);
        return this;
    }

    KeywordBuilder withValidatorClass(final Class c)
    {
        constructor = getConstructor(c);
        return this;
    }

    @Override
    public Keyword freeze()
    {
        return new Keyword(this);
    }


    private static Constructor getConstructor(
        final Class c)
    {
        try {
            return c.getConstructor(JsonNode.class);
        } catch (NoSuchMethodException e) {
            throw new IllegalStateException("No appropriate constructor", e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy