Coverage Summary for Class: CompoundOrdering (com.google.common.collect)

Class Class, % Method, % Line, %
CompoundOrdering 100% (1/1) 33.3% (2/6) 42.1% (8/19)


1 /* 2  * Copyright (C) 2007 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.collect; 18  19 import com.google.common.annotations.GwtCompatible; 20 import java.io.Serializable; 21 import java.util.Arrays; 22 import java.util.Comparator; 23  24 /** An ordering that tries several comparators in order. */ 25 @GwtCompatible(serializable = true) 26 final class CompoundOrdering<T> extends Ordering<T> implements Serializable { 27  final Comparator<? super T>[] comparators; 28  29  CompoundOrdering(Comparator<? super T> primary, Comparator<? super T> secondary) { 30  this.comparators = (Comparator<? super T>[]) new Comparator[] {primary, secondary}; 31  } 32  33  CompoundOrdering(Iterable<? extends Comparator<? super T>> comparators) { 34  this.comparators = Iterables.toArray(comparators, new Comparator[0]); 35  } 36  37  @Override 38  public int compare(T left, T right) { 39  for (int i = 0; i < comparators.length; i++) { 40  int result = comparators[i].compare(left, right); 41  if (result != 0) { 42  return result; 43  } 44  } 45  return 0; 46  } 47  48  @Override 49  public boolean equals(Object object) { 50  if (object == this) { 51  return true; 52  } 53  if (object instanceof CompoundOrdering) { 54  CompoundOrdering<?> that = (CompoundOrdering<?>) object; 55  return Arrays.equals(this.comparators, that.comparators); 56  } 57  return false; 58  } 59  60  @Override 61  public int hashCode() { 62  return Arrays.hashCode(comparators); 63  } 64  65  @Override 66  public String toString() { 67  return "Ordering.compound(" + Arrays.toString(comparators) + ")"; 68  } 69  70  private static final long serialVersionUID = 0; 71 }