Coverage Summary for Class: SortedIterables (com.google.common.collect)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| SortedIterables | 100% (1/1) | 66.7% (2/3) | 61.5% (8/13) |
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 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 11 * express or implied. See the License for the specific language governing permissions and 12 * limitations under the License. 13 */ 14 15 package com.google.common.collect; 16 17 import static com.google.common.base.Preconditions.checkNotNull; 18 19 import com.google.common.annotations.GwtCompatible; 20 import java.util.Comparator; 21 import java.util.SortedSet; 22 23 /** 24 * Utilities for dealing with sorted collections of all types. 25 * 26 * @author Louis Wasserman 27 */ 28 @GwtCompatible 29 final class SortedIterables { 30 private SortedIterables() {} 31 32 /** 33 * Returns {@code true} if {@code elements} is a sorted collection using an ordering equivalent to 34 * {@code comparator}. 35 */ 36 public static boolean hasSameComparator(Comparator<?> comparator, Iterable<?> elements) { 37 checkNotNull(comparator); 38 checkNotNull(elements); 39 Comparator<?> comparator2; 40 if (elements instanceof SortedSet) { 41 comparator2 = comparator((SortedSet<?>) elements); 42 } else if (elements instanceof SortedIterable) { 43 comparator2 = ((SortedIterable<?>) elements).comparator(); 44 } else { 45 return false; 46 } 47 return comparator.equals(comparator2); 48 } 49 50 @SuppressWarnings("unchecked") 51 // if sortedSet.comparator() is null, the set must be naturally ordered 52 public static <E> Comparator<? super E> comparator(SortedSet<E> sortedSet) { 53 Comparator<? super E> result = sortedSet.comparator(); 54 if (result == null) { 55 result = (Comparator<? super E>) Ordering.natural(); 56 } 57 return result; 58 } 59 }