<filename>webtester-core/src/main/java/info/novatec/testit/webtester/utils/conditions/SelectedValues.java
package info.novatec.testit.webtester.utils.conditions;

import java.util.Collection;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import com.google.common.base.Predicate;
import com.google.common.collect.Sets;

import info.novatec.testit.webtester.pageobjects.Select;


/**
 * Predicate to be used in order to check if a {@link Select multi-select} has a
 * certain set of selected values. This will <u>not</u> check weather or not they
 * are the only selected values!
 * <p>
 * <b>Example:</b> Given a selection of the values "foo", "bar" and "xuu", a check
 * for "foo" and "bar" will return true and a check for "foo" and "kuu" will return false.
 *
 * @since 0.9.9
 */
public class SelectedValues implements Predicate<Select> {

    private Set<String> expectedValues;

    public SelectedValues(String... values) {
        this(Sets.newHashSet(values));
    }

    public SelectedValues(Collection<String> values) {
        this.expectedValues = Sets.newHashSet(values);
    }

    @Override
    public boolean apply(Select select) {
        return select.getAllSelectedValues().containsAll(expectedValues);
    }

    @Override
    public String toString() {
        return String.format("selected values: %s", StringUtils.join(expectedValues, ", "));
    }

}
