Package com.guinetik.corefun
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
- Immutable transformations - All operations return new instances
- Fluent API - Chain operations for readable data pipelines
- Validation - Check values with predicates using
isValid(java.util.function.Predicate<T>)andfilter(java.util.function.Predicate<T>, java.util.function.Supplier<T>) - Combination - Combine multiple values with
combine(com.guinetik.corefun.Computable<U>, java.util.function.BiFunction<T, U, R>) - Side effects - Debug with
peek(java.util.function.Consumer<T>)without breaking the chain
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 TypeMethodDescriptiondefault <U,R> Computable<R> combine(Computable<U> other, BiFunction<T, U, R> combiner) Combines this value with another Computable's value.default Computable<T>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.getValue()Retrieves the value managed by this computable.default booleanValidates the value using the specified predicate.default <R> Computable<R>Transforms the value using the provided function.static <T> Computable<T>of(T value) Creates a new Computable wrapping the given value.default TReturns the value if non-null, otherwise returns the default.default Computable<T>Performs an action on the value without altering it.default <R> Rreduce(R initialValue, BiFunction<R, T, R> accumulator) Reduces the value by combining it with an initial value.
-
Method Details
-
of
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
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
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
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
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 testdefaultValue- supplier for the default value if test fails- Returns:
- this Computable if predicate passes, otherwise a new one with default
-
reduce
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 reductionaccumulator- the reduction function- Returns:
- the result of the reduction
-
combine
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 valueR- the type of the result- Parameters:
other- another Computablecombiner- function to combine the values- Returns:
- a new Computable with the combined result
-
peek
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
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
-