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.Beta; 022import com.google.common.annotations.GwtCompatible; 023 024import java.util.Comparator; 025import java.util.NoSuchElementException; 026import java.util.SortedMap; 027 028import javax.annotation.Nullable; 029 030/** 031 * A sorted map which forwards all its method calls to another sorted map. 032 * Subclasses should override one or more methods to modify the behavior of 033 * the backing sorted map as desired per the <a 034 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. 035 * 036 * <p><i>Warning:</i> The methods of {@code ForwardingSortedMap} forward 037 * <i>indiscriminately</i> to the methods of the delegate. For example, 038 * overriding {@link #put} alone <i>will not</i> change the behavior of {@link 039 * #putAll}, which can lead to unexpected behavior. In this case, you should 040 * override {@code putAll} as well, either providing your own implementation, or 041 * delegating to the provided {@code standardPutAll} method. 042 * 043 * <p>Each of the {@code standard} methods, where appropriate, use the 044 * comparator of the map to test equality for both keys and values, unlike 045 * {@code ForwardingMap}. 046 * 047 * <p>The {@code standard} methods and the collection views they return are not 048 * guaranteed to be thread-safe, even when all of the methods that they depend 049 * on are thread-safe. 050 * 051 * @author Mike Bostock 052 * @author Louis Wasserman 053 * @since 2.0 054 */ 055@GwtCompatible 056public abstract class ForwardingSortedMap<K, V> extends ForwardingMap<K, V> 057 implements SortedMap<K, V> { 058 // TODO(lowasser): identify places where thread safety is actually lost 059 060 /** Constructor for use by subclasses. */ 061 protected ForwardingSortedMap() {} 062 063 @Override 064 protected abstract SortedMap<K, V> delegate(); 065 066 @Override 067 public Comparator<? super K> comparator() { 068 return delegate().comparator(); 069 } 070 071 @Override 072 public K firstKey() { 073 return delegate().firstKey(); 074 } 075 076 @Override 077 public SortedMap<K, V> headMap(K toKey) { 078 return delegate().headMap(toKey); 079 } 080 081 @Override 082 public K lastKey() { 083 return delegate().lastKey(); 084 } 085 086 @Override 087 public SortedMap<K, V> subMap(K fromKey, K toKey) { 088 return delegate().subMap(fromKey, toKey); 089 } 090 091 @Override 092 public SortedMap<K, V> tailMap(K fromKey) { 093 return delegate().tailMap(fromKey); 094 } 095 096 /** 097 * A sensible implementation of {@link SortedMap#keySet} in terms of the methods of 098 * {@code ForwardingSortedMap}. In many cases, you may wish to override 099 * {@link ForwardingSortedMap#keySet} to forward to this implementation or a subclass thereof. 100 * 101 * @since 15.0 102 */ 103 @Beta 104 protected class StandardKeySet extends Maps.SortedKeySet<K, V> { 105 /** Constructor for use by subclasses. */ 106 public StandardKeySet() { 107 super(ForwardingSortedMap.this); 108 } 109 } 110 111 // unsafe, but worst case is a CCE is thrown, which callers will be expecting 112 @SuppressWarnings("unchecked") 113 private int unsafeCompare(Object k1, Object k2) { 114 Comparator<? super K> comparator = comparator(); 115 if (comparator == null) { 116 return ((Comparable<Object>) k1).compareTo(k2); 117 } else { 118 return ((Comparator<Object>) comparator).compare(k1, k2); 119 } 120 } 121 122 /** 123 * A sensible definition of {@link #containsKey} in terms of the {@code 124 * firstKey()} method of {@link #tailMap}. If you override {@link #tailMap}, 125 * you may wish to override {@link #containsKey} to forward to this 126 * implementation. 127 * 128 * @since 7.0 129 */ 130 @Override 131 @Beta 132 protected boolean standardContainsKey(@Nullable Object key) { 133 try { 134 // any CCE will be caught 135 @SuppressWarnings("unchecked") 136 SortedMap<Object, V> self = (SortedMap<Object, V>) this; 137 Object ceilingKey = self.tailMap(key).firstKey(); 138 return unsafeCompare(ceilingKey, key) == 0; 139 } catch (ClassCastException e) { 140 return false; 141 } catch (NoSuchElementException e) { 142 return false; 143 } catch (NullPointerException e) { 144 return false; 145 } 146 } 147 148 /** 149 * A sensible default implementation of {@link #subMap(Object, Object)} in 150 * terms of {@link #headMap(Object)} and {@link #tailMap(Object)}. In some 151 * situations, you may wish to override {@link #subMap(Object, Object)} to 152 * forward to this implementation. 153 * 154 * @since 7.0 155 */ 156 @Beta 157 protected SortedMap<K, V> standardSubMap(K fromKey, K toKey) { 158 checkArgument(unsafeCompare(fromKey, toKey) <= 0, "fromKey must be <= toKey"); 159 return tailMap(fromKey).headMap(toKey); 160 } 161}