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 java.util.Map;
 import java.util.Set;
 
A map which forwards all its method calls to another 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 ForwardingMap forward indiscriminately to the methods of the delegate. For example, overriding put(java.lang.Object,java.lang.Object) alone will not change the behavior of 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, where appropriate, use com.google.common.base.Objects.equal(java.lang.Object,java.lang.Object) to test equality for both keys and values. This may not be the desired behavior for map implementations that use non-standard notions of key equality, such as a SortedMap whose comparator is not consistent with equals.

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):
Kevin Bourrillion
Jared Levy
Louis Wasserman
Since:
2.0 (imported from Google Collections Library)
 
 public abstract class ForwardingMap<K, V> extends ForwardingObject
     implements Map<K, V> {
   // TODO(user): identify places where thread safety is actually lost
 
  
Constructor for use by subclasses.
 
   protected ForwardingMap() {}
 
   @Override protected abstract Map<K, V> delegate();
 
   @Override
   public int size() {
     return delegate().size();
   }
 
   @Override
   public boolean isEmpty() {
     return delegate().isEmpty();
   }
 
   @Override
   public V remove(Object object) {
     return delegate().remove(object);
   }
 
   @Override
   public void clear() {
     delegate().clear();
   }
 
   @Override
   public boolean containsKey(Object key) {
     return delegate().containsKey(key);
   }
 
   @Override
   public boolean containsValue(Object value) {
     return delegate().containsValue(value);
   }
 
   @Override
   public V get(Object key) {
    return delegate().get(key);
  }
  public V put(K key, V value) {
    return delegate().put(keyvalue);
  }
  public void putAll(Map<? extends K, ? extends V> map) {
    delegate().putAll(map);
  }
  public Set<K> keySet() {
    return delegate().keySet();
  }
  public Collection<V> values() {
    return delegate().values();
  }
  public Set<Entry<K, V>> entrySet() {
    return delegate().entrySet();
  }
  @Override public boolean equals(@Nullable Object object) {
    return object == this || delegate().equals(object);
  }
  @Override public int hashCode() {
    return delegate().hashCode();
  }

  
A sensible definition of putAll(java.util.Map) in terms of put(java.lang.Object,java.lang.Object). If you override put(java.lang.Object,java.lang.Object), you may wish to override putAll(java.util.Map) to forward to this implementation.

Since:
7.0
  @Beta protected void standardPutAll(Map<? extends K, ? extends V> map) {
    Maps.putAllImpl(thismap);
  }

  
A sensible, albeit inefficient, definition of remove(java.lang.Object) in terms of the iterator method of entrySet(). If you override entrySet(), you may wish to override remove(java.lang.Object) to forward to this implementation.

Alternately, you may wish to override remove(java.lang.Object) with keySet().remove, assuming that approach would not lead to an infinite loop.

Since:
7.0
  @Beta protected V standardRemove(@Nullable Object key) {
    Iterator<Entry<K, V>> entryIterator = entrySet().iterator();
    while (entryIterator.hasNext()) {
      Entry<K, V> entry = entryIterator.next();
      if (Objects.equal(entry.getKey(), key)) {
        V value = entry.getValue();
        entryIterator.remove();
        return value;
      }
    }
    return null;
  }

  
A sensible definition of clear() in terms of the iterator method of entrySet(). In many cases, you may wish to override clear() to forward to this implementation.

Since:
7.0
  @Beta protected void standardClear() {
    Iterator<Entry<K, V>> entryIterator = entrySet().iterator();
    while (entryIterator.hasNext()) {
      entryIterator.next();
      entryIterator.remove();
    }
  }

  
A sensible implementation of java.util.Map.keySet() in terms of the following methods: ForwardingMap.clear(), ForwardingMap.containsKey(java.lang.Object), ForwardingMap.isEmpty(), ForwardingMap.remove(java.lang.Object), ForwardingMap.size(), and the java.util.Set.iterator() method of ForwardingMap.entrySet(). In many cases, you may wish to override ForwardingMap.keySet() to forward to this implementation or a subclass thereof.

Since:
10.0
  @Beta
  protected class StandardKeySet extends Maps.KeySet<K, V> {
    
Constructor for use by subclasses.
    public StandardKeySet() {}
    @Override
    Map<K, V> map() {
      return ForwardingMap.this;
    }
  }

  
A sensible, albeit inefficient, definition of containsKey(java.lang.Object) in terms of the iterator method of entrySet(). If you override entrySet(), you may wish to override containsKey(java.lang.Object) to forward to this implementation.

Since:
7.0
  @Beta protected boolean standardContainsKey(@Nullable Object key) {
    return Maps.containsKeyImpl(thiskey);
  }

  
A sensible implementation of java.util.Map.values() in terms of the following methods: ForwardingMap.clear(), ForwardingMap.containsValue(java.lang.Object), ForwardingMap.isEmpty(), ForwardingMap.size(), and the java.util.Set.iterator() method of ForwardingMap.entrySet(). In many cases, you may wish to override ForwardingMap.values() to forward to this implementation or a subclass thereof.

Since:
10.0
  @Beta
  protected class StandardValues extends Maps.Values<K, V> {
    
Constructor for use by subclasses.
    public StandardValues() {}
    @Override
    Map<K, V> map() {
      return ForwardingMap.this;
    }
  }

  
A sensible definition of containsValue(java.lang.Object) in terms of the iterator method of entrySet(). If you override entrySet(), you may wish to override containsValue(java.lang.Object) to forward to this implementation.

Since:
7.0
  @Beta protected boolean standardContainsValue(@Nullable Object value) {
    return Maps.containsValueImpl(thisvalue);
  }

  
A sensible implementation of java.util.Map.entrySet() in terms of the following methods: ForwardingMap.clear(), ForwardingMap.containsKey(java.lang.Object), ForwardingMap.get(java.lang.Object), ForwardingMap.isEmpty(), ForwardingMap.remove(java.lang.Object), and ForwardingMap.size(). In many cases, you may wish to override ForwardingMap.entrySet() to forward to this implementation or a subclass thereof.

Since:
10.0
  @Beta
  protected abstract class StandardEntrySet extends Maps.EntrySet<K, V> {
    
Constructor for use by subclasses.
    public StandardEntrySet() {}
    @Override
    Map<K, V> map() {
      return ForwardingMap.this;
    }
  }

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

Since:
7.0
  @Beta protected boolean standardIsEmpty() {
    return !entrySet().iterator().hasNext();
  }

  
A sensible definition of equals(java.lang.Object) in terms of the equals method of entrySet(). If you override entrySet(), you may wish to override equals(java.lang.Object) to forward to this implementation.

Since:
7.0
  @Beta protected boolean standardEquals(@Nullable Object object) {
    return Maps.equalsImpl(thisobject);
  }

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

Since:
7.0
  @Beta protected int standardHashCode() {
    return Sets.hashCodeImpl(entrySet());
  }

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

Since:
7.0
  @Beta protected String standardToString() {
    return Maps.toStringImpl(this);
  }
New to GrepCode? Check out our FAQ X