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

net.thucydides.core.matchers.MaxFieldValueMatcher Maven / Gradle / Ivy

There is a newer version: 4.2.1
Show newest version
package net.thucydides.core.matchers;

import ch.lambdaj.function.convert.Converter;
import org.hamcrest.Matcher;

import java.util.Collection;
import java.util.List;

import static ch.lambdaj.Lambda.convert;
import static java.util.Collections.max;
import static net.thucydides.core.matchers.dates.BeanFields.fieldValueIn;

class MaxFieldValueMatcher implements BeanCollectionMatcher {
    private final String fieldName;
    private final Matcher valueMatcher;

    public MaxFieldValueMatcher(String fieldName, Matcher valueMatcher) {
        this.fieldName = fieldName;
        this.valueMatcher = valueMatcher;
    }

    @Override
    public boolean matches(Object target) {
        return matches((Collection) target);
    }

    public  boolean matches(Collection elements) {
        Comparable maximumValue = null;
        try {
            List fieldValues = convert(elements, toComparable());
            maximumValue = max(fieldValues);
        } catch (Exception e) {
            throw new IllegalArgumentException("Could not find property value for " + fieldName);
        }
        return valueMatcher.matches(maximumValue);
    }

    private  Converter toComparable() {
        return new Converter() {
            @Override
            public Comparable convert(T bean) {
                return (Comparable) fieldValueIn(bean).forField(fieldName);
            }
        };
    }

    @Override
    public String toString() {
        return "the maximum " + fieldName + " " + valueMatcher.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy