Interface Computable<T>

Type Parameters:
T - the type of the value being managed
All Known Implementing Classes:
DefaultComputable

public interface Computable<T>
A value wrapper that provides functional composition operations.

Computable encapsulates a value and provides methods for transforming, validating, and combining values in a functional style. It promotes immutability and composability, making it useful for data processing pipelines and building domain-specific languages (DSLs).

Key Features

Example Usage


 // Simple transformation
 Computable<String> name = Computable.of("hello");
 Computable<Integer> length = name.map(String::length);
 System.out.println(length.getValue()); // prints 5

 // Chained transformations
 String result = Computable.of(100)
     .map(n -> n * 2)
     .filter(n -> n > 0, () -> 0)
     .map(n -> "Result: " + n)
     .getValue();

 // Combining values
 Computable<Integer> a = Computable.of(10);
 Computable<Integer> b = Computable.of(5);
 int sum = a.combine(b, Integer::sum).getValue(); // 15

 // Validation
 boolean valid = Computable.of(email)
     .isValid(e -> e.contains("@"));
 
Since:
0.1.0
Author:
Guinetik <guinetik@gmail.com>
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    default <U, R> Computable<R>
    combine(Computable<U> other, BiFunction<T,U,R> combiner)
    Combines this value with another Computable's value.
    default Computable<T>
    filter(Predicate<T> predicate, Supplier<T> defaultValue)
    Filters the value, returning a default if the predicate fails.
    default <R> Computable<R>
    flatMap(Function<? super T,Computable<R>> transformer)
    Applies a transformation that returns a Computable and flattens the result.
    Retrieves the value managed by this computable.
    default boolean
    isValid(Predicate<T> validator)
    Validates the value using the specified predicate.
    default <R> Computable<R>
    map(Function<? super T,? extends R> transformer)
    Transforms the value using the provided function.
    static <T> Computable<T>
    of(T value)
    Creates a new Computable wrapping the given value.
    default T
    orElse(T other)
    Returns the value if non-null, otherwise returns the default.
    default Computable<T>
    peek(Consumer<T> action)
    Performs an action on the value without altering it.
    default <R> R
    reduce(R initialValue, BiFunction<R,T,R> accumulator)
    Reduces the value by combining it with an initial value.
  • Method Details

    • of

      static <T> Computable<T> of(T value)
      Creates a new Computable wrapping the given value.
      Type Parameters:
      T - the type of the value
      Parameters:
      value - the value to wrap
      Returns:
      a new Computable containing the value
    • getValue

      T getValue()
      Retrieves the value managed by this computable.
      Returns:
      the current value
    • map

      default <R> Computable<R> map(Function<? super T,? extends R> transformer)
      Transforms the value using the provided function.

      Example:

      
       Computable<String> original = Computable.of("hello");
       Computable<Integer> length = original.map(String::length);
       
      Type Parameters:
      R - the type of the result
      Parameters:
      transformer - a function to transform the value
      Returns:
      a new Computable with the transformed value
    • flatMap

      default <R> Computable<R> flatMap(Function<? super T,Computable<R>> transformer)
      Applies a transformation that returns a Computable and flattens the result.

      Example:

      
       Computable<Integer> age = Computable.of(30);
       Computable<String> status = age.flatMap(a -> Computable.of(a >= 18 ? "Adult" : "Minor"));
       
      Type Parameters:
      R - the type of the result
      Parameters:
      transformer - a function that returns a Computable
      Returns:
      the Computable returned by the transformer
    • isValid

      default boolean isValid(Predicate<T> validator)
      Validates the value using the specified predicate.

      Example:

      
       Computable<Integer> age = Computable.of(25);
       boolean isAdult = age.isValid(a -> a >= 18);
       
      Parameters:
      validator - a predicate to test the value
      Returns:
      true if the value passes validation
    • filter

      default Computable<T> filter(Predicate<T> predicate, Supplier<T> defaultValue)
      Filters the value, returning a default if the predicate fails.

      Example:

      
       Computable<Integer> age = Computable.of(15);
       Computable<Integer> adultAge = age.filter(a -> a >= 18, () -> 18);
       
      Parameters:
      predicate - the condition to test
      defaultValue - supplier for the default value if test fails
      Returns:
      this Computable if predicate passes, otherwise a new one with default
    • reduce

      default <R> R reduce(R initialValue, BiFunction<R,T,R> accumulator)
      Reduces the value by combining it with an initial value.

      Example:

      
       Computable<String> name = Computable.of("John");
       String greeting = name.reduce("Hello, ", (acc, n) -> acc + n);
       
      Type Parameters:
      R - the type of the result
      Parameters:
      initialValue - the initial value for reduction
      accumulator - the reduction function
      Returns:
      the result of the reduction
    • combine

      default <U, R> Computable<R> combine(Computable<U> other, BiFunction<T,U,R> combiner)
      Combines this value with another Computable's value.

      Example:

      
       Computable<Integer> a = Computable.of(10);
       Computable<Integer> b = Computable.of(5);
       Computable<Integer> sum = a.combine(b, Integer::sum);
       
      Type Parameters:
      U - the type of the other value
      R - the type of the result
      Parameters:
      other - another Computable
      combiner - function to combine the values
      Returns:
      a new Computable with the combined result
    • peek

      default Computable<T> peek(Consumer<T> action)
      Performs an action on the value without altering it.

      Example:

      
       Computable<String> name = Computable.of("Alice");
       name.peek(System.out::println).map(String::toUpperCase);
       
      Parameters:
      action - a consumer to perform on the value
      Returns:
      this Computable for chaining
    • orElse

      default T orElse(T other)
      Returns the value if non-null, otherwise returns the default.

      Example:

      
       Computable<String> nullable = Computable.of(null);
       String name = nullable.orElse("Unknown");
       
      Parameters:
      other - the default value
      Returns:
      the value or the default