org.hibernate.testing.hamcrest.CollectionElementMatcher Maven / Gradle / Ivy
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.testing.hamcrest;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
/**
* @author Steve Ebersole
*/
public class CollectionElementMatcher> extends BaseMatcher {
public static Matcher> hasAllOf(Matcher... elementMatchers) {
return new CollectionElementMatcher<>( elementMatchers );
}
private final List> elementMatchers;
public CollectionElementMatcher(Matcher... elementMatchers) {
this.elementMatchers = Arrays.asList( elementMatchers );
}
@Override
public boolean matches(Object o) {
assert o instanceof Collection;
final Collection collection = (Collection) o;
outer: for ( Matcher valueMatcher : elementMatchers ) {
for ( Object value : collection ) {
if ( valueMatcher.matches( value ) ) {
continue outer;
}
}
return false;
}
return true;
}
@Override
public void describeTo(Description description) {
description.appendText( "contained" );
}
}