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

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

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

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import lombok.EqualsAndHashCode;

import java.util.Collection;

/**
 * An element matcher that matches a collection by its size.
 *
 * @param  The type of the matched entity.
 */
@EqualsAndHashCode(callSuper = false)
public class CollectionSizeMatcher> extends ElementMatcher.Junction.AbstractBase {

    /**
     * The expected size of the matched collection.
     */
    private final int size;

    /**
     * Creates a new matcher that matches the size of a matched collection.
     *
     * @param size The expected size of the matched collection.
     */
    public CollectionSizeMatcher(int size) {
        this.size = size;
    }

    @Override
    @SuppressFBWarnings(value = "DLS_DEAD_LOCAL_STORE", justification = "Iteration required to count size of an iterable")
    public boolean matches(T target) {
        if (target instanceof Collection) {
            return ((Collection) target).size() == size;
        } else {
            int size = 0;
            for (Object ignored : target) {
                size++;
            }
            return size == this.size;
        }
    }

    @Override
    public String toString() {
        return "ofSize(" + size + ')';
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy