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

Class Method, % Line, %
SortedMultisets 0% (0/4) 0% (0/6)
SortedMultisets$ElementSet 0% (0/9) 0% (0/10)
SortedMultisets$NavigableElementSet 0% (0/12) 0% (0/18)
Total 0% (0/25) 0% (0/34)


1 /* 2  * Copyright (C) 2011 The Guava Authors 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5  * use this file except in compliance with the License. You may obtain a copy of 6  * 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, WITHOUT 12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13  * License for the specific language governing permissions and limitations under 14  * the License. 15  */ 16  17 package com.google.common.collect; 18  19 import static com.google.common.collect.BoundType.CLOSED; 20 import static com.google.common.collect.BoundType.OPEN; 21  22 import com.google.common.annotations.GwtCompatible; 23 import com.google.common.annotations.GwtIncompatible; 24 import com.google.common.collect.Multiset.Entry; 25 import com.google.j2objc.annotations.Weak; 26 import java.util.Comparator; 27 import java.util.Iterator; 28 import java.util.NavigableSet; 29 import java.util.NoSuchElementException; 30 import java.util.SortedSet; 31 import javax.annotation.CheckForNull; 32 import org.checkerframework.checker.nullness.qual.Nullable; 33  34 /** 35  * Provides static utility methods for creating and working with {@link SortedMultiset} instances. 36  * 37  * @author Louis Wasserman 38  */ 39 @GwtCompatible(emulated = true) 40 @ElementTypesAreNonnullByDefault 41 final class SortedMultisets { 42  private SortedMultisets() {} 43  44  /** A skeleton implementation for {@link SortedMultiset#elementSet}. */ 45  @SuppressWarnings("JdkObsolete") // TODO(b/6160855): Switch GWT emulations to NavigableSet. 46  static class ElementSet<E extends @Nullable Object> extends Multisets.ElementSet<E> 47  implements SortedSet<E> { 48  @Weak private final SortedMultiset<E> multiset; 49  50  ElementSet(SortedMultiset<E> multiset) { 51  this.multiset = multiset; 52  } 53  54  @Override 55  final SortedMultiset<E> multiset() { 56  return multiset; 57  } 58  59  @Override 60  public Iterator<E> iterator() { 61  return Multisets.elementIterator(multiset().entrySet().iterator()); 62  } 63  64  @Override 65  public Comparator<? super E> comparator() { 66  return multiset().comparator(); 67  } 68  69  @Override 70  public SortedSet<E> subSet(@ParametricNullness E fromElement, @ParametricNullness E toElement) { 71  return multiset().subMultiset(fromElement, CLOSED, toElement, OPEN).elementSet(); 72  } 73  74  @Override 75  public SortedSet<E> headSet(@ParametricNullness E toElement) { 76  return multiset().headMultiset(toElement, OPEN).elementSet(); 77  } 78  79  @Override 80  public SortedSet<E> tailSet(@ParametricNullness E fromElement) { 81  return multiset().tailMultiset(fromElement, CLOSED).elementSet(); 82  } 83  84  @Override 85  @ParametricNullness 86  public E first() { 87  return getElementOrThrow(multiset().firstEntry()); 88  } 89  90  @Override 91  @ParametricNullness 92  public E last() { 93  return getElementOrThrow(multiset().lastEntry()); 94  } 95  } 96  97  /** A skeleton navigable implementation for {@link SortedMultiset#elementSet}. */ 98  @GwtIncompatible // Navigable 99  static class NavigableElementSet<E extends @Nullable Object> extends ElementSet<E> 100  implements NavigableSet<E> { 101  NavigableElementSet(SortedMultiset<E> multiset) { 102  super(multiset); 103  } 104  105  @Override 106  @CheckForNull 107  public E lower(@ParametricNullness E e) { 108  return getElementOrNull(multiset().headMultiset(e, OPEN).lastEntry()); 109  } 110  111  @Override 112  @CheckForNull 113  public E floor(@ParametricNullness E e) { 114  return getElementOrNull(multiset().headMultiset(e, CLOSED).lastEntry()); 115  } 116  117  @Override 118  @CheckForNull 119  public E ceiling(@ParametricNullness E e) { 120  return getElementOrNull(multiset().tailMultiset(e, CLOSED).firstEntry()); 121  } 122  123  @Override 124  @CheckForNull 125  public E higher(@ParametricNullness E e) { 126  return getElementOrNull(multiset().tailMultiset(e, OPEN).firstEntry()); 127  } 128  129  @Override 130  public NavigableSet<E> descendingSet() { 131  return new NavigableElementSet<E>(multiset().descendingMultiset()); 132  } 133  134  @Override 135  public Iterator<E> descendingIterator() { 136  return descendingSet().iterator(); 137  } 138  139  @Override 140  @CheckForNull 141  public E pollFirst() { 142  return getElementOrNull(multiset().pollFirstEntry()); 143  } 144  145  @Override 146  @CheckForNull 147  public E pollLast() { 148  return getElementOrNull(multiset().pollLastEntry()); 149  } 150  151  @Override 152  public NavigableSet<E> subSet( 153  @ParametricNullness E fromElement, 154  boolean fromInclusive, 155  @ParametricNullness E toElement, 156  boolean toInclusive) { 157  return new NavigableElementSet<E>( 158  multiset() 159  .subMultiset( 160  fromElement, BoundType.forBoolean(fromInclusive), 161  toElement, BoundType.forBoolean(toInclusive))); 162  } 163  164  @Override 165  public NavigableSet<E> headSet(@ParametricNullness E toElement, boolean inclusive) { 166  return new NavigableElementSet<E>( 167  multiset().headMultiset(toElement, BoundType.forBoolean(inclusive))); 168  } 169  170  @Override 171  public NavigableSet<E> tailSet(@ParametricNullness E fromElement, boolean inclusive) { 172  return new NavigableElementSet<E>( 173  multiset().tailMultiset(fromElement, BoundType.forBoolean(inclusive))); 174  } 175  } 176  177  private static <E extends @Nullable Object> E getElementOrThrow(@CheckForNull Entry<E> entry) { 178  if (entry == null) { 179  throw new NoSuchElementException(); 180  } 181  return entry.getElement(); 182  } 183  184  @CheckForNull 185  private static <E extends @Nullable Object> E getElementOrNull(@CheckForNull Entry<E> entry) { 186  return (entry == null) ? null : entry.getElement(); 187  } 188 }