com.querydsl.collections.PathMatcher Maven / Gradle / Ivy
Show all versions of querydsl-collections Show documentation
/*
* Copyright 2015, The Querydsl Team (http://www.querydsl.com/team)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.querydsl.collections;
import static org.hamcrest.core.IsNull.notNullValue;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import com.google.common.base.Function;
import com.querydsl.core.types.Path;
/**
* Matches based on the current value of a path.
*
* @author Jeroen van Schagen
* @author tiwe
*
* @param type of the path root
* @param type of value being matched
*/
public class PathMatcher extends TypeSafeDiagnosingMatcher {
private final Function accessor;
private final Matcher super V> matcher;
private final Path path;
public PathMatcher(Path path, Matcher super V> matcher) {
this(path, matcher, GuavaHelpers.wrap(path));
}
public PathMatcher(Path path, Matcher super V> matcher, Function accessor) {
this.path = path;
this.matcher = matcher;
this.accessor = accessor;
}
@Factory
public static Matcher hasValue(Path path) {
return new PathMatcher(path, notNullValue());
}
@Factory
public static Matcher hasValue(Path path, Matcher super P> matcher) {
return new PathMatcher(path, matcher);
}
@Override
protected boolean matchesSafely(T bean, Description mismatchDescription) {
V value = accessor.apply(bean);
boolean valueMatches = matcher.matches(value);
if (!valueMatches) {
mismatchDescription.appendText("value \"" + path.toString() + "\" ");
matcher.describeMismatch(value, mismatchDescription);
}
return valueMatches;
}
@Override
public void describeTo(Description description) {
description.appendText("valueOf(");
description.appendValue(path.toString());
description.appendText(", ");
description.appendDescriptionOf(matcher);
description.appendText(")");
}
}