org.hibernate.testing.hamcrest.CollectionElementMatcher Maven / Gradle / Ivy
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
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" );
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy