Class ForwardingCollection<E>

java.lang.Object
cloud.opencode.base.collections.ForwardingCollection<E>
Type Parameters:
E - element type | 元素类型
All Implemented Interfaces:
Iterable<E>, Collection<E>
Direct Known Subclasses:
ForwardingList, ForwardingSet

public abstract class ForwardingCollection<E> extends Object implements Collection<E>
ForwardingCollection - Abstract Decorator Base for Collection ForwardingCollection - Collection 的抽象装饰器基类

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

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

Features | 主要功能:

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

Usage Examples | 使用示例:

// Create a logging collection wrapper
class LoggingCollection<E> extends ForwardingCollection<E> {
    private final Collection<E> delegate;

    LoggingCollection(Collection<E> delegate) {
        this.delegate = delegate;
    }

    @Override protected Collection<E> delegate() { return delegate; }

    @Override public boolean add(E e) {
        System.out.println("Adding: " + e);
        return super.add(e);
    }
}

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

    • ForwardingCollection

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