/* * This file is part of ComputerCraft - http://www.computercraft.info * Copyright Daniel Ratcliffe, 2011-2021. Do not distribute without permission. * Send enquiries to dratcliffe@gmail.com */ package dan200.computercraft; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeDiagnosingMatcher; import java.util.function.Function; public class ContramapMatcher extends TypeSafeDiagnosingMatcher { private final String desc; private final Function convert; private final Matcher matcher; public ContramapMatcher( String desc, Function convert, Matcher matcher ) { this.desc = desc; this.convert = convert; this.matcher = matcher; } @Override protected boolean matchesSafely( T item, Description mismatchDescription ) { U converted = convert.apply( item ); if( matcher.matches( converted ) ) return true; mismatchDescription.appendText( desc ).appendText( " " ); matcher.describeMismatch( converted, mismatchDescription ); return false; } @Override public void describeTo( Description description ) { description.appendText( desc ).appendText( " " ).appendDescriptionOf( matcher ); } public static Matcher contramap( Matcher matcher, String desc, Function convert ) { return new ContramapMatcher<>( desc, convert, matcher ); } public static Matcher contramap( Matcher matcher, Function convert ) { return new ContramapMatcher<>( "-f(_)->", convert, matcher ); } }