View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   */
19  package org.apache.rat.analysis.matchers;
20  
21  import java.util.Objects;
22  
23  import org.apache.rat.analysis.IHeaderMatcher;
24  /**
25   * An IHeaderMatcher that reverses the result of an enclosed matcher.
26   */
27  public class NotMatcher extends AbstractHeaderMatcher {
28  
29      private final IHeaderMatcher enclosed;
30  
31      /**
32       * Create the matcher with the enclosed matcher.
33       * @param enclosed the enclosed matcher
34       */
35      public NotMatcher(IHeaderMatcher enclosed) {
36          this(null, enclosed);
37      }
38  
39      /**
40       * Create the matcher with the enclosed matcher and id.
41       * @param id the id for this matcher.
42       * @param enclosed the enclosed matcher
43       */
44      public NotMatcher(String id, IHeaderMatcher enclosed) {
45          super(id);
46          Objects.requireNonNull(enclosed, "enclosed matcher may not be null");
47          this.enclosed = enclosed;
48      }
49  
50      @Override
51      public State matches(String line) {
52          enclosed.matches(line);
53          return currentState();
54      }
55  
56      @Override
57      public void reset() {
58          enclosed.reset();
59      }
60  
61      @Override
62      public State finalizeState() {
63          enclosed.finalizeState();
64          return currentState();
65      }
66  
67      @Override
68      public State currentState() {
69          switch (enclosed.currentState()) {
70          case t:
71              return State.f;
72          case f:
73              return State.t;
74          default:
75          case i:
76              return State.i;
77          }
78      }
79  }