aboutsummaryrefslogtreecommitdiff
path: root/hamcrest-core/src/main/java/org/hamcrest/FeatureMatcher.java
diff options
context:
space:
mode:
authorPaul Duffin <paulduffin@google.com>2017-02-24 13:38:19 +0000
committerandroid-build-merger <android-build-merger@google.com>2017-02-24 13:38:19 +0000
commit2c2a845e1e8fdda704e4f2ccaa335d69d043ebf3 (patch)
tree873cd708d3c6fb36684c53bc506ff3940c3ec3f0 /hamcrest-core/src/main/java/org/hamcrest/FeatureMatcher.java
parent9432ade17c16656f8c3ebbe2b7f3081a93c52d4d (diff)
parentc2e228b07b9dd8266a6eeabb34e27233733ef127 (diff)
downloadhamcrest-2cb406bbaeb4af8891f41e85593317bd634623d2.tar.gz
Merge changes from topic 'upgrade-hamcrest-to-2.0' am: f324aac9e2 am: 0d446c0a6fandroid-wear-8.1.0_r1android-vts-8.1_r9android-vts-8.1_r8android-vts-8.1_r7android-vts-8.1_r6android-vts-8.1_r5android-vts-8.1_r4android-vts-8.1_r3android-vts-8.1_r14android-vts-8.1_r13android-vts-8.1_r12android-vts-8.1_r11android-vts-8.1_r10android-vts-8.0_r2android-vts-8.0_r1android-security-8.1.0_r93android-security-8.1.0_r92android-security-8.1.0_r91android-security-8.1.0_r90android-security-8.1.0_r89android-security-8.1.0_r88android-security-8.1.0_r87android-security-8.1.0_r86android-security-8.1.0_r85android-security-8.1.0_r84android-security-8.1.0_r83android-security-8.1.0_r82android-o-mr1-preview-2android-o-mr1-preview-1android-cts-8.1_r9android-cts-8.1_r8android-cts-8.1_r7android-cts-8.1_r6android-cts-8.1_r5android-cts-8.1_r4android-cts-8.1_r3android-cts-8.1_r25android-cts-8.1_r24android-cts-8.1_r23android-cts-8.1_r22android-cts-8.1_r21android-cts-8.1_r20android-cts-8.1_r2android-cts-8.1_r19android-cts-8.1_r18android-cts-8.1_r17android-cts-8.1_r16android-cts-8.1_r15android-cts-8.1_r14android-cts-8.1_r13android-cts-8.1_r12android-cts-8.1_r11android-cts-8.1_r10android-cts-8.1_r1android-8.1.0_r9android-8.1.0_r81android-8.1.0_r80android-8.1.0_r8android-8.1.0_r79android-8.1.0_r78android-8.1.0_r77android-8.1.0_r76android-8.1.0_r75android-8.1.0_r74android-8.1.0_r73android-8.1.0_r72android-8.1.0_r71android-8.1.0_r70android-8.1.0_r7android-8.1.0_r69android-8.1.0_r68android-8.1.0_r67android-8.1.0_r66android-8.1.0_r65android-8.1.0_r64android-8.1.0_r63android-8.1.0_r62android-8.1.0_r61android-8.1.0_r60android-8.1.0_r6android-8.1.0_r53android-8.1.0_r52android-8.1.0_r51android-8.1.0_r50android-8.1.0_r5android-8.1.0_r48android-8.1.0_r47android-8.1.0_r46android-8.1.0_r45android-8.1.0_r43android-8.1.0_r42android-8.1.0_r41android-8.1.0_r40android-8.1.0_r4android-8.1.0_r39android-8.1.0_r38android-8.1.0_r37android-8.1.0_r36android-8.1.0_r35android-8.1.0_r33android-8.1.0_r32android-8.1.0_r31android-8.1.0_r30android-8.1.0_r3android-8.1.0_r29android-8.1.0_r28android-8.1.0_r27android-8.1.0_r26android-8.1.0_r25android-8.1.0_r23android-8.1.0_r22android-8.1.0_r21android-8.1.0_r20android-8.1.0_r2android-8.1.0_r19android-8.1.0_r18android-8.1.0_r17android-8.1.0_r16android-8.1.0_r15android-8.1.0_r14android-8.1.0_r13android-8.1.0_r12android-8.1.0_r11android-8.1.0_r10android-8.1.0_r1android-8.0.0_r34android-8.0.0_r33android-8.0.0_r27android-8.0.0_r26android-8.0.0_r25android-8.0.0_r24android-8.0.0_r23android-8.0.0_r22android-8.0.0_r21security-oc-mr1-releaseoreo-mr1-wear-releaseoreo-mr1-vts-releaseoreo-mr1-security-releaseoreo-mr1-s1-releaseoreo-mr1-releaseoreo-mr1-devoreo-mr1-cuttlefish-testingoreo-mr1-cts-releaseoreo-m8-releaseoreo-m7-releaseoreo-m6-s4-releaseoreo-m6-s3-releaseoreo-m6-s2-releaseoreo-m5-releaseoreo-m4-s9-releaseoreo-m4-s8-releaseoreo-m4-s7-releaseoreo-m4-s6-releaseoreo-m4-s5-releaseoreo-m4-s4-releaseoreo-m4-s3-releaseoreo-m4-s2-releaseoreo-m4-s12-releaseoreo-m4-s11-releaseoreo-m4-s10-releaseoreo-m4-s1-releaseoreo-m3-releaseoreo-m2-s5-releaseoreo-m2-s4-releaseoreo-m2-s3-releaseoreo-m2-s2-releaseoreo-m2-s1-releaseoreo-m2-releaseoreo-dr3-releaseoreo-dr2-releaseoreo-dr1-releaseoreo-dr1-devoreo-dev
am: c2e228b07b Change-Id: I737b987a8b22811c7ea058bcd9898b58694b58ce
Diffstat (limited to 'hamcrest-core/src/main/java/org/hamcrest/FeatureMatcher.java')
-rw-r--r--hamcrest-core/src/main/java/org/hamcrest/FeatureMatcher.java54
1 files changed, 54 insertions, 0 deletions
diff --git a/hamcrest-core/src/main/java/org/hamcrest/FeatureMatcher.java b/hamcrest-core/src/main/java/org/hamcrest/FeatureMatcher.java
new file mode 100644
index 0000000..385cf99
--- /dev/null
+++ b/hamcrest-core/src/main/java/org/hamcrest/FeatureMatcher.java
@@ -0,0 +1,54 @@
+package org.hamcrest;
+
+import org.hamcrest.internal.ReflectiveTypeFinder;
+
+/**
+ * Supporting class for matching a feature of an object. Implement <code>featureValueOf()</code>
+ * in a subclass to pull out the feature to be matched against.
+ *
+ * @param <T> The type of the object to be matched
+ * @param <U> The type of the feature to be matched
+ */
+public abstract class FeatureMatcher<T, U> extends TypeSafeDiagnosingMatcher<T> {
+ private static final ReflectiveTypeFinder TYPE_FINDER = new ReflectiveTypeFinder("featureValueOf", 1, 0);
+ private final Matcher<? super U> subMatcher;
+ private final String featureDescription;
+ private final String featureName;
+
+ /**
+ * Constructor
+ * @param subMatcher The matcher to apply to the feature
+ * @param featureDescription Descriptive text to use in describeTo
+ * @param featureName Identifying text for mismatch message
+ */
+ public FeatureMatcher(Matcher<? super U> subMatcher, String featureDescription, String featureName) {
+ super(TYPE_FINDER);
+ this.subMatcher = subMatcher;
+ this.featureDescription = featureDescription;
+ this.featureName = featureName;
+ }
+
+ /**
+ * Implement this to extract the interesting feature.
+ * @param actual the target object
+ * @return the feature to be matched
+ */
+ protected abstract U featureValueOf(T actual);
+
+ @Override
+ protected boolean matchesSafely(T actual, Description mismatch) {
+ final U featureValue = featureValueOf(actual);
+ if (!subMatcher.matches(featureValue)) {
+ mismatch.appendText(featureName).appendText(" ");
+ subMatcher.describeMismatch(featureValue, mismatch);
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public final void describeTo(Description description) {
+ description.appendText(featureDescription).appendText(" ")
+ .appendDescriptionOf(subMatcher);
+ }
+}