我没有找到Predicate我期望的身份相同,com.google.common.base.Predicates所以我鞭打了这个.我发现在单元测试中关于集合的精确行为的断言是有用的(例如,Multiset<T>)这是否已经存在?如果没有,我认为应该但是可能有一些我不考虑的事情?
/** @see Predicates#is(Object) */
private static class IsPredicate<T> implements Predicate<T>, Serializable {
private final T target;
private IsPredicate(T target) {
this.target = target;
}
public boolean apply(T t) {
return target == t;
}
@Override public int hashCode() {
return target.hashCode();
}
@Override public boolean equals(Object obj) {
if (obj instanceof IsPredicate) {
IsPredicate<?> that = (IsPredicate<?>) obj;
return target.equals(that.target);
}
return false;
}
@Override public String toString() {
return "Is(" + target + ")";
}
private static final long serialVersionUID = 0;
}
/**
* Returns a predicate that evaluates to {@code true} if the object being
* tested {@code ==} the given target or both are null.
*/
public static <T> Predicate<T> is(T target) {
return (target == null)
? Predicates.<T>isNull()
: new IsPredicate<T>(target);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
936 次 |
| 最近记录: |