1 package com.google.code.beanmatchers; 2 3 import static java.util.Arrays.asList; 4 5 import java.util.List; 6 import org.hamcrest.Description; 7 8 public class HasValidBeanHashCodeExcludingMatcher<T> extends AbstractBeanHashCodeMatcher<T> { 9 private final List<String> excludedProperties; 10 11 HasValidBeanHashCodeExcludingMatcher( 12 TypeBasedValueGenerator valueGenerator, String... excludedProperties) { 13 super(valueGenerator); 14 this.excludedProperties = asList(excludedProperties); 15 } 16 17 @Override 18 protected boolean matchesSafely(Class<T> beanType, Description mismatchDescription) { 19 JavaBean bean = new JavaBean(beanType); 20 List<String> properties = bean.properties(); 21 properties.removeAll(excludedProperties); 22 return hashCodeIsInfluencedByProperties(bean, properties, mismatchDescription); 23 } 24 25 @Override 26 public void describeTo(Description description) { 27 if (excludedProperties.isEmpty()) { 28 description.appendText("bean with all properties influencing hashCode"); 29 } else { 30 description.appendText("bean with all properties excluding "); 31 description.appendValue(excludedProperties); 32 description.appendText(" influencing hashCode"); 33 } 34 } 35 }