com.github.methylene.args.predicate.Predicates Maven / Gradle / Ivy
The newest version!
package com.github.methylene.args.predicate;
import com.github.methylene.args.Predicate;
import java.util.*;
import java.util.regex.Pattern;
public class Predicates {
public static Predicate is(final T test) {
return new Predicate() {
@Override
public boolean matches(T arg) {
return Objects.equals(test, arg);
}
};
}
public static Predicate in(T... test) {
return in(new HashSet(Arrays.asList(test)));
}
public static Predicate in(final Set test) {
return new Predicate() {
@Override
public boolean matches(T arg) {
return test.contains(arg);
}
};
}
public static Predicate or(Predicate... predicates) {
return or(Arrays.asList(predicates));
}
public static Predicate or(Predicate p0, Predicate p1) {
ArrayList> predicates = new ArrayList>(2);
predicates.add(p0);
predicates.add(p1);
return or(predicates);
}
public static Predicate or(Predicate p0, Predicate p1, Predicate p2) {
ArrayList> predicates = new ArrayList>(2);
predicates.add(p0);
predicates.add(p1);
predicates.add(p2);
return or(predicates);
}
public static Predicate or(Predicate p0, Predicate p1, Predicate p2, Predicate p3) {
ArrayList> predicates = new ArrayList>(2);
predicates.add(p0);
predicates.add(p1);
predicates.add(p2);
predicates.add(p3);
return or(predicates);
}
public static Predicate and(Predicate p0, Predicate p1) {
ArrayList> predicates = new ArrayList>(2);
predicates.add(p0);
predicates.add(p1);
return and(predicates);
}
public static Predicate and(Predicate p0, Predicate p1, Predicate p2) {
ArrayList> predicates = new ArrayList>(2);
predicates.add(p0);
predicates.add(p1);
predicates.add(p2);
return and(predicates);
}
public static Predicate and(Predicate p0, Predicate p1, Predicate p2, Predicate p3) {
ArrayList> predicates = new ArrayList>(2);
predicates.add(p0);
predicates.add(p1);
predicates.add(p2);
predicates.add(p3);
return and(predicates);
}
public static Predicate and(Predicate... predicates) {
return and(Arrays.asList(predicates));
}
public static Predicate and(final Collection> predicates) {
if (predicates.size() == 1)
return predicates.iterator().next();
return new Predicate() {
@Override
public boolean matches(T arg) {
for (Predicate p : predicates)
if (!p.matches(arg))
return false;
return true;
}
};
}
public static Predicate or(final Collection> predicates) {
if (predicates.size() == 1)
return predicates.iterator().next();
return new Predicate() {
@Override
public boolean matches(T arg) {
for (Predicate p : predicates)
if (p.matches(arg))
return true;
return false;
}
};
}
public static Predicate nothing() {
return new Predicate() {
@Override
public boolean matches(T arg) {
return false;
}
};
}
public static Predicate anything() {
return new Predicate() {
@Override
public boolean matches(T arg) {
return true;
}
};
}
public static Predicate notNull() {
return new Predicate() {
@Override
public boolean matches(T arg) {
return arg != null;
}
};
}
}