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.Collection;
22  
23  import org.apache.rat.analysis.IHeaderMatcher;
24  
25  /**
26   * A matcher that performs a logical {@code AND} across all the contained matchers.
27   */
28  public class AndMatcher extends AbstractMatcherContainer {
29  
30      /**
31       * Constructs the AndMatcher with the specified id and enclosed collection.
32       * @param id the to use.  If null or an empty string a unique random id will be created.
33       * @param enclosed the enclosed collection.
34       */
35      public AndMatcher(String id, Collection<? extends IHeaderMatcher> enclosed) {
36          super(id, enclosed);
37      }
38  
39      /**
40       * Constructs the AndMatcher with the a unique random id and the enclosed collection.
41       * @param enclosed the enclosed collection.
42       */
43      public AndMatcher(Collection<? extends IHeaderMatcher> enclosed) {
44          this(null, enclosed);
45      }
46  
47      @Override
48      public State currentState() {
49          State dflt = State.t;
50          for (IHeaderMatcher matcher : enclosed) {
51              switch (matcher.currentState()) {
52              case f:
53                  return State.f;
54              case i:
55                  dflt = State.i;
56                  break;
57              default:
58                  // do nothing
59                  break;
60              }
61          }
62          return dflt;
63      }
64  
65      @Override
66      public State matches(String line) {
67          enclosed.stream().filter(x -> x.currentState() == State.i).forEach(x -> x.matches(line));
68          return currentState();
69      }
70  
71      @Override
72      public State finalizeState() {
73          enclosed.forEach(IHeaderMatcher::finalizeState);
74          return currentState();
75      }
76  }