Class SupplierChain<I,O>

java.lang.Object
overit.geocall.util.SupplierChain<I,O>
Type Parameters:
I - type of the input parameter
O - type of the output results
Direct Known Subclasses:
VerificationChain

public class SupplierChain<I,O> extends Object
Utility class used to create a chain of handlers that will be responsible to handle e request and provide a result. Following, there is an usages example

 SupplierChain.Handler<String, Boolean> functionValidator = new SupplierChain.Handler<>() {
      {@literal @}Override
      public Boolean get(String function) {
          return Identity.current.hasFunction(function);
      }
 };
 SupplierChain.Handler<String, Boolean> prefixValidator = new SupplierChain.Handler<>() {
      {@literal @}Override
      public Boolean get(String function) {
          if (function.startsWith("geocall")) return true;
          else super.get(function);
      }
 };

 SupplierChain<String, Boolean> supplierChain = new SupplierChain<>();
 supplierChain.add(functionValidator);
 supplierChain.add(prefixValidator);

 supplierChain.get("server.administrator"); // true (if the current identity is a system admin)
 supplierChain.get("geocall.basemodulo"); // true
 supplierChain.get("unknown"); // null
 
Warning: the order the handler are added is important. The first added is the first in the handler chain
  • Constructor Details

    • SupplierChain

      public SupplierChain()
  • Method Details

    • add

      public void add(@NotNull @NotNull SupplierChain.Handler<I,O> handler)
      Add a new handler at the end of the handler's chain. If there's no added handler before, this will be the first and only invoked. If there are other handlers added before, the current will be added as last handler in the chain.
      Parameters:
      handler - instance of the handler that will be added to the chain
      Throws:
      overit.geocall.asserts.AssertsException - if passed a null handler
    • get

      public O get(I input)
      Get the value from the first handler in the chain that is able to handle the input parameter.
      Parameters:
      input - the input parameter
      Returns:
      the output value or null if there's no registered handler or if there's no handler that can handle the passed input.
    • stream

      public Stream<O> stream()
      Get the stream containing all values that registered handler can manage. For example, if there's a handler that manages the translations contained in a dictionary, this method should return a stream for all dictionary entries.
      Returns:
      the stream containing all the output values, or Stream.empty() if there's none.