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

org.hamcrest.collection.IsIn Maven / Gradle / Ivy

There is a newer version: 3.0
Show newest version
package org.hamcrest.collection;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;

import java.util.Arrays;
import java.util.Collection;

public class IsIn extends BaseMatcher {
    private final Collection collection;

    public IsIn(Collection collection) {
        this.collection = collection;
    }
    
    public IsIn(T[] elements) {
        collection = Arrays.asList(elements);
    }
    
    @Override
    public boolean matches(Object o) {
        return collection.contains(o);
    }

    @Override
    public void describeTo(Description buffer) {
        buffer.appendText("one of ");
        buffer.appendValueList("{", ", ", "}", collection);
    }
    
    /**
     * Creates a matcher that matches when the examined object is found within the
     * specified collection.
     * 

* For example: *

assertThat("foo", isIn(Arrays.asList("bar", "foo")))
* * @param collection * the collection in which matching items must be found * */ @Factory public static Matcher isIn(Collection collection) { return new IsIn(collection); } /** * Creates a matcher that matches when the examined object is found within the * specified array. *

* For example: *

assertThat("foo", isIn(new String[]{"bar", "foo"}))
* * @param elements * the array in which matching items must be found * */ @Factory public static Matcher isIn(T[] elements) { return new IsIn(elements); } /** * Creates a matcher that matches when the examined object is equal to one of the * specified elements. *

* For example: *

assertThat("foo", isIn("bar", "foo"))
* * @param elements * the elements amongst which matching items will be found * */ @Factory public static Matcher isOneOf(T... elements) { return isIn(elements); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy