001/* 002 * Copyright (C) 2007 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect; 018 019import static com.google.common.base.Preconditions.checkArgument; 020 021import com.google.common.annotations.GwtCompatible; 022import java.util.Comparator; 023import java.util.NoSuchElementException; 024import java.util.SortedMap; 025import javax.annotation.CheckForNull; 026import org.checkerframework.checker.nullness.qual.Nullable; 027 028/** 029 * A sorted map which forwards all its method calls to another sorted map. Subclasses should 030 * override one or more methods to modify the behavior of the backing sorted map as desired per the 031 * <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. 032 * 033 * <p><b>Warning:</b> The methods of {@code ForwardingSortedMap} forward <i>indiscriminately</i> to 034 * the methods of the delegate. For example, overriding {@link #put} alone <i>will not</i> change 035 * the behavior of {@link #putAll}, which can lead to unexpected behavior. In this case, you should 036 * override {@code putAll} as well, either providing your own implementation, or delegating to the 037 * provided {@code standardPutAll} method. 038 * 039 * <p><b>{@code default} method warning:</b> This class does <i>not</i> forward calls to {@code 040 * default} methods. Instead, it inherits their default implementations. When those implementations 041 * invoke methods, they invoke methods on the {@code ForwardingSortedMap}. 042 * 043 * <p>Each of the {@code standard} methods, where appropriate, use the comparator of the map to test 044 * equality for both keys and values, unlike {@code ForwardingMap}. 045 * 046 * <p>The {@code standard} methods and the collection views they return are not guaranteed to be 047 * thread-safe, even when all of the methods that they depend on are thread-safe. 048 * 049 * @author Mike Bostock 050 * @author Louis Wasserman 051 * @since 2.0 052 */ 053@GwtCompatible 054@ElementTypesAreNonnullByDefault 055public abstract class ForwardingSortedMap<K extends @Nullable Object, V extends @Nullable Object> 056 extends ForwardingMap<K, V> implements SortedMap<K, V> { 057 // TODO(lowasser): identify places where thread safety is actually lost 058 059 /** Constructor for use by subclasses. */ 060 protected ForwardingSortedMap() {} 061 062 @Override 063 protected abstract SortedMap<K, V> delegate(); 064 065 @Override 066 @CheckForNull 067 public Comparator<? super K> comparator() { 068 return delegate().comparator(); 069 } 070 071 @Override 072 @ParametricNullness 073 public K firstKey() { 074 return delegate().firstKey(); 075 } 076 077 @Override 078 public SortedMap<K, V> headMap(@ParametricNullness K toKey) { 079 return delegate().headMap(toKey); 080 } 081 082 @Override 083 @ParametricNullness 084 public K lastKey() { 085 return delegate().lastKey(); 086 } 087 088 @Override 089 public SortedMap<K, V> subMap(@ParametricNullness K fromKey, @ParametricNullness K toKey) { 090 return delegate().subMap(fromKey, toKey); 091 } 092 093 @Override 094 public SortedMap<K, V> tailMap(@ParametricNullness K fromKey) { 095 return delegate().tailMap(fromKey); 096 } 097 098 /** 099 * A sensible implementation of {@link SortedMap#keySet} in terms of the methods of {@code 100 * ForwardingSortedMap}. In many cases, you may wish to override {@link 101 * ForwardingSortedMap#keySet} to forward to this implementation or a subclass thereof. 102 * 103 * @since 15.0 104 */ 105 protected class StandardKeySet extends Maps.SortedKeySet<K, V> { 106 /** Constructor for use by subclasses. */ 107 public StandardKeySet() { 108 super(ForwardingSortedMap.this); 109 } 110 } 111 112 // unsafe, but worst case is a CCE or NPE is thrown, which callers will be expecting 113 @SuppressWarnings({"unchecked", "nullness"}) 114 static int unsafeCompare( 115 @CheckForNull Comparator<?> comparator, @CheckForNull Object o1, @CheckForNull Object o2) { 116 if (comparator == null) { 117 return ((Comparable<@Nullable Object>) o1).compareTo(o2); 118 } else { 119 return ((Comparator<@Nullable Object>) comparator).compare(o1, o2); 120 } 121 } 122 123 /** 124 * A sensible definition of {@link #containsKey} in terms of the {@code firstKey()} method of 125 * {@link #tailMap}. If you override {@link #tailMap}, you may wish to override {@link 126 * #containsKey} to forward to this implementation. 127 * 128 * @since 7.0 129 */ 130 @Override 131 protected boolean standardContainsKey(@CheckForNull Object key) { 132 try { 133 // any CCE or NPE will be caught 134 @SuppressWarnings({"unchecked", "nullness"}) 135 SortedMap<@Nullable Object, V> self = (SortedMap<@Nullable Object, V>) this; 136 Object ceilingKey = self.tailMap(key).firstKey(); 137 return unsafeCompare(comparator(), ceilingKey, key) == 0; 138 } catch (ClassCastException | NoSuchElementException | NullPointerException e) { 139 return false; 140 } 141 } 142 143 /** 144 * A sensible default implementation of {@link #subMap(Object, Object)} in terms of {@link 145 * #headMap(Object)} and {@link #tailMap(Object)}. In some situations, you may wish to override 146 * {@link #subMap(Object, Object)} to forward to this implementation. 147 * 148 * @since 7.0 149 */ 150 protected SortedMap<K, V> standardSubMap(K fromKey, K toKey) { 151 checkArgument(unsafeCompare(comparator(), fromKey, toKey) <= 0, "fromKey must be <= toKey"); 152 return tailMap(fromKey).headMap(toKey); 153 } 154}