org.itsallcode.matcher.config.ConfigurableMatcher Maven / Gradle / Ivy
package org.itsallcode.matcher.config;
import org.itsallcode.matcher.*;
/**
* A {@link BaseTypeSafeDiagnosingMatcher} that can be configured with
* {@link MatcherConfig}.
*
* @param the type supported by the matcher
*/
public class ConfigurableMatcher extends BaseTypeSafeDiagnosingMatcher {
private final MatcherConfig config;
/**
* Create a new {@link ConfigurableMatcher} with the given configuration.
*
* @param config configuration
*/
public ConfigurableMatcher(final MatcherConfig config) {
super(config.getExpected());
this.config = config;
}
@Override
protected final void describeTo(final DescriptionBuilder description) {
for (final PropertyConfig property : this.config.getPropertyConfigs()) {
description.append(property.getPropertyName(), property.getMatcher());
}
}
@Override
protected final void reportMismatches(final T actual, final MismatchReporter mismatchReporter) {
for (final PropertyConfig property : this.config.getPropertyConfigs()) {
reportMismatch(actual, mismatchReporter, property);
}
}
private void reportMismatch(final T actual, final MismatchReporter mismatchReporter,
final PropertyConfig property) {
mismatchReporter.checkMismatch(property.getPropertyName(), property.getMatcher(),
property.getPropertyValue(actual));
}
}