net.jbock.annotated.ItemsFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jbock-compiler Show documentation
Show all versions of jbock-compiler Show documentation
jbock annotation processor
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)));
}
}