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.maven.surefire.group.match;
20  
21  import java.util.Collection;
22  
23  /**
24   * AND group matcher.
25   */
26  public class AndGroupMatcher extends JoinGroupMatcher {
27  
28      public AndGroupMatcher(GroupMatcher... matchers) {
29          for (GroupMatcher matcher : matchers) {
30              addMatcher(matcher);
31          }
32      }
33  
34      public AndGroupMatcher(Collection<GroupMatcher> matchers) {
35          for (GroupMatcher matcher : matchers) {
36              addMatcher(matcher);
37          }
38      }
39  
40      @Override
41      public boolean enabled(Class<?>... cats) {
42          for (GroupMatcher matcher : getMatchers()) {
43              boolean result = matcher.enabled(cats);
44              if (!result) {
45                  return false;
46              }
47          }
48  
49          return true;
50      }
51  
52      @Override
53      public boolean enabled(String... cats) {
54          for (GroupMatcher matcher : getMatchers()) {
55              boolean result = matcher.enabled(cats);
56              if (!result) {
57                  return false;
58              }
59          }
60  
61          return true;
62      }
63  
64      @Override
65      public String toString() {
66          StringBuilder sb = new StringBuilder();
67          for (GroupMatcher matcher : getMatchers()) {
68              if (sb.length() > 0) {
69                  sb.append(" AND ");
70              }
71              sb.append(matcher);
72          }
73  
74          return sb.toString();
75      }
76  
77      @Override
78      public int hashCode() {
79          final int prime = 31;
80          int result = 1;
81          result = prime * result;
82          for (GroupMatcher matcher : getMatchers()) {
83              result += matcher.hashCode();
84          }
85          return result;
86      }
87  
88      @Override
89      public boolean equals(Object obj) {
90          if (this == obj) {
91              return true;
92          }
93          if (obj == null) {
94              return false;
95          }
96          if (getClass() != obj.getClass()) {
97              return false;
98          }
99          AndGroupMatcher other = (AndGroupMatcher) obj;
100         return getMatchers().equals(other.getMatchers());
101     }
102 }