Coverage Summary for Class: ImmutableSortedAsList (com.google.common.collect)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| ImmutableSortedAsList | 100% (1/1) | 12.5% (1/8) | 14.3% (2/14) |
1 /* 2 * Copyright (C) 2009 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 com.google.common.annotations.GwtCompatible; 18 import com.google.common.annotations.GwtIncompatible; 19 import java.util.Comparator; 20 import java.util.Spliterator; 21 import org.checkerframework.checker.nullness.qual.Nullable; 22 23 /** 24 * List returned by {@code ImmutableSortedSet.asList()} when the set isn't empty. 25 * 26 * @author Jared Levy 27 * @author Louis Wasserman 28 */ 29 @GwtCompatible(emulated = true) 30 @SuppressWarnings("serial") 31 final class ImmutableSortedAsList<E> extends RegularImmutableAsList<E> 32 implements SortedIterable<E> { 33 ImmutableSortedAsList(ImmutableSortedSet<E> backingSet, ImmutableList<E> backingList) { 34 super(backingSet, backingList); 35 } 36 37 @Override 38 ImmutableSortedSet<E> delegateCollection() { 39 return (ImmutableSortedSet<E>) super.delegateCollection(); 40 } 41 42 @Override 43 public Comparator<? super E> comparator() { 44 return delegateCollection().comparator(); 45 } 46 47 // Override indexOf() and lastIndexOf() to be O(log N) instead of O(N). 48 49 @GwtIncompatible // ImmutableSortedSet.indexOf 50 // TODO(cpovirk): consider manual binary search under GWT to preserve O(log N) lookup 51 @Override 52 public int indexOf(@Nullable Object target) { 53 int index = delegateCollection().indexOf(target); 54 55 // TODO(kevinb): reconsider if it's really worth making feeble attempts at 56 // sanity for inconsistent comparators. 57 58 // The equals() check is needed when the comparator isn't compatible with 59 // equals(). 60 return (index >= 0 && get(index).equals(target)) ? index : -1; 61 } 62 63 @GwtIncompatible // ImmutableSortedSet.indexOf 64 @Override 65 public int lastIndexOf(@Nullable Object target) { 66 return indexOf(target); 67 } 68 69 @Override 70 public boolean contains(Object target) { 71 // Necessary for ISS's with comparators inconsistent with equals. 72 return indexOf(target) >= 0; 73 } 74 75 @GwtIncompatible // super.subListUnchecked does not exist; inherited subList is valid if slow 76 /* 77 * TODO(cpovirk): if we start to override indexOf/lastIndexOf under GWT, we'll want some way to 78 * override subList to return an ImmutableSortedAsList for better performance. Right now, I'm not 79 * sure there's any performance hit from our failure to override subListUnchecked under GWT 80 */ 81 @Override 82 ImmutableList<E> subListUnchecked(int fromIndex, int toIndex) { 83 ImmutableList<E> parentSubList = super.subListUnchecked(fromIndex, toIndex); 84 return new RegularImmutableSortedSet<E>(parentSubList, comparator()).asList(); 85 } 86 87 @Override 88 public Spliterator<E> spliterator() { 89 return CollectSpliterators.indexed( 90 size(), 91 ImmutableList.SPLITERATOR_CHARACTERISTICS | Spliterator.SORTED | Spliterator.DISTINCT, 92 delegateList()::get, 93 comparator()); 94 } 95 }