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

Class Class, % Method, % Line, %
RegularImmutableSortedMultiset 100% (1/1) 53.3% (8/15) 56.4% (22/39)


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 import static com.google.common.base.Preconditions.checkPositionIndexes; 19 import static com.google.common.collect.BoundType.CLOSED; 20  21 import com.google.common.annotations.GwtIncompatible; 22 import com.google.common.annotations.VisibleForTesting; 23 import com.google.common.primitives.Ints; 24 import java.util.Comparator; 25 import java.util.function.ObjIntConsumer; 26 import javax.annotation.CheckForNull; 27  28 /** 29  * An immutable sorted multiset with one or more distinct elements. 30  * 31  * @author Louis Wasserman 32  */ 33 @SuppressWarnings("serial") // uses writeReplace, not default serialization 34 @GwtIncompatible 35 @ElementTypesAreNonnullByDefault 36 final class RegularImmutableSortedMultiset<E> extends ImmutableSortedMultiset<E> { 37  private static final long[] ZERO_CUMULATIVE_COUNTS = {0}; 38  39  static final ImmutableSortedMultiset<Comparable> NATURAL_EMPTY_MULTISET = 40  new RegularImmutableSortedMultiset<>(Ordering.natural()); 41  42  @VisibleForTesting final transient RegularImmutableSortedSet<E> elementSet; 43  private final transient long[] cumulativeCounts; 44  private final transient int offset; 45  private final transient int length; 46  47  RegularImmutableSortedMultiset(Comparator<? super E> comparator) { 48  this.elementSet = ImmutableSortedSet.emptySet(comparator); 49  this.cumulativeCounts = ZERO_CUMULATIVE_COUNTS; 50  this.offset = 0; 51  this.length = 0; 52  } 53  54  RegularImmutableSortedMultiset( 55  RegularImmutableSortedSet<E> elementSet, long[] cumulativeCounts, int offset, int length) { 56  this.elementSet = elementSet; 57  this.cumulativeCounts = cumulativeCounts; 58  this.offset = offset; 59  this.length = length; 60  } 61  62  private int getCount(int index) { 63  return (int) (cumulativeCounts[offset + index + 1] - cumulativeCounts[offset + index]); 64  } 65  66  @Override 67  Entry<E> getEntry(int index) { 68  return Multisets.immutableEntry(elementSet.asList().get(index), getCount(index)); 69  } 70  71  @Override 72  public void forEachEntry(ObjIntConsumer<? super E> action) { 73  checkNotNull(action); 74  for (int i = 0; i < length; i++) { 75  action.accept(elementSet.asList().get(i), getCount(i)); 76  } 77  } 78  79  @Override 80  @CheckForNull 81  public Entry<E> firstEntry() { 82  return isEmpty() ? null : getEntry(0); 83  } 84  85  @Override 86  @CheckForNull 87  public Entry<E> lastEntry() { 88  return isEmpty() ? null : getEntry(length - 1); 89  } 90  91  @Override 92  public int count(@CheckForNull Object element) { 93  int index = elementSet.indexOf(element); 94  return (index >= 0) ? getCount(index) : 0; 95  } 96  97  @Override 98  public int size() { 99  long size = cumulativeCounts[offset + length] - cumulativeCounts[offset]; 100  return Ints.saturatedCast(size); 101  } 102  103  @Override 104  public ImmutableSortedSet<E> elementSet() { 105  return elementSet; 106  } 107  108  @Override 109  public ImmutableSortedMultiset<E> headMultiset(E upperBound, BoundType boundType) { 110  return getSubMultiset(0, elementSet.headIndex(upperBound, checkNotNull(boundType) == CLOSED)); 111  } 112  113  @Override 114  public ImmutableSortedMultiset<E> tailMultiset(E lowerBound, BoundType boundType) { 115  return getSubMultiset( 116  elementSet.tailIndex(lowerBound, checkNotNull(boundType) == CLOSED), length); 117  } 118  119  ImmutableSortedMultiset<E> getSubMultiset(int from, int to) { 120  checkPositionIndexes(from, to, length); 121  if (from == to) { 122  return emptyMultiset(comparator()); 123  } else if (from == 0 && to == length) { 124  return this; 125  } else { 126  RegularImmutableSortedSet<E> subElementSet = elementSet.getSubSet(from, to); 127  return new RegularImmutableSortedMultiset<E>( 128  subElementSet, cumulativeCounts, offset + from, to - from); 129  } 130  } 131  132  @Override 133  boolean isPartialView() { 134  return offset > 0 || length < cumulativeCounts.length - 1; 135  } 136 }