Class ForwardingList<E>

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

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

An abstract base class that forwards all List method calls to a delegate. Extends ForwardingCollection and adds List-specific method delegation. Subclasses implement delegate() and can override individual methods to add custom behavior.

将所有 List 方法调用转发给委托对象的抽象基类。扩展 ForwardingCollection 并添加 List 特定的方法委托。子类实现 delegate() 并可以重写单个方法以添加自定义行为。

Features | 主要功能:

  • Decorator pattern for List - List 的装饰器模式
  • All List methods forwarded - 所有 List 方法转发
  • Includes index-based operations - 包含基于索引的操作
  • ListIterator and subList delegation - ListIterator 和 subList 委托

Usage Examples | 使用示例:

// Create a read-only list wrapper
class UnmodifiableList<E> extends ForwardingList<E> {
    private final List<E> delegate;

    UnmodifiableList(List<E> delegate) {
        this.delegate = delegate;
    }

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

    @Override public boolean add(E e) {
        throw new UnsupportedOperationException();
    }
}

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

    • ForwardingList

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

    • delegate

      protected abstract List<E> delegate()
      Return the backing delegate list. 返回后备委托列表。
      Specified by:
      delegate in class ForwardingCollection<E>
      Returns:
      the delegate list | 委托列表
    • add

      public void add(int index, E element)
      Specified by:
      add in interface List<E>
    • addAll

      public boolean addAll(int index, Collection<? extends E> c)
      Add all elements at the given index by iterating through add(int, Object). This ensures subclass overrides of add(int, E) are honored. 通过迭代调用 add(int, Object) 在指定位置添加所有元素。确保子类对 add(int, E) 的覆写被执行。
      Specified by:
      addAll in interface List<E>
      Parameters:
      index - position to insert | 插入位置
      c - collection of elements to add | 要添加的元素集合
      Returns:
      true if this list changed | 如果列表发生变化则返回 true
    • get

      public E get(int index)
      Specified by:
      get in interface List<E>
    • set

      public E set(int index, E element)
      Specified by:
      set in interface List<E>
    • remove

      public E remove(int index)
      Specified by:
      remove in interface List<E>
    • indexOf

      public int indexOf(Object o)
      Specified by:
      indexOf in interface List<E>
    • lastIndexOf

      public int lastIndexOf(Object o)
      Specified by:
      lastIndexOf in interface List<E>
    • listIterator

      public ListIterator<E> listIterator()
      Specified by:
      listIterator in interface List<E>
    • listIterator

      public ListIterator<E> listIterator(int index)
      Specified by:
      listIterator in interface List<E>
    • subList

      public List<E> subList(int fromIndex, int toIndex)
      Specified by:
      subList in interface List<E>