Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2015 Mark Michaelis
   *
   * 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.github.mmichaelis.hamcrest.nextdeed.function;
 

Matcher which first transforms the matched object and hands over the comparison to the delegate matcher.

Note:
In order not to report a different value on failure than used for comparison the previously transformed value is stored per thread when matching is tried. As a consequence describeMismatchSafely(java.lang.Object,org.hamcrest.Description) ignores the item expecting that it did not change meanwhile.

Parameters:
<F> type of the actual value in the assertion; typically the component under test
<T> type of the actual value to compare; typically the state of the component under test
Since:
0.1.0
 
 public class ApplyingMatcher<F, T> extends TypeSafeMatcher<F> {
 
   @NotNull
   private final ThreadLocal<T> lastValue = new ThreadLocal<>();
   @NotNull
   private final Function<F, T> function;
   @NotNull
   private final Matcher<? super T> delegateMatcher;

  

Constructor.

Parameters:
function the function to apply to convert the matched object to the target value
delegateMatcher matcher to apply to the transformed value
Since:
0.1.0
 
   public ApplyingMatcher(@NotNull Function<F, T> function,
                          @NotNull Matcher<? super T> delegateMatcher) {
     this. = function;
     this. = delegateMatcher;
   }

  

Applies a transformation to the value before comparing the transformed result with the given matcher.

Parameters:
function the function to apply to convert the asserted value to the target value
delegateMatcher matcher to apply to the transformed value; typically the state of the component under test
<F> type to input into assertion
<T> actual value type to compare
Returns:
matcher which transforms input before comparison
Since:
0.1.0
 
   public static <F, T> Matcher<F> applying(@NotNull Function<F, T> function,
                                            @NotNull Matcher<? super T> delegateMatcher) {
     return new ApplyingMatcher<>(functiondelegateMatcher);
   }
 
   @Override
   public void describeTo(@NotNull Description description) {
     .describeTo(description);
   }
 
   @Override
   protected boolean matchesSafely(F item) {
     .set(.apply(item));
     return .matches(.get());
   }
 
   @Override
   protected void describeMismatchSafely(F item, @NotNull Description mismatchDescription) {
     // Ignoring item, expecting that it did not change between call to describeMismatch and matches.
    .describeMismatch(.get(), mismatchDescription);
  }
New to GrepCode? Check out our FAQ X