fr.vergne.pester.definition.PropertyDefinition Maven / Gradle / Ivy
Show all versions of pester-core Show documentation
package fr.vergne.pester.definition;
import static java.util.function.Predicate.*;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import fr.vergne.pester.options.Mutability;
import fr.vergne.pester.options.Option;
import fr.vergne.pester.options.Scope;
import fr.vergne.pester.options.Visibility;
import fr.vergne.pester.util.namer.NameNamer;
import fr.vergne.pester.util.namer.Namer;
import fr.vergne.pester.util.namer.PredicateNamer;
import fr.vergne.pester.value.Generator;
import fr.vergne.pester.value.Modifier;
import fr.vergne.pester.value.Nullable;
import fr.vergne.pester.value.Type;
public class PropertyDefinition {
private final Class
pojoClass;
private final String name;
private final Type type;
private Optional> fieldDefinition = Optional.empty();
private Optional> getterDefinition = Optional.empty();
private Optional> setterDefinition = Optional.empty();
private Optional> modifier = Optional.empty();
private Optional> generator = Optional.empty();
private Optional> defaultValue = Optional.empty();
public PropertyDefinition(Class pojoClass, String name, Type type) {
this.pojoClass = pojoClass;
this.name = name;
this.type = type;
}
// BASE
public String getName() {
return name;
}
public Type getType() {
return type;
}
// FIELD
public PropertyDefinition withField(Option... options) {
return withField(name, options);
}
public PropertyDefinition
withField(String name, Option... options) {
return withFieldInternal(name, extractOption(options, Mutability.class), extractOption(options, Visibility.class), extractOption(options, Scope.class));
}
private static Optional extractOption(Option[] options, Class optionType) {
List providedOptions = Stream.of(options)
.distinct()
.filter(optionType::isInstance)
.map(optionType::cast)
.collect(Collectors.toList());
if (providedOptions.size() > 1) {
throw new IllegalArgumentException(String.format("More than one %s provided: %s", optionType.getSimpleName(), providedOptions));
} else {
return providedOptions.stream().findFirst();
}
}
private PropertyDefinition withFieldInternal(String name, Optional mutability, Optional visibility, Optional scope) {
this.fieldDefinition = Optional.of(new FieldDefinition<>(pojoClass, name, visibility, mutability, scope));
return this;
}
public Optional> getFieldDefinition() {
return fieldDefinition;
}
// GETTER
private final Predicate