All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.vpro.domain.api.AbstractMultipleResult Maven / Gradle / Ivy

Go to download

Contains the objects used by the Frontend API, like forms and result objects

There is a newer version: 8.3.3
Show newest version
package nl.vpro.domain.api;

import java.util.AbstractList;
import java.util.List;
import java.util.function.BiFunction;

import javax.xml.bind.annotation.XmlTransient;

import nl.vpro.domain.constraint.DisplayablePredicate;
import nl.vpro.domain.constraint.PredicateTestResult;

/**
 * @author Michiel Meeuwissen
 * @since 4.7
 */
public abstract class AbstractMultipleResult extends Result> {

    @XmlTransient
    protected final BiFunction> producer;

    public AbstractMultipleResult(BiFunction> producer) {
        this.producer = producer;
        this.offset = null;
    }

    public AbstractMultipleResult(BiFunction> producer, List ids, List objects) {
        this(producer, ids, objects, null);
    }

    public AbstractMultipleResult(BiFunction> producer, List ids, List objects, DisplayablePredicate predicate) {
        this(producer);
        this.items = adapt(ids, objects, predicate);
        this.total = (long) objects.size();
        this.totalQualifier = TotalQualifier.EQUAL_TO;
        this.offset = null;
        this.max = null;

    }

    private List> adapt(final List ids, final List list, DisplayablePredicate predicate) {
        return new AbstractList>() {
            @Override
            public MultipleEntry get(int index) {
                MultipleEntry entry = producer.apply(ids.get(index), list.get(index));
                if (predicate != null) {
                    PredicateTestResult result = predicate.testWithReason(entry.getResult());
                    if (! result.applies()) {
                        entry.setError("Does not match profile " + predicate.toString() + ": " + ids.get(index));
                        entry.setReason(result);
                        entry.setResult(null); // ?
                    }
                }

                return entry;
            }

            @Override
            public int size() {
                return list.size();
            }
        };
    }

    public Result toResult() {
        return stream().map(MultipleEntry::getResult).collect(Result.collector());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy