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
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 Summary
ConstructorsModifierConstructorDescriptionprotectedProtected constructor for subclasses. -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleanaddAll(Collection<? extends E> c) Add all elements by iterating throughadd(Object).voidclear()booleanbooleancontainsAll(Collection<?> c) protected abstract Collection<E> delegate()Return the backing delegate collection.booleaninthashCode()booleanisEmpty()iterator()booleanbooleanremoveAll(Collection<?> c) booleanretainAll(Collection<?> c) intsize()Object[]toArray()<T> T[]toArray(T[] a) toString()Methods inherited from interface Collection
parallelStream, removeIf, spliterator, stream, toArray
-
Constructor Details
-
ForwardingCollection
protected ForwardingCollection()Protected constructor for subclasses. 子类的受保护构造方法。
-
-
Method Details
-
delegate
Return the backing delegate collection. 返回后备委托集合。- Returns:
- the delegate collection | 委托集合
-
size
public int size()- Specified by:
sizein interfaceCollection<E>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection<E>
-
contains
- Specified by:
containsin interfaceCollection<E>
-
iterator
-
toArray
- Specified by:
toArrayin interfaceCollection<E>
-
toArray
public <T> T[] toArray(T[] a) - Specified by:
toArrayin interfaceCollection<E>
-
add
- Specified by:
addin interfaceCollection<E>
-
remove
- Specified by:
removein interfaceCollection<E>
-
containsAll
- Specified by:
containsAllin interfaceCollection<E>
-
addAll
Add all elements by iterating throughadd(Object). This ensures subclass overrides ofadd()are honored. 通过迭代调用add(Object)添加所有元素。确保子类对add()的覆写被执行。- Specified by:
addAllin interfaceCollection<E>- Parameters:
c- collection of elements to add | 要添加的元素集合- Returns:
- true if this collection changed | 如果集合发生变化则返回 true
-
removeAll
- Specified by:
removeAllin interfaceCollection<E>
-
retainAll
- Specified by:
retainAllin interfaceCollection<E>
-
clear
public void clear()- Specified by:
clearin interfaceCollection<E>
-
toString
-
hashCode
public int hashCode()- Specified by:
hashCodein interfaceCollection<E>- Overrides:
hashCodein classObject
-
equals
-