net.jbock.validate.ParameterValidator 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.validate;
import io.jbock.simple.Inject;
import io.jbock.util.Either;
import net.jbock.annotated.Items;
import net.jbock.annotated.Parameter;
import net.jbock.common.ValidationFailure;
import net.jbock.convert.Mapping;
import net.jbock.convert.MappingFinder;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static io.jbock.util.Either.right;
import static io.jbock.util.Eithers.allFailures;
import static io.jbock.util.Eithers.optionalList;
import static io.jbock.util.Eithers.toOptionalList;
final class ParameterValidator {
private final MappingFinder mappingFinder;
@Inject
ParameterValidator(MappingFinder mappingFinder) {
this.mappingFinder = mappingFinder;
}
Either, List>> wrapPositionalParams(
Items items) {
return validatePositions(items.positionalParameters())
.flatMap(parameters -> parameters.stream()
.map(mappingFinder::findMapping)
.collect(allFailures()))
.filter(this::checkNoRequiredAfterOptional);
}
private Either, List> validatePositions(
List parameters) {
List failures = new ArrayList<>();
for (int i = 0; i < parameters.size(); i++) {
Parameter parameter = parameters.get(i);
int index = parameter.index();
if (index != i) {
failures.add(parameter.fail("invalid position: expecting " + i + " but found " + index));
}
}
return optionalList(failures)
., List>>map(Either::left)
.orElseGet(() -> right(parameters));
}
/* Left-Optional
*/
private Optional> checkNoRequiredAfterOptional(
List> positionalParameters) {
return positionalParameters.stream()
.filter(Mapping::isOptional)
.findFirst()
.map(Mapping::item)
.flatMap(firstOptional -> positionalParameters.stream()
.filter(Mapping::isRequired)
.map(Mapping::item)
.filter(sourceMethod -> sourceMethod.index()
> firstOptional.index())
.map(item -> item.fail("position of required parameter '" +
item.method().getSimpleName() +
"' is greater than position of optional parameter '" +
firstOptional.method().getSimpleName() + "'"))
.collect(toOptionalList()));
}
}