1 package org.kuali.common.jute.base;
2
3 import static com.google.common.base.Predicates.alwaysFalse;
4 import static com.google.common.base.Predicates.alwaysTrue;
5 import static com.google.common.base.Predicates.and;
6 import static com.google.common.base.Predicates.containsPattern;
7 import static com.google.common.base.Predicates.not;
8 import static com.google.common.base.Predicates.or;
9 import static com.google.common.collect.Iterables.isEmpty;
10 import static com.google.common.collect.Lists.newArrayList;
11 import static org.kuali.common.jute.base.Precondition.checkNotNull;
12
13 import java.util.List;
14
15 import com.google.common.base.Predicate;
16
17 public final class Predicates {
18
19 private Predicates() {}
20
21 private static final Predicate<CharSequence> ALWAYS_TRUE = alwaysTrue();
22 private static final Predicate<CharSequence> ALWAYS_FALSE = alwaysFalse();
23
24
25
26
27
28
29
30
31 public static Predicate<CharSequence> includesExcludes(Iterable<String> includes, Iterable<String> excludes) {
32 checkNotNull(includes, "includes");
33 checkNotNull(excludes, "excludes");
34 Predicate<CharSequence> incl = isEmpty(includes) ? ALWAYS_TRUE : containsAny(includes);
35 Predicate<CharSequence> excl = isEmpty(excludes) ? ALWAYS_FALSE : containsAny(excludes);
36 return and(incl, not(excl));
37 }
38
39
40
41
42 public static Predicate<CharSequence> containsAny(Iterable<String> patterns) {
43 checkNotNull(patterns, "patterns");
44 List<Predicate<CharSequence>> predicates = newArrayList();
45 for (String pattern : patterns) {
46 Predicate<CharSequence> predicate = containsPattern(pattern);
47 predicates.add(predicate);
48 }
49 return or(predicates);
50 }
51 }