Class RProvider<I>

java.lang.Object
de.team33.patterns.pooling.ariel.RProvider<I>
Type Parameters:
I - The type of provided instances (items).

public class RProvider<I> extends Object
A tool that makes instances of a certain type available for the course of operations that require such an instance. The instances provided are referred to as item in the following.

The tool maintains a number of such items and only creates as many as are actually required at most at the same time in its application context. The items are retained and reused for subsequent operations.

In this respect, this tool is suitable for providing item types whose instantiation is relatively "expensive", which are rather unsuitable for concurrent access, but are designed for multiple or permanent use. Database or other client-server connections may be an example.

Note: this implementation cannot detect when an internal operation is taking place in the course of an operation to which the same item could be made available.

This implementation supports expiry and reinitialisation of provided items.

This implementation does not support checked exceptions to occur while creating new item instances.

See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    RProvider(Supplier<? extends I> newItem, long maxIdle, long maxLiving)
    Initializes a new instance giving a Supplier that defines the intended initialization of a new item.
  • Method Summary

    Modifier and Type
    Method
    Description
    final <R> R
    get(Function<? super I,R> function)
    Calls a given Function with a parameter provided for it and returns its result.
    final <R, X extends Exception>
    R
    getEx(XFunction<? super I,R,X> xFunction)
    Calls a given XFunction with a parameter provided for it and returns its result.
    final void
    run(Consumer<? super I> consumer)
    Runs a given Consumer with a parameter provided for it.
    final <X extends Exception>
    void
    runEx(XConsumer<? super I,X> xConsumer)
    Runs a given XConsumer with a parameter provided for it.
    final int
    Returns the number of currently unused subjects in stock.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • RProvider

      public RProvider(Supplier<? extends I> newItem, long maxIdle, long maxLiving)
      Initializes a new instance giving a Supplier that defines the intended initialization of a new item.

      Once an instance item is initialized it will expire and be renewed after a maximum idle time or at least after a maximum lifetime.

  • Method Details

    • run

      public final void run(Consumer<? super I> consumer)
      Runs a given Consumer with a parameter provided for it. The parameter is kept for future use.

      While the Consumer is being executed, the parameter is exclusively available to it, but must not be "hijacked" from the context of the execution or the executing thread!

    • runEx

      public final <X extends Exception> void runEx(XConsumer<? super I,X> xConsumer) throws X
      Runs a given XConsumer with a parameter provided for it. The parameter is kept for future use.

      While the XConsumer is being executed, the parameter is exclusively available to it, but must not be "hijacked" from the context of the execution or the executing thread!

      Type Parameters:
      X - A type of exception that may be caused by the given XConsumer.
      Throws:
      X - if the execution of the given XConsumer causes one.
    • get

      public final <R> R get(Function<? super I,R> function)
      Calls a given Function with a parameter provided for it and returns its result. The parameter is kept for future use.

      While the Function is being called, the parameter is exclusively available to it, but must not be "hijacked" from the context of the call or the executing thread!

      Type Parameters:
      R - The result type of the given Function
    • getEx

      public final <R, X extends Exception> R getEx(XFunction<? super I,R,X> xFunction) throws X
      Calls a given XFunction with a parameter provided for it and returns its result. The parameter is kept for future use.

      While the XFunction is being called, the parameter is exclusively available to it, but must not be "hijacked" from the context of the call or the executing thread!

      Type Parameters:
      R - The result type of the given XFunction
      X - A type of exception that may be caused by the given XFunction.
      Throws:
      X - if the execution of the given XFunction causes one.
    • size

      public final int size()
      Returns the number of currently unused subjects in stock.