一、Equivalence
一种判定两个实例是否相等的策略
全路径:
com.google.common.base
声明:
@GwtCompatible
public abstract class Equivalence<T>
核心方法
equivalent()
public final boolean equivalent(@Nullable T a, @Nullable T b) { if (a == b) { return true; } if (a == null || b == null) { return false; } return doEquivalent(a, b); }
Equivalence<F> onResultOf(Function<F, ? extends T> function) 先计算funcion,在比较
boolean result = Equivalence.equals().onResultOf(new Function<Object, Object>() { @Override public Object apply(Object input) { return null; } }).equivalent(1, 2);
Predicate<T> equivalentTo(@Nullable T target) 转成predicate
Map<String,Integer> result=Maps.filterKeys(ImmutableMap.of("a",1,"b",2),Equivalence.equals().equivalentTo("a"));
静态内部类 Equals(如其名,判断内容是否相等)
声明:
static final class Equals extends Equivalence<Object> implements Serializable
重写 doEquivalent
@Override protected boolean doEquivalent(Object a, Object b) { return a.equals(b); }
获得Equals实例
Equivalence.equals()
举个例子
String s="abc"; String s1=new String("abc"); System.out.println(Equivalence.equals().equivalent(s,s1));//true
静态内部类 Identity (如其名,判断内存地址是否唯一)
声明:
static final class Identity extends Equivalence<Object> implements Serializable
重写 doEquivalent
@Override protected boolean doEquivalent(Object a, Object b) { return false; }
获得Equals实例
Equivalence.identity()
举个例子
String s="abc"; String s1=new String("abc"); System.out.println(Equivalence.identity().equivalent(s,s1)); //false
子类 FunctionalEquivalence
声明:
final class FunctionalEquivalence<F, T> extends Equivalence<F> implements Serializabl
构造函数:
FunctionalEquivalence(Function<F, ? extends T> function, Equivalence<T> resultEquivalence) { this.function = checkNotNull(function); this.resultEquivalence = checkNotNull(resultEquivalence); }
重写 doEquivalent
@Override protected boolean doEquivalent(F a, F b) { return resultEquivalence.equivalent(function.apply(a), function.apply(b)); }
举个例子
看起来是经过function处理后,判断相等关系。default修饰符,暂时没看懂怎么使用 - -
子类 PairwiseEquivalence
声明:
final class PairwiseEquivalence<T> extends Equivalence<Iterable<T>> implements Serializable
构造函数
PairwiseEquivalence(Equivalence<? super T> elementEquivalence) { this.elementEquivalence = Preconditions.checkNotNull(elementEquivalence); }
重写 doEquivalent
@Override protected boolean doEquivalent(Iterable<T> iterableA, Iterable<T> iterableB) { Iterator<T> iteratorA = iterableA.iterator(); Iterator<T> iteratorB = iterableB.iterator(); while (iteratorA.hasNext() && iteratorB.hasNext()) { if (!elementEquivalence.equivalent(iteratorA.next(), iteratorB.next())) { return false; } } return !iteratorA.hasNext() && !iteratorB.hasNext(); }
举个例子
看起来是传入一种相等策略,判断两个集合中的元素是否完全相等,default修饰,暂时没看懂怎么使用
二、Supplier
惰性求值。我们可以把耗资源运算放到get方法里,在程序里,我们传递的是Supplier对象,直到调用get方法时,运算才会执行。
全路径:
com.google.common.base
定义
public interface Supplier<T> { @CanIgnoreReturnValue T get(); }
举个例子
Supplier<Integer> supplier = new Supplier<Integer>() { @Override public Integer get() { return 2; } }; int result=supplier.get();
Suppliers
工具类,提供一些静态方法,生成一些特殊的Supplier
Supplier<T> memoize(Supplier<T> delegate) 返回一个只计算一次的supplier
Supplier supplier1= Suppliers.memoize(new Supplier<Object>() { @Override public Object get() { return null; } });
Supplier<T> memoizeWithExpiration(Supplier<T> delegate, long duration, TimeUnit unit) 返回一个超时的supplier,超时后会重新计算
Supplier supplier1= Suppliers.memoizeWithExpiration((new Supplier<Object>() { @Override public Object get() { return null; } }),2,TimeUnit.HOURS);
Supplier<T> ofInstance(@Nullable T instance) 常量
Supplier supplier= Suppliers.ofInstance(2);
Supplier<T> compose(Function<? super F, T> function, Supplier<F> supplier) 先计算supplier,再计算funciton
Supplier supplier= Suppliers.compose(new Function<Object, Object>() { @Override public Object apply(Object input) { System.out.println("aaaaaa"); return null; } }, new Supplier<Object>() { @Override public Object get() { System.out.println("bbbbbbb"); return null; } });
Supplier<T> synchronizedSupplier(Supplier<T> delegate) 线程安全
Supplier supplier= Suppliers.synchronizedSupplier(new Supplier() { @Override public Object get() { return null; } });