Coverage Summary for Class: FunctionalEquivalence (com.google.common.base)

Class Class, % Method, % Line, %
FunctionalEquivalence 0% (0/1) 0% (0/6) 0% (0/14)


1 /* 2  * Copyright (C) 2011 The Guava Authors 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5  * in compliance with the License. You may obtain a copy of the License at 6  * 7  * http://www.apache.org/licenses/LICENSE-2.0 8  * 9  * Unless required by applicable law or agreed to in writing, software distributed under the License 10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11  * or implied. See the License for the specific language governing permissions and limitations under 12  * the License. 13  */ 14  15 package com.google.common.base; 16  17 import static com.google.common.base.Preconditions.checkNotNull; 18  19 import com.google.common.annotations.Beta; 20 import com.google.common.annotations.GwtCompatible; 21 import java.io.Serializable; 22 import javax.annotation.CheckForNull; 23  24 /** 25  * Equivalence applied on functional result. 26  * 27  * @author Bob Lee 28  * @since 10.0 29  */ 30 @Beta 31 @GwtCompatible 32 @ElementTypesAreNonnullByDefault 33 final class FunctionalEquivalence<F, T> extends Equivalence<F> implements Serializable { 34  35  private static final long serialVersionUID = 0; 36  37  private final Function<? super F, ? extends T> function; 38  private final Equivalence<T> resultEquivalence; 39  40  FunctionalEquivalence( 41  Function<? super F, ? extends T> function, Equivalence<T> resultEquivalence) { 42  this.function = checkNotNull(function); 43  this.resultEquivalence = checkNotNull(resultEquivalence); 44  } 45  46  @Override 47  protected boolean doEquivalent(F a, F b) { 48  return resultEquivalence.equivalent(function.apply(a), function.apply(b)); 49  } 50  51  @Override 52  protected int doHash(F a) { 53  return resultEquivalence.hash(function.apply(a)); 54  } 55  56  @Override 57  public boolean equals(@CheckForNull Object obj) { 58  if (obj == this) { 59  return true; 60  } 61  if (obj instanceof FunctionalEquivalence) { 62  FunctionalEquivalence<?, ?> that = (FunctionalEquivalence<?, ?>) obj; 63  return function.equals(that.function) && resultEquivalence.equals(that.resultEquivalence); 64  } 65  return false; 66  } 67  68  @Override 69  public int hashCode() { 70  return Objects.hashCode(function, resultEquivalence); 71  } 72  73  @Override 74  public String toString() { 75  return resultEquivalence + ".onResultOf(" + function + ")"; 76  } 77 }