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

net.thucydides.core.matchers.MinFieldValueMatcher 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.min;
import static net.thucydides.core.matchers.dates.BeanFields.fieldValueIn;

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

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

    public  boolean matches(Collection elements) {
        List fieldValues = convert(elements, toComparable());
        return valueMatcher.matches(min(fieldValues));
    }

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

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy