From 8abb17312f788fb2b510145e22d35f31c2a96c01 Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Thu, 29 Aug 2024 21:55:00 +0200 Subject: [PATCH] feat: add factory methods to create custom Channel --- .../org/bsc/langgraph4j/state/Channel.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/core-jdk8/src/main/java/org/bsc/langgraph4j/state/Channel.java b/core-jdk8/src/main/java/org/bsc/langgraph4j/state/Channel.java index dce7ac5..404242d 100644 --- a/core-jdk8/src/main/java/org/bsc/langgraph4j/state/Channel.java +++ b/core-jdk8/src/main/java/org/bsc/langgraph4j/state/Channel.java @@ -3,6 +3,30 @@ import java.util.Optional; import java.util.function.Supplier; +import static java.util.Optional.ofNullable; + +/** + * + * @param + */ +class BaseChannel implements Channel { + final Supplier defaultProvider; + final Reducer reducer; + + BaseChannel(Reducer reducer, Supplier defaultProvider ) { + this.defaultProvider = defaultProvider; + this.reducer = reducer; + } + + public Optional> getDefault() { + return ofNullable(defaultProvider); + } + + public Optional> getReducer() { + return ofNullable(reducer); + } +} + /** * A Channel is a mechanism used to maintain a state property. *

@@ -28,6 +52,16 @@ */ public interface Channel { + static Channel of( Supplier defaultProvider) { + return new BaseChannel(null, defaultProvider); + } + static Channel of( Reducer reducer ) { + return new BaseChannel(reducer, null); + } + static Channel of( Reducer reducer, Supplier defaultProvider ) { + return new BaseChannel(reducer, defaultProvider); + } + /** * The Reducer, if provided, is invoked for each state property to compute value. *