Skip to content

Commit 9e70311

Browse files
committed
refactor(Edge.java): replace Collection with StateGraph.Nodes for improved performance and readability
- Updated `validate` methods to use `StateGraph.Nodes.anyMatchById` instead of manual containment checks within a collection, enhancing both performance and code clarity. work on #73
1 parent 03abb16 commit 9e70311

File tree

1 file changed

+5
-5
lines changed
  • core/src/main/java/org/bsc/langgraph4j

1 file changed

+5
-5
lines changed

core/src/main/java/org/bsc/langgraph4j/Edge.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ public Edge<State> withSourceAndTargetIdsUpdated(Node<State> node,
5858

5959
}
6060

61-
public void validate( @NonNull Collection<Node<State>> nodes) throws GraphStateException {
62-
if ( !Objects.equals(sourceId(),START) && !nodes.contains(new Node<State>(sourceId()))) {
61+
public void validate( @NonNull StateGraph.Nodes<State> nodes ) throws GraphStateException {
62+
if ( !Objects.equals(sourceId(),START) && !nodes.anyMatchById(sourceId())) {
6363
throw StateGraph.Errors.missingNodeReferencedByEdge.exception(sourceId());
6464
}
6565

@@ -82,14 +82,14 @@ public void validate( @NonNull Collection<Node<State>> nodes) throws GraphStateE
8282

8383
}
8484

85-
private void validate( EdgeValue<State> target, Collection<Node<State>> nodes ) throws GraphStateException {
85+
private void validate( EdgeValue<State> target, StateGraph.Nodes<State> nodes ) throws GraphStateException {
8686
if (target.id() != null) {
87-
if (!Objects.equals(target.id(), StateGraph.END) && !nodes.contains(new Node<State>(target.id()))) {
87+
if (!Objects.equals(target.id(), StateGraph.END) && !nodes.anyMatchById(target.id())) {
8888
throw StateGraph.Errors.missingNodeReferencedByEdge.exception(target.id());
8989
}
9090
} else if (target.value() != null) {
9191
for (String nodeId : target.value().mappings().values()) {
92-
if (!Objects.equals(nodeId, StateGraph.END) && !nodes.contains(new Node<State>(nodeId))) {
92+
if (!Objects.equals(nodeId, StateGraph.END) && !nodes.anyMatchById(nodeId)) {
9393
throw StateGraph.Errors.missingNodeInEdgeMapping.exception(sourceId(), nodeId);
9494
}
9595
}

0 commit comments

Comments
 (0)