Merge dev/fluorine work across to master
[unimgr.git] / impl / src / test / java / org / opendaylight / unimgr / utils / PredicateMatcher.java
1 /*
2  * Copyright (c) 2018 Amartus and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.unimgr.utils;
9
10 import java.util.function.Predicate;
11
12 import org.hamcrest.BaseMatcher;
13 import org.hamcrest.Description;
14
15
16 public class PredicateMatcher<T> extends BaseMatcher<T> {
17
18     private final Predicate<T> predicate;
19
20     public PredicateMatcher(Predicate<T> predicate) {
21         this.predicate = predicate;
22     }
23
24     @SuppressWarnings("unchecked")
25     @Override
26     public boolean matches(Object item) {
27         return predicate.test((T) item);
28     }
29
30     @Override
31     public void describeTo(Description description) {
32         description.appendText("Predicate not fulfilled");
33     }
34
35     public static <U> PredicateMatcher<U> fromPredicate(Predicate<U> predicate) {
36         return new PredicateMatcher<>(predicate);
37     }
38 }