Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2007 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.google.common.collect;
 
 import static com.google.common.base.Preconditions.checkArgument;
 
 
 
 import  javax.annotation.Nullable;

A sorted map which forwards all its method calls to another sorted map. Subclasses should override one or more methods to modify the behavior of the backing sorted map as desired per the decorator pattern.

Warning: The methods of ForwardingSortedMap forward indiscriminately to the methods of the delegate. For example, overriding put alone will not change the behavior of putAll, which can lead to unexpected behavior. In this case, you should override putAll as well, either providing your own implementation, or delegating to the provided standardPutAll method.

Each of the standard methods, where appropriate, use the comparator of the map to test equality for both keys and values, unlike ForwardingMap.

The standard methods and the collection views they return are not guaranteed to be thread-safe, even when all of the methods that they depend on are thread-safe.

Author(s):
Mike Bostock
Louis Wasserman
Since:
2.0 (imported from Google Collections Library)
 
 public abstract class ForwardingSortedMap<K, V> extends ForwardingMap<K, V>
     implements SortedMap<K, V> {
   // TODO(user): identify places where thread safety is actually lost
 
  
Constructor for use by subclasses.
 
   protected ForwardingSortedMap() {}
 
   @Override protected abstract SortedMap<K, V> delegate();
 
   @Override
   public Comparator<? super K> comparator() {
     return delegate().comparator();
   }
 
   @Override
   public K firstKey() {
     return delegate().firstKey();
   }
 
   @Override
   public SortedMap<K, V> headMap(K toKey) {
     return delegate().headMap(toKey);
   }
 
   @Override
   public K lastKey() {
     return delegate().lastKey();
   }
 
   @Override
   public SortedMap<K, V> subMap(K fromKey, K toKey) {
     return delegate().subMap(fromKeytoKey);
   }
 
   @Override
   public SortedMap<K, V> tailMap(K fromKey) {
     return delegate().tailMap(fromKey);
   }

  
A sensible implementation of SortedMap.keySet in terms of the methods of ForwardingSortedMap. In many cases, you may wish to override ForwardingSortedMap.keySet to forward to this implementation or a subclass thereof.

Since:
15.0
  @Beta
  protected class StandardKeySet extends Maps.SortedKeySet<K, V> {
    
Constructor for use by subclasses.
    public StandardKeySet() {
      super(ForwardingSortedMap.this);
    }
  }
  // unsafe, but worst case is a CCE is thrown, which callers will be expecting
  @SuppressWarnings("unchecked")
  private int unsafeCompare(Object k1Object k2) {
    Comparator<? super K> comparator = comparator();
    if (comparator == null) {
      return ((Comparable<Object>) k1).compareTo(k2);
    } else {
      return ((Comparator<Object>) comparator).compare(k1k2);
    }
  }

  
A sensible definition of containsKey in terms of the firstKey() method of tailMap. If you override tailMap, you may wish to override containsKey to forward to this implementation.

Since:
7.0
  @Override @Beta protected boolean standardContainsKey(@Nullable Object key) {
    try {
      // any CCE will be caught
      @SuppressWarnings("unchecked")
      SortedMap<Object, V> self = (SortedMap<Object, V>) this;
      Object ceilingKey = self.tailMap(key).firstKey();
      return unsafeCompare(ceilingKeykey) == 0;
    } catch (ClassCastException e) {
      return false;
    } catch (NoSuchElementException e) {
      return false;
    } catch (NullPointerException e) {
      return false;
    }
  }

  
A sensible definition of remove in terms of the iterator() of the entrySet() of tailMap. If you override tailMap, you may wish to override remove to forward to this implementation.

Deprecated:
This implementation is extremely awkward, is rarely worthwhile, and has been discovered to interact badly with http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6467933 in Java 6 when used with certain null-friendly comparators. It is scheduled for deletion in Guava 16.0.
Since:
7.0
  @Override @Beta protected V standardRemove(@Nullable Object key) {
    try {
      // any CCE will be caught
      @SuppressWarnings("unchecked")
      SortedMap<Object, V> self = (SortedMap<Object, V>) this;
      Iterator<Entry<Object, V>> entryIterator =
          self.tailMap(key).entrySet().iterator();
      if (entryIterator.hasNext()) {
        Entry<Object, V> ceilingEntry = entryIterator.next();
        if (unsafeCompare(ceilingEntry.getKey(), key) == 0) {
          V value = ceilingEntry.getValue();
          entryIterator.remove();
          return value;
        }
      }
    } catch (ClassCastException e) {
      return null;
    } catch (NullPointerException e) {
      return null;
    }
    return null;
  }

  
A sensible default implementation of subMap(Object, Object) in terms of headMap(Object) and tailMap(Object). In some situations, you may wish to override subMap(Object, Object) to forward to this implementation.

Since:
7.0
  @Beta protected SortedMap<K, V> standardSubMap(K fromKey, K toKey) {
    checkArgument(unsafeCompare(fromKeytoKey) <= 0, "fromKey must be <= toKey");
    return tailMap(fromKey).headMap(toKey);
  }
New to GrepCode? Check out our FAQ X