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

net.jbock.annotated.ItemsFactory Maven / Gradle / Ivy

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

import io.jbock.simple.Inject;
import io.jbock.util.Either;
import net.jbock.common.ValidationFailure;
import net.jbock.processor.SourceElement;

import java.util.List;
import java.util.Optional;

public class ItemsFactory {

    private final SourceElement sourceElement;
    private final AbstractMethodsFinder abstractMethodsFinder;

    @Inject
    public ItemsFactory(
            SourceElement sourceElement,
            AbstractMethodsFinder abstractMethodsFinder) {
        this.sourceElement = sourceElement;
        this.abstractMethodsFinder = abstractMethodsFinder;
    }

    public Either, Items> createItems() {
        return abstractMethodsFinder.findAbstractMethods()
                .flatMap(ItemListFactory::createItemList)
                .flatMap(ItemListValidator::validate)
                .map(Items::createItems)
                .filter(this::validateAtLeastOneParameterInSuperCommand);
    }

    private Optional> validateAtLeastOneParameterInSuperCommand(
            Items items) {
        if (!sourceElement.isSuperCommand() ||
                !items.positionalParameters().isEmpty()) {
            return Optional.empty();
        }
        String message = "At least one @Parameter must be defined" +
                " in a @SuperCommand";
        return Optional.of(List.of(sourceElement.fail(message)));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy