Home | History | Annotate | Download | only in helpers
      1 /*
      2  * Copyright 2016 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 com.android.mediaframeworktest.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 /**
     31  * (non-Javadoc)
     32  * @see android.hardware.camera2.cts.helpers.InMatcher
     33  */
     34 public class InMatcher<T> extends BaseMatcher<T> {
     35 
     36     protected Collection<T> mValues;
     37 
     38     public InMatcher(Collection<T> values) {
     39         Preconditions.checkNotNull("values", values);
     40         mValues = values;
     41     }
     42 
     43     public InMatcher(T... values) {
     44         Preconditions.checkNotNull(values);
     45         mValues = Arrays.asList(values);
     46     }
     47 
     48     @SuppressWarnings("unchecked")
     49     @Override
     50     public boolean matches(Object o) {
     51         T obj = (T) o;
     52         for (T elem : mValues) {
     53             if (Objects.equals(o, elem)) {
     54                 return true;
     55             }
     56         }
     57         return false;
     58     }
     59 
     60     @Override
     61     public void describeTo(Description description) {
     62         description.appendText("in(").appendValue(mValues).appendText(")");
     63     }
     64 
     65     @Factory
     66     public static <T> Matcher<T> in(T... operand) {
     67         return new InMatcher<T>(operand);
     68     }
     69 
     70     @Factory
     71     public static <T> Matcher<T> in(Collection<T> operand) {
     72         return new InMatcher<T>(operand);
     73     }
     74 }
     75