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

net.thucydides.core.matchers.BeanUniquenessMatcher 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 java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static ch.lambdaj.Lambda.convert;
import static net.thucydides.core.matchers.dates.BeanFields.fieldValueIn;

class BeanUniquenessMatcher implements BeanCollectionMatcher {

    private final String fieldName;
    
    public BeanUniquenessMatcher(String fieldName) {
        this.fieldName = fieldName;
    }

    @Override
    public boolean matches(Object target) {
        return matches((Collection) target);  //To change body of implemented methods use File | Settings | File Templates.
    }

    public  boolean matches(Collection elements) {
        List allFieldValues = convert(elements, new FieldValueExtractor());
        Set uniquefieldValues = new HashSet();

        uniquefieldValues.addAll(allFieldValues);

        return (uniquefieldValues.size() == elements.size());
    }

    public class FieldValueExtractor implements Converter {
        @Override
        public Object convert(Object bean) {
            return fieldValueIn(bean).forField(fieldName);
        }
    }

    @Override
    public String toString() {
        return "each " + fieldName + " is different";
    }

}