Class Functional<V>

  • Type Parameters:
    V - The type of the value being operated on

    public class Functional<V>
    extends java.lang.Object
    A fluent API utility class for performing chained operations on a value of type V.

    This class allows for conditional processing (on(Predicate)), value transformation (as(Function)), and side-effect execution (apply(Consumer)) in a fluent and readable manner.

    The state of the object is not thread-safe due to the mutable internal state tracking (e.g., matched). It's designed for single-threaded use or must be externally synchronized when used concurrently.

    Example Usage

    
     // Example 1: Conditional mapping
     String result = Functional.value("Hello")
         .on(s -> s.length() > 3)
         .as(String::toUpperCase)
         .value();
    
     // Example 2: Conditional side effect
     Functional.value(42)
         .on(n -> n > 40)
         .apply(n -> System.out.println("Value is: " + n));
    
     // Example 3: Named functional operation
     Functional.of("user", user)
         .on(u -> u.isActive())
         .as(User::getName)
         .apply(name -> System.out.println("Active user: " + name));
     
    Since:
    1.0.0
    Author:
    Mercy
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Functional​(java.lang.String name, V value)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void apply​(java.util.function.Consumer<V> valueConsumer)  
      <R> Functional<R> as​(java.util.function.Function<V,​R> function)  
      static <V> Functional<V> of​(java.lang.String name, java.util.function.Supplier<V> valueSupplier)  
      static <V> Functional<V> of​(java.lang.String name, V value)  
      Functional<V> on​(java.util.function.Predicate<? super V> predicate)  
      java.lang.String toString()  
      static <V> Functional<V> value​(java.util.function.Supplier<V> valueSupplier)  
      static <V> Functional<V> value​(V value)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • Functional

        protected Functional​(java.lang.String name,
                             V value)
    • Method Detail

      • on

        public Functional<V> on​(java.util.function.Predicate<? super V> predicate)
      • as

        public <R> Functional<R> as​(java.util.function.Function<V,​R> function)
      • apply

        public void apply​(java.util.function.Consumer<V> valueConsumer)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • value

        public static <V> Functional<V> value​(java.util.function.Supplier<V> valueSupplier)
      • value

        public static <V> Functional<V> value​(V value)
      • of

        public static <V> Functional<V> of​(java.lang.String name,
                                           java.util.function.Supplier<V> valueSupplier)
      • of

        public static <V> Functional<V> of​(java.lang.String name,
                                           V value)