net.serenitybdd.screenplay.Question Maven / Gradle / Ivy
package net.serenitybdd.screenplay;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
public interface Question {
ANSWER answeredBy(Actor actor);
default String getSubject() {
return "";
}
static QuestionBuilder about(String subject) {
return new QuestionBuilder(subject);
}
default Question asBoolean() {
return Question.about(getSubject()).answeredBy(actor -> Boolean.parseBoolean(String.valueOf(this.answeredBy(actor))));
}
static Question not(Question question) {
return Question.about(question.getSubject()).answeredBy(actor -> !question.answeredBy(actor));
}
default Question asString() {
return Question.about(getSubject()).answeredBy(actor -> String.valueOf(this.answeredBy(actor)));
}
default Question asInteger() {
return Question.about(getSubject()).answeredBy(actor -> Integer.parseInt(String.valueOf(this.answeredBy(actor))));
}
default Question asDouble() {
return Question.about(getSubject()).answeredBy(actor -> Double.parseDouble(String.valueOf(this.answeredBy(actor))));
}
default Question asFloat() {
return Question.about(getSubject()).answeredBy(actor -> Float.parseFloat(String.valueOf(this.answeredBy(actor))));
}
default Question asLong() {
return Question.about(getSubject()).answeredBy(actor -> Long.parseLong(String.valueOf(this.answeredBy(actor))));
}
default Question asBigDecimal() {
return Question.about(getSubject()).answeredBy(actor -> new BigDecimal(String.valueOf(this.answeredBy(actor))));
}
default Question asADate() {
return Question.about(getSubject()).answeredBy(actor -> LocalDate.parse(String.valueOf(this.answeredBy(actor))));
}
default Question asADate(String format) {
return Question.about(getSubject()).answeredBy(actor -> LocalDate.parse(String.valueOf(this.answeredBy(actor)), DateTimeFormatter.ofPattern(format)));
}
default Question asEnum(Class enumType) {
return Question.about(getSubject())
.answeredBy(actor -> EnumValues.forType(enumType).getValueOf(String.valueOf(this.answeredBy(actor))));
}
/**
* Convert the answer to a question into another form using an arbitrary function.
*
* @param transformer
* @param
* @return
*/
default Question map(Function transformer) {
return Question.about(getSubject()).answeredBy(
actor -> transformer.apply(answeredBy(actor))
);
}
/**
* Convert all the matching answers to a question into another form using an arbitrary function.
*/
default Question> mapEach(Function transformer) {
return (actor) -> ((List) this.answeredBy(actor)).stream().map(
value -> (T) transformer.apply(value)
).collect(Collectors.toList());
}
/**
* Returns a new question with the specified text as a subject.
*
* @param description
* @return
*/
default Question describedAs(String description) {
return Question.about(description).answeredBy(this::answeredBy);
}
default Question as(Class type) {
return actor -> (ANSWER) DefaultConverters.converterFor(type).convert(answeredBy(actor));
}
default Question> asListOf(Class type) {
return (actor) -> ((List) this.answeredBy(actor)).stream().map(
value -> (T) DefaultConverters.converterFor(type).convert(value)
).collect(Collectors.toList());
}
default Question> asCollectionOf(Class type) {
return (actor) -> ((List) this.answeredBy(actor)).stream().map(
value -> (T) DefaultConverters.converterFor(type).convert(value)
).collect(Collectors.toList());
}
}