com.github.methylene.args.predicate.ListPredicates Maven / Gradle / Ivy
The newest version!
package com.github.methylene.args.predicate;
import com.github.methylene.args.Predicate;
import com.github.methylene.args.Token;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
public class ListPredicates {
public static Predicate> matches(final Predicate predicate) {
return new Predicate>() {
@Override
public boolean matches(List tokens) {
if (tokens == null)
tokens = Collections.emptyList();
for (Token token: tokens)
if (!predicate.matches(token))
return false;
return true;
}
};
}
public static Predicate> matches(final Pattern pattern) {
return matches(TokenPredicates.matches(StringPredicates.matches(pattern)));
}
public static Predicate> hasCardinality(final Predicate cardinality) {
return new Predicate>() {
@Override
public boolean matches(List arg) {
if (arg == null)
arg = Collections.emptyList();
return cardinality.matches(arg.size());
}
};
}
public static Predicate> isSingleValue() {
return Predicates.and(isValue(), hasCardinality(IntegerPredicates.exactly(1)));
}
public static Predicate> isSingleFlag() {
return Predicates.and(isFlag(), hasCardinality(IntegerPredicates.exactly(1)));
}
public static Predicate> isPresent() {
return hasCardinality(IntegerPredicates.geq(1));
}
public static Predicate> isValue() {
return matches(TokenPredicates.isValue());
}
public static Predicate> isFlag() {
return matches(TokenPredicates.isFlag());
}
}