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

net.jbock.validate.OptionValidator Maven / Gradle / Ivy

There is a newer version: 5.18
Show newest version
package net.jbock.validate;

import io.jbock.simple.Inject;
import io.jbock.util.Either;
import net.jbock.annotated.Items;
import net.jbock.annotated.Option;
import net.jbock.common.ValidationFailure;
import net.jbock.convert.Mapping;
import net.jbock.convert.MappingFinder;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import static io.jbock.util.Either.left;
import static io.jbock.util.Either.right;
import static io.jbock.util.Eithers.allFailures;
import static io.jbock.util.Eithers.toOptionalList;
import static java.lang.Character.isWhitespace;
import static javax.lang.model.type.TypeKind.BOOLEAN;

final class OptionValidator {

    private final MappingFinder mappingFinder;

    @Inject
    OptionValidator(MappingFinder mappingFinder) {
        this.mappingFinder = mappingFinder;
    }

    Either, List>> wrapOptions(
            Items items) {
        return items.namedOptions().stream()
                .map(this::checkOptionNames)
                .collect(allFailures())
                .filter(this::validateUniqueOptionNames)
                .flatMap(sourceOptions -> sourceOptions.stream()
                        .map(this::wrapOption)
                        .collect(allFailures()));
    }

    private Either> wrapOption(
            Option option) {
        return checkNullary(option)
                .map(mappingFinder::findNullaryMapping)
                .orElseGet(() -> mappingFinder.findMapping(option));
    }

    private Optional




© 2015 - 2024 Weber Informatics LLC | Privacy Policy