Skip to content

Commit

Permalink
feat: add factory methods to create custom Channel
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Aug 29, 2024
1 parent e37621c commit 8abb173
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions core-jdk8/src/main/java/org/bsc/langgraph4j/state/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,30 @@
import java.util.Optional;
import java.util.function.Supplier;

import static java.util.Optional.ofNullable;

/**
*
* @param <T>
*/
class BaseChannel<T> implements Channel<T> {
final Supplier<T> defaultProvider;
final Reducer<T> reducer;

BaseChannel(Reducer<T> reducer, Supplier<T> defaultProvider ) {
this.defaultProvider = defaultProvider;
this.reducer = reducer;
}

public Optional<Supplier<T>> getDefault() {
return ofNullable(defaultProvider);
}

public Optional<Reducer<T>> getReducer() {
return ofNullable(reducer);
}
}

/**
* A Channel is a mechanism used to maintain a state property.
* <p>
Expand All @@ -28,6 +52,16 @@
*/
public interface Channel<T> {

static <T> Channel<T> of( Supplier<T> defaultProvider) {
return new BaseChannel<T>(null, defaultProvider);
}
static <T> Channel<T> of( Reducer<T> reducer ) {
return new BaseChannel<T>(reducer, null);
}
static <T> Channel<T> of( Reducer<T> reducer, Supplier<T> defaultProvider ) {
return new BaseChannel<T>(reducer, defaultProvider);
}

/**
* The Reducer, if provided, is invoked for each state property to compute value.
*
Expand Down

0 comments on commit 8abb173

Please sign in to comment.