com.google.common.base ** Predicates**
包含实用的静态方法用和Predicates实例有关。
只要给定的参数是可序列化的,所有的方法返回可序列化的断言(predicates)。
/** Returns a predicate that always evaluates to {@code true}. */
@GwtCompatible(serializable = true)
public static <T> Predicate<T> alwaysTrue()
返回为真的predicate
@GwtCompatible(serializable = true)
public static <T> Predicate<T> alwaysFalse()
返回为假的predicate
@GwtCompatible(serializable = true)
public static <T> Predicate<T> isNull()
返回为空的predicate
@GwtCompatible(serializable = true)
public static <T> Predicate<T> notNull(
返回非空的predicate
public static <T> Predicate<T> not(Predicate<T> predicate)
如果参数为真,返回为假的predicate。
public static <T> Predicate<T> and(Iterable<? extends Predicate<? super T>> components)
@SafeVarargs
public static <T> Predicate<T> and(Predicate<? super T>... components)
且
public static <T> Predicate<T> or(Iterable<? extends Predicate<? super T>> components)
@SafeVarargs
public static <T> Predicate<T> or(Predicate<? super T>... components)
或
public static <T> Predicate<T> equalTo(@NullableDecl T target)
判断相等。如果测试对象为null,返回false。
@GwtIncompatible // Class.isInstance
public static Predicate<Object> instanceOf(Class<?> clazz)
判断是否是实例。
@since 20.0 (since 10.0 under the incorrect name {@code assignableFrom})
@GwtIncompatible // Class.isAssignableFrom
@Beta
public static Predicate<Class<?>> subtypeOf(Class<?> clazz)
public static <T> Predicate<T> in(Collection<? extends T> target)
检查是否包含。
public static <A, B> Predicate<A> compose(
Predicate<B> predicate, Function<A, ? extends B> function)
复合计算
@GwtIncompatible // Only used by other GWT-incompatible code.
public static Predicate<CharSequence> containsPattern(String pattern)
正则校验
@GwtIncompatible(value = "java.util.regex.Pattern")
public static Predicate<CharSequence> contains(Pattern pattern)
用正则检查是否包含。
public class User {
private int userId;
private String username;
private int age;
// getter and setter ...
// toString()
}
public class PredicateTest {
public static void main(String[] args) {
Joiner joiner = Joiner.on("\n");
List<User> users = new ArrayList<User>();
users.add(new User(1, "user1", 11));
users.add(new User(2, "user2", 21));
users.add(new User(3, "user2", 41));
users.add(new User(4, "user3", 31));
// 保留小于30岁的用户
Predicate<User> predicateAge = input -> (input.getAge() < 30);
//显示过滤结果
System.out.println(
joiner.join(Lists.newArrayList(Iterables.filter(users, predicateAge))
)
);
// {id: 1; username: user1; age: 11;}
// {id: 2; username: user2; age: 21;}
// 保留名字为user2的用户
Predicate<User> predicateName = input -> "user2".equals(input.getUsername());
//显示过滤结果
System.out.println(
joiner.join(Lists.newArrayList(Iterables.filter(users, predicateName))
)
);
// {id: 2; username: user2; age: 21;}
// {id: 3; username: user2; age: 41;}
// 保留名字为user2且年龄小于30的
Predicate<User> predicateAnd = Predicates.and(predicateAge, predicateName);
//显示过滤结果
System.out.println(
joiner.join(Lists.newArrayList(Iterables.filter(users, predicateAnd))
)
);
// {id: 2; username: user2; age: 21;}
// 保留名字为user2或年龄小于30的
Predicate<User> predicateOr = Predicates.or(predicateAge, predicateName);
//显示过滤结果
System.out.println(
joiner.join(Lists.newArrayList(Iterables.filter(users, predicateOr))
)
);
// {id: 1; username: user1; age: 11;}
// {id: 2; username: user2; age: 21;}
// {id: 3; username: user2; age: 41;}
// 保留名字不是user2的
Predicate<User> predicateNot = Predicates.not(predicateName);
//显示过滤结果
System.out.println(
joiner.join(Lists.newArrayList(Iterables.filter(users, predicateNot))
)
);
// {id: 1; username: user1; age: 11;}
// {id: 4; username: user3; age: 31;}
}
}
其中的lambda表达式
Predicate<User> predicateAge = input -> (input.getAge() < 30);
原写法是:
Predicate<User> predicateAge2 = new Predicate<User>(){
@Override
public boolean apply(@NullableDecl User input) {
return (input.getAge() < 30);
}
};