nl.vpro.domain.constraint.AbstractOr Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of api-domain Show documentation
Show all versions of api-domain Show documentation
Contains the objects used by the Frontend API, like forms and result objects
/*
* Copyright (C) 2013 All rights reserved
* VPRO The Netherlands
*/
package nl.vpro.domain.constraint;
import java.util.*;
import java.util.stream.Collectors;
import javax.el.ELContext;
import javax.xml.bind.annotation.*;
import org.checkerframework.checker.nullness.qual.Nullable;
import static nl.vpro.domain.constraint.PredicateTestResult.FACTORY;
/**
* See https://jira.vpro.nl/browse/API-
*
* @author Roelof Jan Koekoek
* @since 2.0
*/
@XmlAccessorType(XmlAccessType.NONE)
@XmlTransient
public abstract class AbstractOr implements Constraint {
protected List> constraints = new ArrayList<>();
protected AbstractOr() {
}
@SafeVarargs
protected AbstractOr(Constraint... constraints) {
this(new ArrayList<>(Arrays.asList(constraints)));
}
protected AbstractOr(List> constraints) {
this.constraints = constraints;
}
public List> getConstraints() {
return constraints;
}
public void setConstraint(List> constraints) {
this.constraints = constraints;
}
@Override
public boolean test(@Nullable T t) {
return constraints != null &&
constraints
.stream()
.filter(Objects::nonNull)
.anyMatch(p -> p.test(t));
}
@Override
public OrPredicateTestResult testWithReason(@Nullable T t) {
return DisplayablePredicates.or(this, t, testsWithReason(t));
}
public List testsWithReason(@Nullable T t) {
return constraints.stream().map(c -> c.testWithReason(t)).collect(Collectors.toList());
}
@Override
public void setELContext(ELContext ctx, Object value, Locale locale, PredicateTestResult result) {
Constraint.super.setELContext(ctx, value, locale, result);
OrPredicateTestResult orResult = (OrPredicateTestResult) result;
ResourceBundle bundle = DisplayablePredicates.getBundleForFalse(this, locale);
List clauses = orResult.getClauses()
.stream().map(p -> p.getDescription(locale))
.collect(Collectors.toList());
String joinedClauses = clauses.stream()
.collect(Collectors.joining(" " + bundle.getString("OR") + " "));
ctx.getVariableMapper().setVariable("clauses",
FACTORY.createValueExpression(clauses, List.class));
ctx.getVariableMapper().setVariable("joinedclauses",
FACTORY.createValueExpression(joinedClauses, String.class));
}
@Override
public String toString() {
return constraints.stream().map(Object::toString).collect(Collectors.joining(" OR "));
}
@Override
public List getDefaultBundleKey() {
List result = Constraint.super.getDefaultBundleKey();
result.add(0, "Or");
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy