View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.util;
15  
16  import java.util.Arrays;
17  import java.util.ArrayList;
18  import java.util.Collection;
19  import java.util.List;
20  import java.util.regex.Pattern;
21  
22  /**
23   * Static utility methods for working with collections of strings.
24   *
25   * @author Carl Harris
26   */
27  public class StringCollectionUtil {
28  
29      /**
30       * Retains all values in the subject collection that are matched by
31       * at least one of a collection of regular expressions.
32       * <p>
33       * This method is a convenience overload for
34       * {@link #retainMatching(Collection, Collection)}.
35       *   
36       * @param values subject value collection 
37       * @param patterns patterns to match
38       */
39      public static void retainMatching(Collection<String> values, String... patterns) {
40          retainMatching(values, Arrays.asList(patterns));
41      }
42  
43      /**
44       * Retains all values in the subject collection that are matched by
45       * at least one of a collection of regular expressions.
46       * <p>
47       * The semantics of this method are conceptually similar to
48       * {@link Collection#retainAll(Collection)}, but uses pattern matching
49       * instead of exact matching.
50       * 
51       * @param values subject value collection 
52       * @param patterns patterns to match
53       */
54      public static void retainMatching(Collection<String> values, Collection<String> patterns) {
55          if (patterns.isEmpty())
56              return;
57          List<String> matches = new ArrayList<String>(values.size());
58          for (String p : patterns) {
59              Pattern pattern = Pattern.compile(p);
60              for (String value : values) {
61                  if (pattern.matcher(value).matches()) {
62                      matches.add(value);
63                  }
64              }
65          }
66          values.retainAll(matches);
67      }
68  
69      /**
70       * Removes all values in the subject collection that are matched by
71       * at least one of a collection of regular expressions.
72       * <p>
73       * This method is a convenience overload for
74       * {@link #removeMatching(Collection, Collection)}.
75       * 
76       * @param values subject value collection 
77       * @param patterns patterns to match
78       */
79      public static void removeMatching(Collection<String> values, String... patterns) {
80          removeMatching(values, Arrays.asList(patterns));
81      }
82  
83      /**
84       * Removes all values in the subject collection that are matched by
85       * at least one of a collection of regular expressions.
86       * <p>
87       * The semantics of this method are conceptually similar to
88       * {@link Collection#removeAll(Collection)}, but uses pattern matching
89       * instead of exact matching.
90       * 
91       * @param values subject value collection 
92       * @param patterns patterns to match
93       */
94      public static void removeMatching(Collection<String> values, Collection<String> patterns) {
95          List<String> matches = new ArrayList<String>(values.size());
96          for (String p : patterns) {
97              Pattern pattern = Pattern.compile(p);
98              for (String value : values) {
99                  if (pattern.matcher(value).matches()) {
100                     matches.add(value);
101                 }
102             }
103         }
104         values.removeAll(matches);
105     }
106 
107 }