Class BasePooledObjectFactory<T>
java.lang.Object
cloud.opencode.base.pool.factory.BasePooledObjectFactory<T>
- Type Parameters:
T- the type of object being pooled - 池化对象类型
- All Implemented Interfaces:
PooledObjectFactory<T>
BasePooledObjectFactory - Base Pooled Object Factory
BasePooledObjectFactory - 基础池化对象工厂
Abstract base class for PooledObjectFactory implementations. Subclasses only need to implement the create() method.
PooledObjectFactory实现的抽象基类。子类只需实现create()方法。
Features | 主要功能:
- Simplified factory implementation - 简化的工厂实现
- Default no-op lifecycle methods - 默认空操作的生命周期方法
- Automatic object wrapping - 自动对象包装
- Override only what you need - 只覆盖需要的方法
Usage Examples | 使用示例:
PooledObjectFactory<StringBuilder> factory = new BasePooledObjectFactory<>() {
@Override
protected StringBuilder create() {
return new StringBuilder();
}
@Override
public void passivateObject(PooledObject<StringBuilder> obj) {
obj.getObject().setLength(0); // Reset on return
}
};
Security | 安全性:
- Thread-safe: Implementation dependent - 线程安全: 取决于实现
- Null-safe: No - 空值安全: 否
- Since:
- JDK 25, opencode-base-pool V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidactivateObject(PooledObject<T> obj) Activates a pooled object.protected abstract Tcreate()Creates the actual object instance.voiddestroyObject(PooledObject<T> obj) Destroys a pooled object.Creates a new pooled object.voidpassivateObject(PooledObject<T> obj) Passivates a pooled object.booleanvalidateObject(PooledObject<T> obj) Validates a pooled object.protected PooledObject<T> Wraps an object in a PooledObject wrapper.
-
Constructor Details
-
BasePooledObjectFactory
public BasePooledObjectFactory()
-
-
Method Details
-
create
Creates the actual object instance. 创建实际的对象实例。Subclasses must implement this method to create the object.
子类必须实现此方法来创建对象。
- Returns:
- the new object - 新对象
- Throws:
OpenPoolException- if creation fails - 如果创建失败
-
wrap
Wraps an object in a PooledObject wrapper. 将对象包装在PooledObject包装器中。- Parameters:
obj- the object to wrap - 要包装的对象- Returns:
- the wrapped object - 包装后的对象
-
makeObject
Description copied from interface:PooledObjectFactoryCreates a new pooled object. 创建新的池化对象。Called when the pool needs to create a new object.
当池需要创建新对象时调用。
- Specified by:
makeObjectin interfacePooledObjectFactory<T>- Returns:
- the new pooled object - 新的池化对象
- Throws:
OpenPoolException- if creation fails - 如果创建失败
-
destroyObject
Description copied from interface:PooledObjectFactoryDestroys a pooled object. 销毁池化对象。Called when the object is being removed from the pool.
当对象从池中移除时调用。
- Specified by:
destroyObjectin interfacePooledObjectFactory<T>- Parameters:
obj- the object to destroy - 要销毁的对象- Throws:
OpenPoolException- if destruction fails - 如果销毁失败
-
validateObject
Description copied from interface:PooledObjectFactoryValidates a pooled object. 验证池化对象。Called to check if the object is still valid for use.
调用以检查对象是否仍可使用。
- Specified by:
validateObjectin interfacePooledObjectFactory<T>- Parameters:
obj- the object to validate - 要验证的对象- Returns:
- true if valid, false otherwise - 有效返回true,否则返回false
-
activateObject
Description copied from interface:PooledObjectFactoryActivates a pooled object. 激活池化对象。Called before the object is borrowed from the pool.
在对象从池中借出前调用。
- Specified by:
activateObjectin interfacePooledObjectFactory<T>- Parameters:
obj- the object to activate - 要激活的对象- Throws:
OpenPoolException- if activation fails - 如果激活失败
-
passivateObject
Description copied from interface:PooledObjectFactoryPassivates a pooled object. 钝化池化对象。Called after the object is returned to the pool.
在对象归还到池后调用。
- Specified by:
passivateObjectin interfacePooledObjectFactory<T>- Parameters:
obj- the object to passivate - 要钝化的对象- Throws:
OpenPoolException- if passivation fails - 如果钝化失败
-