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

org.testifyproject.bytebuddy.matcher.CollectionOneToOneMatcher Maven / Gradle / Ivy

The newest version!
package org.testifyproject.bytebuddy.matcher;

import lombok.EqualsAndHashCode;

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

/**
 * An element matcher that matches a given iterable collection to a list of matchers on a per-element basis. For a
 * successful match, any element of the matched iterable collection must be successfully matched by a next
 * matcher of the supplied list of element matchers. For this to be possible, the matched iterable collection
 * and the supplied list of element matchers contain the same number of elements.
 *
 * @param  The type of the matched entity.
 */
@EqualsAndHashCode(callSuper = false)
public class CollectionOneToOneMatcher extends ElementMatcher.Junction.AbstractBase> {

    /**
     * The list of element matchers to match any elements of the matched iterable collection against.
     */
    private final List> matchers;

    /**
     * Creates a new matcher that compares a matched iterable collection against a list of element matchers.
     *
     * @param matchers The list of element matchers to match any elements of the matched iterable collection
     *                        against.
     */
    public CollectionOneToOneMatcher(List> matchers) {
        this.matchers = matchers;
    }

    @Override
    public boolean matches(Iterable target) {
        if ((target instanceof Collection) && ((Collection) target).size() != matchers.size()) {
            return false;
        }
        Iterator> iterator = matchers.iterator();
        for (T value : target) {
            if (!iterator.hasNext() || !iterator.next().matches(value)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder("containing(");
        boolean first = true;
        for (Object value : matchers) {
            if (first) {
                first = false;
            } else {
                stringBuilder.append(", ");
            }
            stringBuilder.append(value);
        }
        return stringBuilder.append(")").toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy