Package org.apache.commons.pool2

# Interface PooledObject<T>

Type Parameters:
  `T` - the type of object in the pool.

All Superinterfaces:
  `Comparable<PooledObject<T>>`

All Known Implementing Classes:
  `DefaultPooledObject`, `PooledSoftReference`

public interface PooledObject<T> extends Comparable<PooledObject<T>>
Defines the wrapper that is used to track the additional information, such as state, for the pooled objects.

Implementations of this class are required to be thread-safe.

Since:
  2.0

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `allocate()`
  Allocates the object.
  `int`
  `compareTo(PooledObject<T> other)`
  Orders instances based on idle time - i.e. the length of time since the instance was returned to the pool.
  `boolean`
  `deallocate()`
  Deallocates the object and sets it `IDLE` if it is currently `ALLOCATED`.
  `boolean`
  `endEvictionTest(Deque<PooledObject<T>> idleQueue)`
  Notifies the object that the eviction test has ended.
  `boolean`
  `equals(Object obj)`
  `default Duration`
  `getActiveDuration()`
  Gets the amount of time this object last spent in the active state (it may still be active in which case subsequent calls will return an increased value).
  `default Duration`
  `getActiveTime()`
  Deprecated.
  Use `getActiveDuration()`.
  `long`
  `getActiveTimeMillis()`
  Deprecated.
  Use `getActiveTime()` which offers the best precision.
  `default long`
  `getBorrowedCount()`
  Gets the number of times this object has been borrowed.
  `default Instant`
  `getCreateInstant()`
  Gets the time (using the same basis as `Instant.now()`) that this object was created.
  `long`
  `getCreateTime()`
  Deprecated.
  Use `getCreateInstant()` which offers the best precision.
  `default Duration`
  `getFullDuration()`
  Gets the duration since this object was created (using `Instant.now()`).
  `default Duration`
  `getIdleDuration()`
  Gets the amount of time that this object last spend in the idle state (it may still be idle in which case subsequent calls will return an increased value).
  `default Duration`
  `getIdleTime()`
  Deprecated.
  Use `getIdleDuration()`.
  `long`
  `getIdleTimeMillis()`
  Deprecated.
  Use `getIdleTime()` which offers the best precision.
  `default Instant`
  `getLastBorrowInstant()`
  Gets the time the wrapped object was last borrowed.
  `long`
  `getLastBorrowTime()`
  Deprecated.
  Use `getLastBorrowInstant()` which offers the best precision.
  `default Instant`
  `getLastReturnInstant()`
  Gets the time the wrapped object was last borrowed.
  `long`
  `getLastReturnTime()`
  Deprecated.
  Use `getLastReturnInstant()` which offers the best precision.
  `default Instant`
  `getLastUsedInstant()`
  Gets an estimate of the last time this object was used.
  `long`
  `getLastUsedTime()`
  Deprecated.
  Use `getLastUsedInstant()` which offers the best precision.
  `T`
  `getObject()`
  Gets the underlying object that is wrapped by this instance of `PooledObject`.
  `static <T> T`
  `getObject(PooledObject<T> pooledObject)`
  Gets the wrapped object or null.
  `PooledObjectState`
  `getState()`
  Gets the state of this object.
  `int`
  `hashCode()`
  `void`
  `invalidate()`
  Sets the state to `INVALID`.
  `static boolean`
  `isNull(PooledObject<?> pooledObject)`
  Tests whether the given PooledObject is null or wraps a null.
  `void`
  `markAbandoned()`
  Marks the pooled object as abandoned.
  `void`
  `markReturning()`
  Marks the object as returning to the pool.
  `static boolean`
  `nonNull(PooledObject<?> pooledObject)`
  Tests whether the given PooledObject isn't null and doesn't wraps a null.
  `void`
  `printStackTrace(PrintWriter writer)`
  Prints the stack trace of the code that borrowed this pooled object and the stack trace of the last code to use this object (if available) to the supplied writer.
  `void`
  `setLogAbandoned(boolean logAbandoned)`
  Sets whether to use abandoned object tracking.
  `default void`
  `setRequireFullStackTrace(boolean requireFullStackTrace)`
  Sets the stack trace generation strategy based on whether or not fully detailed stack traces are required.
  `boolean`
  `startEvictionTest()`
  Attempts to place the pooled object in the `PooledObjectState.EVICTION` state.
  `String`
  `toString()`
  Gets a String form of the wrapper for debug purposes.
  `void`
  `use()`
  Records the current stack trace as the last time the object was used.

- ## Method Details

  - ### getObject

    static <T> T getObject(PooledObject<T> pooledObject)
    Gets the wrapped object or null.
    Type Parameters:
      `T` - the type of object in the pool.
    Parameters:
      `pooledObject` - the PooledObject to unwrap, may be null.
    Returns:
      the wrapped object or null.
    Since:
      2.13.0

  - ### isNull

    static boolean isNull(PooledObject<?> pooledObject)
    Tests whether the given PooledObject is null or wraps a null.
    Parameters:
      `pooledObject` - the PooledObject to test, may be null.
    Returns:
      whether the given PooledObject is null or wraps a null.
    Since:
      2.12.0

  - ### nonNull

    static boolean nonNull(PooledObject<?> pooledObject)
    Tests whether the given PooledObject isn't null and doesn't wraps a null.
    Parameters:
      `pooledObject` - the PooledObject to test, may be null.
    Returns:
      whether the given PooledObject isn't null and doesn't wraps a null.
    Since:
      2.13.0

  - ### allocate

    boolean allocate()
    Allocates the object.
    Returns:
      `true` if the original state was `IDLE`

  - ### compareTo

    int compareTo(PooledObject<T> other)
    Orders instances based on idle time - i.e. the length of time since the instance was returned to the pool. Used by the GKOP idle object evictor.

    Note: This class has a natural ordering that is inconsistent with equals if distinct objects have the same identity hash code.

    Specified by:
      `compareTo` in interface `Comparable<T>`

  - ### deallocate

    boolean deallocate()
    Deallocates the object and sets it `IDLE` if it is currently `ALLOCATED`.
    Returns:
      `true` if the state was `ALLOCATED`.

  - ### endEvictionTest

    boolean endEvictionTest(Deque<PooledObject<T>> idleQueue)
    Notifies the object that the eviction test has ended.
    Parameters:
      `idleQueue` - The queue of idle objects to which the object should be returned.
    Returns:
      Currently not used.

  - ### equals

    boolean equals(Object obj)
    Overrides:
      `equals` in class `Object`

  - ### getActiveDuration

    default Duration getActiveDuration()
    Gets the amount of time this object last spent in the active state (it may still be active in which case subsequent calls will return an increased value).
    Returns:
      The duration last spent in the active state.
    Since:
      2.11.0

  - ### getActiveTime

    @Deprecated default Duration getActiveTime()
    Deprecated.
    Use `getActiveDuration()`.
    Gets the amount of time this object last spent in the active state (it may still be active in which case subsequent calls will return an increased value).
    Returns:
      The duration last spent in the active state.
    Since:
      2.10.0

  - ### getActiveTimeMillis

    @Deprecated long getActiveTimeMillis()
    Deprecated.
    Use `getActiveTime()` which offers the best precision.
    Gets the amount of time in milliseconds this object last spent in the active state (it may still be active in which case subsequent calls will return an increased value).
    Returns:
      The time in milliseconds last spent in the active state.

  - ### getBorrowedCount

    default long getBorrowedCount()
    Gets the number of times this object has been borrowed.
    Returns:
      -1 by default for implementations prior to release 2.7.0.
    Since:
      2.7.0

  - ### getCreateInstant

    default Instant getCreateInstant()
    Gets the time (using the same basis as `Instant.now()`) that this object was created.
    Returns:
      The creation time for the wrapped object.
    Since:
      2.11.0

  - ### getCreateTime

    @Deprecated long getCreateTime()
    Deprecated.
    Use `getCreateInstant()` which offers the best precision.
    Gets the time (using the same basis as `Clock.instant()`) that this object was created.
    Returns:
      The creation time for the wrapped object.

  - ### getFullDuration

    default Duration getFullDuration()
    Gets the duration since this object was created (using `Instant.now()`).
    Returns:
      The duration since this object was created.
    Since:
      2.12.0

  - ### getIdleDuration

    default Duration getIdleDuration()
    Gets the amount of time that this object last spend in the idle state (it may still be idle in which case subsequent calls will return an increased value).
    Returns:
      The amount of time in last spent in the idle state.
    Since:
      2.11.0

  - ### getIdleTime

    @Deprecated default Duration getIdleTime()
    Deprecated.
    Use `getIdleDuration()`.
    Gets the amount of time that this object last spend in the idle state (it may still be idle in which case subsequent calls will return an increased value).
    Returns:
      The amount of time in last spent in the idle state.
    Since:
      2.10.0

  - ### getIdleTimeMillis

    @Deprecated long getIdleTimeMillis()
    Deprecated.
    Use `getIdleTime()` which offers the best precision.
    Gets the amount of time in milliseconds that this object last spend in the idle state (it may still be idle in which case subsequent calls will return an increased value).
    Returns:
      The time in milliseconds last spent in the idle state.

  - ### getLastBorrowInstant

    default Instant getLastBorrowInstant()
    Gets the time the wrapped object was last borrowed.
    Returns:
      The time the object was last borrowed.
    Since:
      2.11.0

  - ### getLastBorrowTime

    @Deprecated long getLastBorrowTime()
    Deprecated.
    Use `getLastBorrowInstant()` which offers the best precision.
    Gets the time the wrapped object was last borrowed.
    Returns:
      The time the object was last borrowed.

  - ### getLastReturnInstant

    default Instant getLastReturnInstant()
    Gets the time the wrapped object was last borrowed.
    Returns:
      The time the object was last borrowed.
    Since:
      2.11.0

  - ### getLastReturnTime

    @Deprecated long getLastReturnTime()
    Deprecated.
    Use `getLastReturnInstant()` which offers the best precision.
    Gets the time the wrapped object was last returned.
    Returns:
      The time the object was last returned.

  - ### getLastUsedInstant

    default Instant getLastUsedInstant()
    Gets an estimate of the last time this object was used. If the class of the pooled object implements `TrackedUse`, what is returned is the maximum of `TrackedUse.getLastUsedInstant()` and `getLastBorrowTime()`; otherwise this method gives the same value as `getLastBorrowTime()`.
    Returns:
      the last time this object was used
    Since:
      2.11.0

  - ### getLastUsedTime

    @Deprecated long getLastUsedTime()
    Deprecated.
    Use `getLastUsedInstant()` which offers the best precision.
    Gets an estimate of the last time this object was used. If the class of the pooled object implements `TrackedUse`, what is returned is the maximum of `TrackedUse.getLastUsedInstant()` and `getLastBorrowTime()`; otherwise this method gives the same value as `getLastBorrowTime()`.
    Returns:
      the last time this object was used.

  - ### getObject

    T getObject()
    Gets the underlying object that is wrapped by this instance of `PooledObject`.
    Returns:
      The wrapped object.

  - ### getState

    PooledObjectState getState()
    Gets the state of this object.
    Returns:
      state

  - ### hashCode

    int hashCode()
    Overrides:
      `hashCode` in class `Object`

  - ### invalidate

    void invalidate()
    Sets the state to `INVALID`.

  - ### markAbandoned

    void markAbandoned()
    Marks the pooled object as abandoned.

  - ### markReturning

    void markReturning()
    Marks the object as returning to the pool.

  - ### printStackTrace

    void printStackTrace(PrintWriter writer)
    Prints the stack trace of the code that borrowed this pooled object and the stack trace of the last code to use this object (if available) to the supplied writer.
    Parameters:
      `writer` - The destination for the debug output.

  - ### setLogAbandoned

    void setLogAbandoned(boolean logAbandoned)
    Sets whether to use abandoned object tracking. If this is true the implementation will need to record the stack trace of the last caller to borrow this object.
    Parameters:
      `logAbandoned` - The new configuration setting for abandoned object tracking.

  - ### setRequireFullStackTrace

    default void setRequireFullStackTrace(boolean requireFullStackTrace)
    Sets the stack trace generation strategy based on whether or not fully detailed stack traces are required. When set to false, abandoned logs may only include caller class information rather than method names, line numbers, and other normal metadata available in a full stack trace.
    Parameters:
      `requireFullStackTrace` - the new configuration setting for abandoned object logging.
    Since:
      2.7.0

  - ### startEvictionTest

    boolean startEvictionTest()
    Attempts to place the pooled object in the `PooledObjectState.EVICTION` state.
    Returns:
      `true` if the object was placed in the `PooledObjectState.EVICTION` state otherwise `false`.

  - ### toString

    String toString()
    Gets a String form of the wrapper for debug purposes. The format is not fixed and may change at any time.
    Overrides:
      `toString` in class `Object`

  - ### use

    void use()
    Records the current stack trace as the last time the object was used.