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