Class Delegates

java.lang.Object
dev.demeng.pluginbase.delegate.Delegates

public final class Delegates extends Object
A collection of utility methods for delegating Java 8 functions.
  • Constructor Details

    • Delegates

      public Delegates()
  • Method Details

    • runnableToConsumer

      public static <T> Consumer<T> runnableToConsumer(Runnable runnable)
      Wraps a Runnable as a Consumer. The consumer ignores its argument and simply runs the delegate.
      Type Parameters:
      T - the type of the consumer's argument
      Parameters:
      runnable - the runnable to wrap
      Returns:
      a consumer that delegates to the given runnable
    • runnableToSupplier

      public static Supplier<Void> runnableToSupplier(Runnable runnable)
      Wraps a Runnable as a Supplier<Void>. Runs the delegate and returns null.
      Parameters:
      runnable - the runnable to wrap
      Returns:
      a supplier that runs the delegate and always returns null
    • callableToSupplier

      public static <T> Supplier<T> callableToSupplier(Callable<T> callable)
      Wraps a Callable as a Supplier. Any checked exception thrown by the callable is rethrown wrapped in a BaseException; unchecked exceptions propagate as-is.
      Type Parameters:
      T - the type of the supplied value
      Parameters:
      callable - the callable to wrap
      Returns:
      a supplier that delegates to the given callable
      Throws:
      BaseException - if the callable throws a checked exception
    • consumerToBiConsumerFirst

      public static <T, U> BiConsumer<T,U> consumerToBiConsumerFirst(Consumer<T> consumer)
      Wraps a Consumer as a BiConsumer that applies the consumer to the first argument, ignoring the second.
      Type Parameters:
      T - the type of the first argument
      U - the type of the second argument (ignored)
      Parameters:
      consumer - the consumer to wrap
      Returns:
      a bi-consumer that delegates to the given consumer using the first argument
    • consumerToBiConsumerSecond

      public static <T, U> BiConsumer<T,U> consumerToBiConsumerSecond(Consumer<U> consumer)
      Wraps a Consumer as a BiConsumer that applies the consumer to the second argument, ignoring the first.
      Type Parameters:
      T - the type of the first argument (ignored)
      U - the type of the second argument
      Parameters:
      consumer - the consumer to wrap
      Returns:
      a bi-consumer that delegates to the given consumer using the second argument
    • predicateToBiPredicateFirst

      public static <T, U> BiPredicate<T,U> predicateToBiPredicateFirst(Predicate<T> predicate)
      Wraps a Predicate as a BiPredicate that tests the first argument, ignoring the second.
      Type Parameters:
      T - the type of the first argument
      U - the type of the second argument (ignored)
      Parameters:
      predicate - the predicate to wrap
      Returns:
      a bi-predicate that delegates to the given predicate using the first argument
    • predicateToBiPredicateSecond

      public static <T, U> BiPredicate<T,U> predicateToBiPredicateSecond(Predicate<U> predicate)
      Wraps a Predicate as a BiPredicate that tests the second argument, ignoring the first.
      Type Parameters:
      T - the type of the first argument (ignored)
      U - the type of the second argument
      Parameters:
      predicate - the predicate to wrap
      Returns:
      a bi-predicate that delegates to the given predicate using the second argument
    • consumerToFunction

      public static <T, U> Function<T,U> consumerToFunction(Consumer<T> consumer)
      Wraps a Consumer as a Function. Applies the consumer to the argument and returns null.
      Type Parameters:
      T - the type of the function's argument
      U - the declared return type of the function (always null at runtime)
      Parameters:
      consumer - the consumer to wrap
      Returns:
      a function that delegates to the given consumer and always returns null
    • runnableToFunction

      public static <T, U> Function<T,U> runnableToFunction(Runnable runnable)
      Wraps a Runnable as a Function. Runs the delegate, ignores the argument, and returns null.
      Type Parameters:
      T - the type of the function's argument (ignored)
      U - the declared return type of the function (always null at runtime)
      Parameters:
      runnable - the runnable to wrap
      Returns:
      a function that delegates to the given runnable and always returns null