Class ForwardingMap<K,V>

java.lang.Object
cloud.opencode.base.collections.ForwardingMap<K,V>
Type Parameters:
K - key type | 键类型
V - value type | 值类型
All Implemented Interfaces:
Map<K,V>

public abstract class ForwardingMap<K,V> extends Object implements Map<K,V>
ForwardingMap - Abstract Decorator Base for Map ForwardingMap - Map 的抽象装饰器基类

An abstract base class that forwards all Map method calls to a delegate. Subclasses implement delegate() and can override individual methods to add custom behavior such as logging, validation, or transformation.

将所有 Map 方法调用转发给委托对象的抽象基类。子类实现 delegate() 并可以重写单个方法以添加自定义行为,如日志记录、验证或转换。

Features | 主要功能:

  • Decorator pattern for Map - Map 的装饰器模式
  • All Map methods forwarded - 所有 Map 方法转发
  • Override individual methods for custom behavior - 重写单个方法以自定义行为
  • Consistent equals/hashCode/toString delegation - 一致的 equals/hashCode/toString 委托

Usage Examples | 使用示例:

// Create a case-insensitive map wrapper
class CaseInsensitiveMap<V> extends ForwardingMap<String, V> {
    private final Map<String, V> delegate;

    CaseInsensitiveMap(Map<String, V> delegate) {
        this.delegate = delegate;
    }

    @Override protected Map<String, V> delegate() { return delegate; }

    @Override public V put(String key, V value) {
        return super.put(key.toLowerCase(), value);
    }
}

Security | 安全性:

  • Thread-safe: Depends on delegate - 线程安全: 取决于委托对象
  • Null-safe: Depends on delegate - 空值安全: 取决于委托对象
Since:
JDK 25, opencode-base-collections V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • ForwardingMap

      protected ForwardingMap()
      Protected constructor for subclasses. 子类的受保护构造方法。
  • Method Details

    • delegate

      protected abstract Map<K,V> delegate()
      Return the backing delegate map. 返回后备委托映射。
      Returns:
      the delegate map | 委托映射
    • size

      public int size()
      Specified by:
      size in interface Map<K,V>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<K,V>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<K,V>
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<K,V>
    • get

      public V get(Object key)
      Specified by:
      get in interface Map<K,V>
    • put

      public V put(K key, V value)
      Specified by:
      put in interface Map<K,V>
    • remove

      public V remove(Object key)
      Specified by:
      remove in interface Map<K,V>
    • putAll

      public void putAll(Map<? extends K, ? extends V> m)
      Put all entries by iterating through put(Object, Object). This ensures subclass overrides of put() are honored. 通过迭代调用 put(Object, Object) 放入所有条目。确保子类对 put() 的覆写被执行。
      Specified by:
      putAll in interface Map<K,V>
      Parameters:
      m - mappings to put | 要放入的映射
    • clear

      public void clear()
      Specified by:
      clear in interface Map<K,V>
    • keySet

      public Set<K> keySet()
      Specified by:
      keySet in interface Map<K,V>
    • values

      public Collection<V> values()
      Specified by:
      values in interface Map<K,V>
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Specified by:
      entrySet in interface Map<K,V>
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Map<K,V>
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object o)
      Specified by:
      equals in interface Map<K,V>
      Overrides:
      equals in class Object