1 /*
2  * Copyright 2014 - 2020 Rafael Winterhalter
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 net.bytebuddy.matcher;
17
18 import net.bytebuddy.build.HashCodeAndEqualsPlugin;
19
20 /**
21  * An element matcher that reverses the matching result of another matcher.
22  *
23  * @param <T> The type of the matched entity.
24  */

25 @HashCodeAndEqualsPlugin.Enhance
26 public class NegatingMatcher<T> extends ElementMatcher.Junction.AbstractBase<T> {
27
28     /**
29      * The element matcher to be negated.
30      */

31     private final ElementMatcher<? super T> matcher;
32
33     /**
34      * Creates a new negating element matcher.
35      *
36      * @param matcher The element matcher to be negated.
37      */

38     public NegatingMatcher(ElementMatcher<? super T> matcher) {
39         this.matcher = matcher;
40     }
41
42     /**
43      * {@inheritDoc}
44      */

45     public boolean matches(T target) {
46         return !matcher.matches(target);
47     }
48
49     @Override
50     public String toString() {
51         return "not(" + matcher + ')';
52     }
53 }
54