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

Class Class, % Method, % Line, %
ExplicitOrdering 0% (0/1) 0% (0/7) 0% (0/16)


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.List; 22 import org.checkerframework.checker.nullness.qual.Nullable; 23  24 /** An ordering that compares objects according to a given order. */ 25 @GwtCompatible(serializable = true) 26 final class ExplicitOrdering<T> extends Ordering<T> implements Serializable { 27  final ImmutableMap<T, Integer> rankMap; 28  29  ExplicitOrdering(List<T> valuesInOrder) { 30  this(Maps.indexMap(valuesInOrder)); 31  } 32  33  ExplicitOrdering(ImmutableMap<T, Integer> rankMap) { 34  this.rankMap = rankMap; 35  } 36  37  @Override 38  public int compare(T left, T right) { 39  return rank(left) - rank(right); // safe because both are nonnegative 40  } 41  42  private int rank(T value) { 43  Integer rank = rankMap.get(value); 44  if (rank == null) { 45  throw new IncomparableValueException(value); 46  } 47  return rank; 48  } 49  50  @Override 51  public boolean equals(@Nullable Object object) { 52  if (object instanceof ExplicitOrdering) { 53  ExplicitOrdering<?> that = (ExplicitOrdering<?>) object; 54  return this.rankMap.equals(that.rankMap); 55  } 56  return false; 57  } 58  59  @Override 60  public int hashCode() { 61  return rankMap.hashCode(); 62  } 63  64  @Override 65  public String toString() { 66  return "Ordering.explicit(" + rankMap.keySet() + ")"; 67  } 68  69  private static final long serialVersionUID = 0; 70 }