Top Description Fields Constructors Methods
stormpot

public final Class PoolBuilder<T extends Poolable>

extends Object
implements Cloneable
Class Inheritance
All Implemented Interfaces
java.lang.Cloneable
Type Parameters
<T>
The type of Poolable objects that a Pool based on this `PoolBuilder` will produce.
Imports
java.util.Map, java.util.concurrent.ThreadFactory, .TimeUnit

The `PoolBuilder` collects information about how big a pool should be, and how it should allocate objects, an so on, and finally acts as the factory for building the pool instances themselves, with the build() method. Pool builder instances are obtained by calling one of the `from*` methods on Pool, such as Pool#fromThreaded(Allocator). This class is made thread-safe by having the fields be protected by the intrinsic object lock on the `PoolBuilder` object itself. This way, pools can `synchronize` on the builder object to read the values out atomically. The various set* methods are made to return the `PoolBuilder` instance itself, so that the method calls may be chained if so desired.
Author
Chris Vest

Field Summary

Modifier and TypeField and Description
private final AllocationProcess
private Allocator<T>
private int
private boolean
pack-priv static final Map<AllocationProcessMode, PoolBuilderDefaults>
private Expiration<? super T>
private MetricsRecorder
pack-priv static final Map<AllocationProcessMode, PoolBuilderPermissions>
private final PoolBuilderPermissions
private boolean
private int
private ThreadFactory

Constructor Summary

AccessConstructor and Description
pack-priv
PoolBuilder(AllocationProcess allocationProcess, Allocator<T> allocator)

Build a new empty `PoolBuilder` object.

Method Summary

Modifier and TypeMethod and Description
public synchronized Pool<T>

Returns:

A Pool instance as configured by this builder.
build
()

Build a Pool instance based on the collected configuration.

private void
checkPermission(boolean permission, String fieldDescription)

public final synchronized PoolBuilder<T>

Returns:

A new `PoolBuilder` object of the exact same type as this one, with identical values in all its fields.
clone
()

Overrides java.lang.Object.clone.

Returns a shallow copy of this `PoolBuilder` object.
pack-priv synchronized Reallocator<T>
getAdaptedReallocator()

Returns a `Reallocator`, possibly by adapting the configured `Allocator` if need be.

public synchronized Allocator<T>

Returns:

The configured Allocator instance.
getAllocator
()

Get the configured Allocator instance.

public synchronized int

Returns:

the delay, in milliseconds, between background maintenance tasks.
getBackgroundExpirationCheckDelay
()

Return the default approximate delay, in milliseconds, between background maintenance tasks, such as the background expiration checks and retrying failed allocations.

public synchronized Expiration<? super T>

Returns:

The configured Expiration.
getExpiration
()

Get the configured Expiration instance.

public synchronized MetricsRecorder

Returns:

The configured MetricsRecorder.
getMetricsRecorder
()

Get the configured MetricsRecorder instance, or null if none has been configured.

public synchronized Reallocator<T>

Returns:

A configured or adapted Reallocator.
getReallocator
()

Get the configured stormpot.Allocator instance as a stormpot.Reallocator.

public synchronized int

Returns:

The configured pool size.
getSize
()

Get the currently configured size.

public synchronized ThreadFactory

Returns:

The configured thread factory.
getThreadFactory
()

Get the ThreadFactory that has been configured, and will be used to create the background allocation threads for the pools.

public synchronized boolean

Returns:

`true` if background expiration is enabled.
isBackgroundExpirationEnabled
()

Return whether or not background expiration is enabled.

public synchronized boolean

Returns:

`true` if precise object leak detection is enabled.
isPreciseLeakDetectionEnabled
()

Return whether or not precise object leak detection is enabled, which is the case by default.

public synchronized <
The type of Poolable that is created by the allocator, and the type of objects that the configured pools will contain.
X extends Poolable
>
PoolBuilder<X>

Returns:

This `PoolBuilder` instance, but with a generic type parameter that matches that of the allocator.
setAllocator
(Allocator<X>
The allocator we want our pools to use. This cannot be `null`.
allocator
)

Set the Allocator or Reallocator to use for the pools we want to configure.

public synchronized PoolBuilder<T>

Returns:

This `PoolBuilder` instance.
setBackgroundExpirationCheckDelay
(int
the desired delay, in milliseconds, between background maintenance tasks.
delay
)

Set the approximate delay, in milliseconds, between background maintenance tasks.

public synchronized PoolBuilder<T>

Returns:

This `PoolBuilder` instance.
setBackgroundExpirationEnabled
(boolean
`true` (the default) to turn background expiration checking on, `false` to turn it off.
enabled
)

Enable or disable background object expiration checking.

public synchronized PoolBuilder<T>

Returns:

This `PoolBuilder` instance.
setExpiration
(Expiration<? super T>
The expiration we want our pools to use. Not null.
expiration
)

Set the Expiration to use for the pools we want to configure.

public synchronized PoolBuilder<T>

Returns:

This `PoolBuilder` instance.
setMetricsRecorder
(MetricsRecorder
The MetricsRecorder to use, or null if we don't want to use any.
metricsRecorder
)

Set the MetricsRecorder to use for the pools we want to configure.

public synchronized PoolBuilder<T>

Returns:

This `PoolBuilder` instance.
setPreciseLeakDetectionEnabled
(boolean
`true` to turn on precise object leak detection (the default) `false` to turn it off.
enabled
)

Enable or disable precise object leak detection.

public synchronized PoolBuilder<T>

Returns:

This `PoolBuilder` instance.
setSize
(int
The target pool size. Must be at least 0.
size
)

Set the size of the pool we are building.

public synchronized PoolBuilder<T>

Returns:

This `PoolBuilder` instance.
setThreadFactory
(ThreadFactory
The ThreadFactory the pool should use to create their background threads.
factory
)

Set the ThreadFactory that the pools will use to create its background threads with.

Inherited from java.lang.Object:
equalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

allocationProcessback to summary
private final AllocationProcess allocationProcess
allocatorback to summary
private Allocator<T> allocator
backgroundExpirationCheckDelayback to summary
private int backgroundExpirationCheckDelay
backgroundExpirationEnabledback to summary
private boolean backgroundExpirationEnabled
DEFAULTSback to summary
pack-priv static final Map<AllocationProcessMode, PoolBuilderDefaults> DEFAULTS
expirationback to summary
private Expiration<? super T> expiration
metricsRecorderback to summary
private MetricsRecorder metricsRecorder
PERMISSIONSback to summary
pack-priv static final Map<AllocationProcessMode, PoolBuilderPermissions> PERMISSIONS
permissionsback to summary
private final PoolBuilderPermissions permissions
preciseLeakDetectionEnabledback to summary
private boolean preciseLeakDetectionEnabled
sizeback to summary
private int size
threadFactoryback to summary
private ThreadFactory threadFactory

Constructor Detail

PoolBuilderback to summary
pack-priv PoolBuilder(AllocationProcess allocationProcess, Allocator<T> allocator)

Build a new empty `PoolBuilder` object.

Method Detail

buildback to summary
public synchronized Pool<T> build()

Build a Pool instance based on the collected configuration.

Returns:Pool<T>

A Pool instance as configured by this builder.

checkPermissionback to summary
private void checkPermission(boolean permission, String fieldDescription)
cloneback to summary
public final synchronized PoolBuilder<T> clone()

Overrides java.lang.Object.clone.

Returns a shallow copy of this `PoolBuilder` object.

Returns:PoolBuilder<T>

A new `PoolBuilder` object of the exact same type as this one, with identical values in all its fields.

Annotations
@SuppressWarnings:unchecked
@Override
getAdaptedReallocatorback to summary
pack-priv synchronized Reallocator<T> getAdaptedReallocator()

Returns a `Reallocator`, possibly by adapting the configured `Allocator` if need be. If a `MetricsRecorder` has been configured, the return `Reallocator` will automatically record allocation, reallocation and deallocation latencies.

getAllocatorback to summary
public synchronized Allocator<T> getAllocator()

Get the configured Allocator instance.

Returns:Allocator<T>

The configured Allocator instance.

getBackgroundExpirationCheckDelayback to summary
public synchronized int getBackgroundExpirationCheckDelay()

Return the default approximate delay, in milliseconds, between background maintenance tasks, such as the background expiration checks and retrying failed allocations.

Returns:int

the delay, in milliseconds, between background maintenance tasks.

getExpirationback to summary
public synchronized Expiration<? super T> getExpiration()

Get the configured Expiration instance. The default is a Expiration#after(long, long, TimeUnit) that expires objects after somewhere between 8 to 10 minutes.

Returns:Expiration<? super T>

The configured Expiration.

getMetricsRecorderback to summary
public synchronized MetricsRecorder getMetricsRecorder()

Get the configured MetricsRecorder instance, or null if none has been configured.

Returns:MetricsRecorder

The configured MetricsRecorder.

getReallocatorback to summary
public synchronized Reallocator<T> getReallocator()

Get the configured stormpot.Allocator instance as a stormpot.Reallocator. If the configured allocator implements the Reallocator interface, then it is returned directly. Otherwise, the allocator is wrapped in an adaptor.

Returns:Reallocator<T>

A configured or adapted Reallocator.

getSizeback to summary
public synchronized int getSize()

Get the currently configured size. The default is 10.

Returns:int

The configured pool size.

getThreadFactoryback to summary
public synchronized ThreadFactory getThreadFactory()

Get the ThreadFactory that has been configured, and will be used to create the background allocation threads for the pools. The default is similar to the java.util.concurrent.Executors#defaultThreadFactory(), except the string "Stormpot-" is prepended to the thread name.

Returns:ThreadFactory

The configured thread factory.

isBackgroundExpirationEnabledback to summary
public synchronized boolean isBackgroundExpirationEnabled()

Return whether or not background expiration is enabled. By default, background expiration is enabled.

Returns:boolean

`true` if background expiration is enabled.

See Also
setBackgroundExpirationEnabled(boolean)
isPreciseLeakDetectionEnabledback to summary
public synchronized boolean isPreciseLeakDetectionEnabled()

Return whether or not precise object leak detection is enabled, which is the case by default.

Returns:boolean

`true` if precise object leak detection is enabled.

See Also
setPreciseLeakDetectionEnabled(boolean)
setAllocatorback to summary
public synchronized <X extends Poolable> PoolBuilder<X> setAllocator(Allocator<X> allocator)

Set the Allocator or Reallocator to use for the pools we want to configure. This will change the type-parameter of the `PoolBuilder` object to match that of the new Allocator. The allocator is initially specified by the Pool#from(Allocator) method, so there is usually no need to set it later.

Parameters
<X>
The type of Poolable that is created by the allocator, and the type of objects that the configured pools will contain.
allocator:Allocator<X>

The allocator we want our pools to use. This cannot be `null`.

Returns:PoolBuilder<X>

This `PoolBuilder` instance, but with a generic type parameter that matches that of the allocator.

Annotations
@SuppressWarnings:unchecked
setBackgroundExpirationCheckDelayback to summary
public synchronized PoolBuilder<T> setBackgroundExpirationCheckDelay(int delay)

Set the approximate delay, in milliseconds, between background maintenance tasks. These tasks include the background expiration checks, and retrying failed allocations. The default delay is 1.000 milliseconds (1 second). Lowering this value will improve the pools responsiveness to repairing failed allocations, and also increase the frequency of the background expiration checks. This comes at the cost of higher idle CPU usage. It is not recommended to set this value lower than 100 milliseconds. Values lower than this tend to have increased CPU and power usage, for very little gain in responsiveness for the background tasks.

Parameters
delay:int

the desired delay, in milliseconds, between background maintenance tasks.

Returns:PoolBuilder<T>

This `PoolBuilder` instance.

setBackgroundExpirationEnabledback to summary
public synchronized PoolBuilder<T> setBackgroundExpirationEnabled(boolean enabled)

Enable or disable background object expiration checking. This is enabled by default, but can be turned off if the check is expensive. The cost of the check matters because it might end up taking resources away from the background thread and hinder its ability to keep up with the demand for allocations and deallocations, even though these tasks always take priority over any expiration checking.

Parameters
enabled:boolean

`true` (the default) to turn background expiration checking on, `false` to turn it off.

Returns:PoolBuilder<T>

This `PoolBuilder` instance.

setExpirationback to summary
public synchronized PoolBuilder<T> setExpiration(Expiration<? super T> expiration)

Set the Expiration to use for the pools we want to configure. The Expiration determines when a pooled object is valid for claiming, or when the objects are invalid and should be deallocated. The default Expiration is an Expiration#after(long, long, TimeUnit) that invalidates the objects after they have been active for somewhere between 8 to 10 minutes.

Parameters
expiration:Expiration<? super T>

The expiration we want our pools to use. Not null.

Returns:PoolBuilder<T>

This `PoolBuilder` instance.

setMetricsRecorderback to summary
public synchronized PoolBuilder<T> setMetricsRecorder(MetricsRecorder metricsRecorder)

Set the MetricsRecorder to use for the pools we want to configure.

Parameters
metricsRecorder:MetricsRecorder

The MetricsRecorder to use, or null if we don't want to use any.

Returns:PoolBuilder<T>

This `PoolBuilder` instance.

setPreciseLeakDetectionEnabledback to summary
public synchronized PoolBuilder<T> setPreciseLeakDetectionEnabled(boolean enabled)

Enable or disable precise object leak detection. It is enabled by default. Precise object leak detection makes the pool keep an eye on the allocated Poolables, such that it notices if they get garbage collected without first being deallocated. Using the garbage collector for this purpose, means that no false positives (counting objects as leaked, even though they are not) are ever reported. [NOTE] -- While the pool is able to detect object leaks, it cannot prevent them. All leaks are a sign that there is a bug in the system; most likely a bug in your code, or in the way the pool is used. -- Precise object leak detection incurs virtually no overhead, and is safe to leave enabled at all times – even in the most demanding production environments.

Parameters
enabled:boolean

`true` to turn on precise object leak detection (the default) `false` to turn it off.

Returns:PoolBuilder<T>

This `PoolBuilder` instance.

setSizeback to summary
public synchronized PoolBuilder<T> setSize(int size)

Set the size of the pool we are building. Pools are required to control the allocations and deallocations, such that no more than this number of objects are allocated at any time. This means that a pool of size 1, whose single object have expired, will deallocate that one object before allocating a replacement. The size must be at least one, or an IllegalArgumentException will be thrown when building the pool. Note that the pool size can be modified after the pool has been built, by calling the Pool#setTargetSize(int) or ManagedPool#setTargetSize(int) methods.

Parameters
size:int

The target pool size. Must be at least 0.

Returns:PoolBuilder<T>

This `PoolBuilder` instance.

setThreadFactoryback to summary
public synchronized PoolBuilder<T> setThreadFactory(ThreadFactory factory)

Set the ThreadFactory that the pools will use to create its background threads with. The ThreadFactory is not allowed to be null, and creating a pool with a null ThreadFactory will throw an IllegalArgumentException.

Parameters
factory:ThreadFactory

The ThreadFactory the pool should use to create their background threads.

Returns:PoolBuilder<T>

This `PoolBuilder` instance.