1 /* 2 * Copyright 2014 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 package android.hardware.camera2.cts.helpers; 17 18 import org.hamcrest.BaseMatcher; 19 import org.hamcrest.Description; 20 import org.hamcrest.Factory; 21 import org.hamcrest.Matcher; 22 23 import java.util.Arrays; 24 import java.util.Collection; 25 import java.util.Objects; 26 27 /** 28 * A {@link Matcher} class for checking if value contained in a {@link Collection} or array. 29 */ 30 public class InMatcher<T> extends BaseMatcher<T> { 31 32 protected Collection<T> mValues; 33 34 public InMatcher(Collection<T> values) { 35 Preconditions.checkNotNull("values", values); 36 mValues = values; 37 } 38 39 public InMatcher(T... values) { 40 Preconditions.checkNotNull(values); 41 mValues = Arrays.asList(values); 42 } 43 44 @SuppressWarnings("unchecked") 45 @Override 46 public boolean matches(Object o) { 47 T obj = (T) o; 48 for (T elem : mValues) { 49 if (Objects.equals(o, elem)) { 50 return true; 51 } 52 } 53 return false; 54 } 55 56 @Override 57 public void describeTo(Description description) { 58 description.appendText("in(").appendValue(mValues).appendText(")"); 59 } 60 61 @Factory 62 public static <T> Matcher<T> in(T... operand) { 63 return new InMatcher<T>(operand); 64 } 65 66 @Factory 67 public static <T> Matcher<T> in(Collection<T> operand) { 68 return new InMatcher<T>(operand); 69 } 70 } 71