1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.intentresolver; 18 19 import org.hamcrest.BaseMatcher; 20 import org.hamcrest.Description; 21 import org.hamcrest.Matcher; 22 23 /** 24 * Utils for helping with more customized matching options, for example matching the first 25 * occurrence of a set criteria. 26 */ 27 public class MatcherUtils { 28 29 /** 30 * Returns a {@link Matcher} which only matches the first occurrence of a set criteria. 31 */ first(final Matcher<T> matcher)32 static <T> Matcher<T> first(final Matcher<T> matcher) { 33 return new BaseMatcher<T>() { 34 boolean isFirstMatch = true; 35 36 @Override 37 public boolean matches(final Object item) { 38 if (isFirstMatch && matcher.matches(item)) { 39 isFirstMatch = false; 40 return true; 41 } 42 return false; 43 } 44 45 @Override 46 public void describeTo(final Description description) { 47 description.appendText("Returns the first matching item"); 48 } 49 }; 50 } 51 } 52