View Javadoc
1   /*
2    * #%L
3    * settings4j
4    * ===============================================================
5    * Copyright (C) 2008 - 2015 Brabenetz Harald, Austria
6    * ===============================================================
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package org.settings4j.settings;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.regex.Pattern;
25  
26  import org.settings4j.Filter;
27  
28  /**
29   * The Default Implementation uses the RegEx-{@link Pattern} Expressions to evaluate the includes and excludes.
30   *
31   * @author Harald.Brabenetz
32   */
33  public class DefaultFilter implements Filter {
34  
35      private final List<Pattern> includePatterns = new ArrayList<Pattern>();
36      private final List<Pattern> excludePatterns = new ArrayList<Pattern>();
37  
38      @Override
39      public void addExclude(final String pattern) {
40          try {
41              final Pattern p = Pattern.compile(pattern);
42              this.excludePatterns.add(p);
43          } catch (final RuntimeException e) {
44              throw new IllegalArgumentException(String.format("cannot compile exclude filter pattern '%s': %s", pattern, e.getMessage()), e);
45          }
46      }
47  
48      @Override
49      public void addInclude(final String pattern) {
50          try {
51              final Pattern p = Pattern.compile(pattern);
52              this.includePatterns.add(p);
53          } catch (final RuntimeException e) {
54              throw new IllegalArgumentException(String.format("cannot compile include filter pattern '%s': %s", pattern, e.getMessage()), e);
55          }
56      }
57  
58      @Override
59      public boolean isValid(final String key) {
60          // if exclude match, return always false.
61          for (final Pattern pattern : this.excludePatterns) {
62              if (pattern.matcher(key).matches()) {
63                  return false;
64              }
65          }
66  
67          // if no include is defined, return always true
68          if (this.includePatterns.isEmpty()) {
69              return true;
70          }
71  
72          // if include match, return true.
73          for (final Pattern pattern : this.includePatterns) {
74              if (pattern.matcher(key).matches()) {
75                  return true;
76              }
77          }
78  
79          // no includePattern matched.
80          return false;
81      }
82  
83  }