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

org.fluentlenium.core.conditions.BaseObjectListConditions Maven / Gradle / Ivy

package org.fluentlenium.core.conditions;

import org.fluentlenium.core.domain.FluentWebElement;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;

import static java.util.stream.Collectors.toList;

/**
 * Base condition for list of objects
 *
 * @param  type of underlying object in the list
 * @param  type of conditions
 */
public class BaseObjectListConditions> implements ConditionsObject> {
    protected Conditions conditions;
    protected final Function objectGetter;
    protected final Function conditionsGetter;

    /**
     * Creates a new list conditions
     *
     * @param conditions       object conditions
     * @param objectGetter     getter of the underlying object
     * @param conditionsGetter getter of the underlying object conditions
     */
    public BaseObjectListConditions(Conditions conditions, Function objectGetter,
            Function conditionsGetter) {
        this.conditions = conditions;
        this.objectGetter = objectGetter;
        this.conditionsGetter = conditionsGetter;
    }

    @Override
    public List getActualObject() {
        if (conditions instanceof ListConditionsElements) {
            List elements = ((ListConditionsElements) conditions).getActualElements();
            return elements.stream().map(objectGetter).collect(toList());
        }
        return new ArrayList<>();
    }

    /**
     * Verify the predicate against the condition.
     *
     * @param predicate predicate
     * @return true if the predicate is verified
     */
    public boolean verify(Predicate predicate) {
        return conditions.verify(input -> predicate.test(objectGetter.apply(input)));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy