Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2012 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.collect.CollectPreconditions.checkRemove;
 import static com.google.common.collect.Maps.keyOrNull;
 
 
A navigable map which forwards all its method calls to another navigable map. Subclasses should override one or more methods to modify the behavior of the backing map as desired per the decorator pattern.

Warning: The methods of ForwardingNavigableMap forward indiscriminately to the methods of the delegate. For example, overriding ForwardingMap.put(java.lang.Object,java.lang.Object) alone will not change the behavior of ForwardingMap.putAll(java.util.Map), 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 uses the map's comparator (or the natural ordering of the elements, if there is no comparator) to test element equality. As a result, if the comparator is not consistent with equals, some of the standard implementations may violate the Map contract.

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):
Louis Wasserman
Since:
12.0
 
 public abstract class ForwardingNavigableMap<K, V>
     extends ForwardingSortedMap<K, V> implements NavigableMap<K, V> {

  
Constructor for use by subclasses.
 
   protected ForwardingNavigableMap() {}
 
   @Override
   protected abstract NavigableMap<K, V> delegate();
 
   @Override
   public Entry<K, V> lowerEntry(K key) {
     return delegate().lowerEntry(key);
   }

  
A sensible definition of lowerEntry(java.lang.Object) in terms of the lastEntry() of headMap(java.lang.Object,boolean). If you override headMap, you may wish to override lowerEntry to forward to this implementation.
 
   protected Entry<K, V> standardLowerEntry(K key) {
     return headMap(keyfalse).lastEntry();
   }
 
   @Override
   public K lowerKey(K key) {
     return delegate().lowerKey(key);
   }

  
A sensible definition of lowerKey(java.lang.Object) in terms of lowerEntry. If you override lowerEntry(java.lang.Object), you may wish to override lowerKey to forward to this implementation.
 
   protected K standardLowerKey(K key) {
     return keyOrNull(lowerEntry(key));
   }
 
   @Override
   public Entry<K, V> floorEntry(K key) {
     return delegate().floorEntry(key);
   }

  
A sensible definition of floorEntry(java.lang.Object) in terms of the lastEntry() of headMap(java.lang.Object,boolean). If you override headMap, you may wish to override floorEntry to forward to this implementation.
 
   protected Entry<K, V> standardFloorEntry(K key) {
    return headMap(keytrue).lastEntry();
  }
  public K floorKey(K key) {
    return delegate().floorKey(key);
  }

  
A sensible definition of floorKey(java.lang.Object) in terms of floorEntry. If you override floorEntry, you may wish to override floorKey to forward to this implementation.
  protected K standardFloorKey(K key) {
    return keyOrNull(floorEntry(key));
  }
  public Entry<K, V> ceilingEntry(K key) {
    return delegate().ceilingEntry(key);
  }

  
A sensible definition of ceilingEntry(java.lang.Object) in terms of the firstEntry() of tailMap(java.lang.Object,boolean). If you override tailMap, you may wish to override ceilingEntry to forward to this implementation.
  protected Entry<K, V> standardCeilingEntry(K key) {
    return tailMap(keytrue).firstEntry();
  }
  public K ceilingKey(K key) {
    return delegate().ceilingKey(key);
  }

  
A sensible definition of ceilingKey(java.lang.Object) in terms of ceilingEntry. If you override ceilingEntry, you may wish to override ceilingKey to forward to this implementation.
  protected K standardCeilingKey(K key) {
    return keyOrNull(ceilingEntry(key));
  }
  public Entry<K, V> higherEntry(K key) {
    return delegate().higherEntry(key);
  }

  
A sensible definition of higherEntry(java.lang.Object) in terms of the firstEntry() of tailMap(java.lang.Object,boolean). If you override tailMap, you may wish to override higherEntry to forward to this implementation.
  protected Entry<K, V> standardHigherEntry(K key) {
    return tailMap(keyfalse).firstEntry();
  }
  public K higherKey(K key) {
    return delegate().higherKey(key);
  }

  
A sensible definition of higherKey(java.lang.Object) in terms of higherEntry. If you override higherEntry, you may wish to override higherKey to forward to this implementation.
  protected K standardHigherKey(K key) {
    return keyOrNull(higherEntry(key));
  }
  public Entry<K, V> firstEntry() {
    return delegate().firstEntry();
  }

  
A sensible definition of firstEntry() in terms of the iterator() of ForwardingMap.entrySet(). If you override entrySet, you may wish to override firstEntry to forward to this implementation.
  protected Entry<K, V> standardFirstEntry() {
    return Iterables.getFirst(entrySet(), null);
  }

  
A sensible definition of ForwardingSortedMap.firstKey() in terms of firstEntry. If you override firstEntry, you may wish to override firstKey to forward to this implementation.
  protected K standardFirstKey() {
    Entry<K, V> entry = firstEntry();
    if (entry == null) {
      throw new NoSuchElementException();
    } else {
      return entry.getKey();
    }
  }
  public Entry<K, V> lastEntry() {
    return delegate().lastEntry();
  }

  
A sensible definition of lastEntry() in terms of the iterator() of the ForwardingMap.entrySet() of descendingMap(). If you override descendingMap, you may wish to override lastEntry to forward to this implementation.
  protected Entry<K, V> standardLastEntry() {
    return Iterables.getFirst(descendingMap().entrySet(), null);
  }

  
A sensible definition of ForwardingSortedMap.lastKey() in terms of lastEntry. If you override lastEntry, you may wish to override lastKey to forward to this implementation.
  protected K standardLastKey() {
    Entry<K, V> entry = lastEntry();
    if (entry == null) {
      throw new NoSuchElementException();
    } else {
      return entry.getKey();
    }
  }
  public Entry<K, V> pollFirstEntry() {
    return delegate().pollFirstEntry();
  }

  
A sensible definition of pollFirstEntry() in terms of the iterator of entrySet. If you override entrySet, you may wish to override pollFirstEntry to forward to this implementation.
  protected Entry<K, V> standardPollFirstEntry() {
    return Iterators.pollNext(entrySet().iterator());
  }
  public Entry<K, V> pollLastEntry() {
    return delegate().pollLastEntry();
  }

  
A sensible definition of pollFirstEntry() in terms of the iterator of the entrySet of descendingMap. If you override descendingMap, you may wish to override pollFirstEntry to forward to this implementation.
  protected Entry<K, V> standardPollLastEntry() {
    return Iterators.pollNext(descendingMap().entrySet().iterator());
  }
  public NavigableMap<K, V> descendingMap() {
    return delegate().descendingMap();
  }

  
A sensible implementation of java.util.NavigableMap.descendingMap() in terms of the methods of this NavigableMap. In many cases, you may wish to override ForwardingNavigableMap.descendingMap() to forward to this implementation or a subclass thereof.

In particular, this map iterates over entries with repeated calls to java.util.NavigableMap.lowerEntry(java.lang.Object). If a more efficient means of iteration is available, you may wish to override the entryIterator() method of this class.

Since:
12.0
  @Beta
  protected class StandardDescendingMap extends Maps.DescendingMap<K, V> {
    
Constructor for use by subclasses.
    public StandardDescendingMap() {}
    @Override
    NavigableMap<K, V> forward() {
      return ForwardingNavigableMap.this;
    }
    @Override
    protected Iterator<Entry<K, V>> entryIterator() {
      return new Iterator<Entry<K, V>>() {
        private Entry<K, V> toRemove = null;
        private Entry<K, V> nextOrNull = forward().lastEntry();
        @Override
        public boolean hasNext() {
          return  != null;
        }
        @Override
        public java.util.Map.Entry<K, V> next() {
          if (!hasNext()) {
            throw new NoSuchElementException();
          }
          try {
            return ;
          } finally {
             = ;
             = forward().lowerEntry(.getKey());
          }
        }
        @Override
        public void remove() {
          checkRemove( != null);
          forward().remove(.getKey());
           = null;
        }
      };
    }
  }
  public NavigableSet<K> navigableKeySet() {
    return delegate().navigableKeySet();
  }

  
A sensible implementation of java.util.NavigableMap.navigableKeySet() in terms of the methods of this NavigableMap. In many cases, you may wish to override ForwardingNavigableMap.navigableKeySet() to forward to this implementation or a subclass thereof.

Since:
12.0
  @Beta
  protected class StandardNavigableKeySet extends Maps.NavigableKeySet<K, V> {
    
Constructor for use by subclasses.
    public StandardNavigableKeySet() {
      super(ForwardingNavigableMap.this);
    }
  }
  public NavigableSet<K> descendingKeySet() {
    return delegate().descendingKeySet();
  }

  
A sensible definition of descendingKeySet() as the navigableKeySet of descendingMap(). (The ForwardingNavigableMap.StandardDescendingMap implementation implements navigableKeySet on its own, so as not to cause an infinite loop.) If you override descendingMap, you may wish to override descendingKeySet to forward to this implementation.
  @Beta
    return descendingMap().navigableKeySet();
  }

  
A sensible definition of ForwardingSortedMap.subMap(java.lang.Object,java.lang.Object) in terms of subMap(java.lang.Object,boolean,java.lang.Object,boolean). If you override subMap(K, boolean, K, boolean), you may wish to override subMap to forward to this implementation.
  protected SortedMap<K, V> standardSubMap(K fromKey, K toKey) {
    return subMap(fromKeytruetoKeyfalse);
  }
  public NavigableMap<K, V> subMap(K fromKeyboolean fromInclusive, K toKeyboolean toInclusive) {
    return delegate().subMap(fromKeyfromInclusivetoKeytoInclusive);
  }
  public NavigableMap<K, V> headMap(K toKeyboolean inclusive) {
    return delegate().headMap(toKeyinclusive);
  }
  public NavigableMap<K, V> tailMap(K fromKeyboolean inclusive) {
    return delegate().tailMap(fromKeyinclusive);
  }

  
A sensible definition of ForwardingSortedMap.headMap(java.lang.Object) in terms of headMap(java.lang.Object,boolean). If you override headMap(K, boolean), you may wish to override headMap to forward to this implementation.
  protected SortedMap<K, V> standardHeadMap(K toKey) {
    return headMap(toKeyfalse);
  }

  
A sensible definition of ForwardingSortedMap.tailMap(java.lang.Object) in terms of tailMap(java.lang.Object,boolean). If you override tailMap(K, boolean), you may wish to override tailMap to forward to this implementation.
  protected SortedMap<K, V> standardTailMap(K fromKey) {
    return tailMap(fromKeytrue);
  }
New to GrepCode? Check out our FAQ X