Coverage Summary for Class: JdkBackedImmutableMultiset (com.google.common.collect)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| JdkBackedImmutableMultiset | 0% (0/1) | 0% (0/7) | 0% (0/23) |
1 /* 2 * Copyright (C) 2018 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 License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * 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 com.google.common.primitives.Ints; 21 import java.util.Collection; 22 import java.util.Map; 23 import javax.annotation.CheckForNull; 24 25 /** 26 * An implementation of ImmutableMultiset backed by a JDK Map and a list of entries. Used to protect 27 * against hash flooding attacks. 28 * 29 * @author Louis Wasserman 30 */ 31 @GwtCompatible 32 @ElementTypesAreNonnullByDefault 33 final class JdkBackedImmutableMultiset<E> extends ImmutableMultiset<E> { 34 private final Map<E, Integer> delegateMap; 35 private final ImmutableList<Entry<E>> entries; 36 private final long size; 37 38 static <E> ImmutableMultiset<E> create(Collection<? extends Entry<? extends E>> entries) { 39 @SuppressWarnings("unchecked") 40 Entry<E>[] entriesArray = entries.toArray(new Entry[0]); 41 Map<E, Integer> delegateMap = Maps.newHashMapWithExpectedSize(entriesArray.length); 42 long size = 0; 43 for (int i = 0; i < entriesArray.length; i++) { 44 Entry<E> entry = entriesArray[i]; 45 int count = entry.getCount(); 46 size += count; 47 E element = checkNotNull(entry.getElement()); 48 delegateMap.put(element, count); 49 if (!(entry instanceof Multisets.ImmutableEntry)) { 50 entriesArray[i] = Multisets.immutableEntry(element, count); 51 } 52 } 53 return new JdkBackedImmutableMultiset<>( 54 delegateMap, ImmutableList.asImmutableList(entriesArray), size); 55 } 56 57 private JdkBackedImmutableMultiset( 58 Map<E, Integer> delegateMap, ImmutableList<Entry<E>> entries, long size) { 59 this.delegateMap = delegateMap; 60 this.entries = entries; 61 this.size = size; 62 } 63 64 @Override 65 public int count(@CheckForNull Object element) { 66 return delegateMap.getOrDefault(element, 0); 67 } 68 69 @CheckForNull private transient ImmutableSet<E> elementSet; 70 71 @Override 72 public ImmutableSet<E> elementSet() { 73 ImmutableSet<E> result = elementSet; 74 return (result == null) ? elementSet = new ElementSet<E>(entries, this) : result; 75 } 76 77 @Override 78 Entry<E> getEntry(int index) { 79 return entries.get(index); 80 } 81 82 @Override 83 boolean isPartialView() { 84 return false; 85 } 86 87 @Override 88 public int size() { 89 return Ints.saturatedCast(size); 90 } 91 }