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

Class Method, % Line, %
ForwardingSortedMap 10% (1/10) 5.6% (1/18)
ForwardingSortedMap$StandardKeySet 0% (0/1) 0% (0/2)
Total 9.1% (1/11) 5% (1/20)


1 /* 2  * Copyright (C) 2007 The Guava Authors 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); 5  * you may not use this file except in compliance with the License. 6  * You may obtain a copy of 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, 12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13  * See the License for the specific language governing permissions and 14  * limitations under the License. 15  */ 16  17 package com.google.common.collect; 18  19 import static com.google.common.base.Preconditions.checkArgument; 20  21 import com.google.common.annotations.Beta; 22 import com.google.common.annotations.GwtCompatible; 23 import java.util.Comparator; 24 import java.util.NoSuchElementException; 25 import java.util.SortedMap; 26 import org.checkerframework.checker.nullness.qual.Nullable; 27  28 /** 29  * A sorted map which forwards all its method calls to another sorted map. Subclasses should 30  * override one or more methods to modify the behavior of the backing sorted map as desired per the 31  * <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. 32  * 33  * <p><b>Warning:</b> The methods of {@code ForwardingSortedMap} forward <i>indiscriminately</i> to 34  * the methods of the delegate. For example, overriding {@link #put} alone <i>will not</i> change 35  * the behavior of {@link #putAll}, which can lead to unexpected behavior. In this case, you should 36  * override {@code putAll} as well, either providing your own implementation, or delegating to the 37  * provided {@code standardPutAll} method. 38  * 39  * <p><b>{@code default} method warning:</b> This class does <i>not</i> forward calls to {@code 40  * default} methods. Instead, it inherits their default implementations. When those implementations 41  * invoke methods, they invoke methods on the {@code ForwardingSortedMap}. 42  * 43  * <p>Each of the {@code standard} methods, where appropriate, use the comparator of the map to test 44  * equality for both keys and values, unlike {@code ForwardingMap}. 45  * 46  * <p>The {@code standard} methods and the collection views they return are not guaranteed to be 47  * thread-safe, even when all of the methods that they depend on are thread-safe. 48  * 49  * @author Mike Bostock 50  * @author Louis Wasserman 51  * @since 2.0 52  */ 53 @GwtCompatible 54 public abstract class ForwardingSortedMap<K, V> extends ForwardingMap<K, V> 55  implements SortedMap<K, V> { 56  // TODO(lowasser): identify places where thread safety is actually lost 57  58  /** Constructor for use by subclasses. */ 59  protected ForwardingSortedMap() {} 60  61  @Override 62  protected abstract SortedMap<K, V> delegate(); 63  64  @Override 65  public Comparator<? super K> comparator() { 66  return delegate().comparator(); 67  } 68  69  @Override 70  public K firstKey() { 71  return delegate().firstKey(); 72  } 73  74  @Override 75  public SortedMap<K, V> headMap(K toKey) { 76  return delegate().headMap(toKey); 77  } 78  79  @Override 80  public K lastKey() { 81  return delegate().lastKey(); 82  } 83  84  @Override 85  public SortedMap<K, V> subMap(K fromKey, K toKey) { 86  return delegate().subMap(fromKey, toKey); 87  } 88  89  @Override 90  public SortedMap<K, V> tailMap(K fromKey) { 91  return delegate().tailMap(fromKey); 92  } 93  94  /** 95  * A sensible implementation of {@link SortedMap#keySet} in terms of the methods of {@code 96  * ForwardingSortedMap}. In many cases, you may wish to override {@link 97  * ForwardingSortedMap#keySet} to forward to this implementation or a subclass thereof. 98  * 99  * @since 15.0 100  */ 101  @Beta 102  protected class StandardKeySet extends Maps.SortedKeySet<K, V> { 103  /** Constructor for use by subclasses. */ 104  public StandardKeySet() { 105  super(ForwardingSortedMap.this); 106  } 107  } 108  109  // unsafe, but worst case is a CCE is thrown, which callers will be expecting 110  @SuppressWarnings("unchecked") 111  private int unsafeCompare(Object k1, Object k2) { 112  Comparator<? super K> comparator = comparator(); 113  if (comparator == null) { 114  return ((Comparable<Object>) k1).compareTo(k2); 115  } else { 116  return ((Comparator<Object>) comparator).compare(k1, k2); 117  } 118  } 119  120  /** 121  * A sensible definition of {@link #containsKey} in terms of the {@code firstKey()} method of 122  * {@link #tailMap}. If you override {@link #tailMap}, you may wish to override {@link 123  * #containsKey} to forward to this implementation. 124  * 125  * @since 7.0 126  */ 127  @Override 128  @Beta 129  protected boolean standardContainsKey(@Nullable Object key) { 130  try { 131  // any CCE will be caught 132  @SuppressWarnings("unchecked") 133  SortedMap<Object, V> self = (SortedMap<Object, V>) this; 134  Object ceilingKey = self.tailMap(key).firstKey(); 135  return unsafeCompare(ceilingKey, key) == 0; 136  } catch (ClassCastException | NoSuchElementException | NullPointerException e) { 137  return false; 138  } 139  } 140  141  /** 142  * A sensible default implementation of {@link #subMap(Object, Object)} in terms of {@link 143  * #headMap(Object)} and {@link #tailMap(Object)}. In some situations, you may wish to override 144  * {@link #subMap(Object, Object)} to forward to this implementation. 145  * 146  * @since 7.0 147  */ 148  @Beta 149  protected SortedMap<K, V> standardSubMap(K fromKey, K toKey) { 150  checkArgument(unsafeCompare(fromKey, toKey) <= 0, "fromKey must be <= toKey"); 151  return tailMap(fromKey).headMap(toKey); 152  } 153 }