fr.vergne.pester.definition.ConstructorDefinition Maven / Gradle / Ivy
Show all versions of pester-core Show documentation
package fr.vergne.pester.definition;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import fr.vergne.pester.model.Constructor;
import fr.vergne.pester.options.Visibility;
import fr.vergne.pester.value.Generator;
import fr.vergne.pester.value.Modifier;
import fr.vergne.pester.value.Type;
public class ConstructorDefinition implements InstanciableDefinition> {
private final Constructor instance;
private final Optional visibility;
private final List> parametersDefinitions;
private final Generator> parametersGenerator;
private final Modifier> parametersModifier;
public ConstructorDefinition(Class pojoClass, List> parameters, Optional visibility) {
this.visibility = visibility;
this.parametersDefinitions = parameters;
this.parametersGenerator = () -> parameters.stream()
.map(PropertyDefinition::getGenerator)
.map(Generator::create)
.collect(Collectors.toList());
this.parametersModifier = values -> IntStream.range(0, values.size())
.mapToObj(i -> newValue(parameters.get(i), values.get(i)))
.collect(Collectors.toList());
List> parametersTypes = parameters.stream()
.map(PropertyDefinition::getType)
.collect(Collectors.toList());
this.instance = new Constructor<>(pojoClass, parametersTypes);
}
@SuppressWarnings("unchecked")
private static T newValue(PropertyDefinition
property, Object value) {
return property.getModifier().modify((T) value);
}
@Override
public Constructor
getInstance() {
return instance;
}
public Optional getVisibility() {
return visibility;
}
public Generator> getParametersGenerator() {
return parametersGenerator;
}
public Modifier> getParametersModifier() {
return parametersModifier;
}
public List> getParametersDefinitions() {
return parametersDefinitions;
}
}