Skip to content

Commit

Permalink
refactor(Edge.java): replace Collection with StateGraph.Nodes for imp…
Browse files Browse the repository at this point in the history
…roved 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
  • Loading branch information
bsorrentino committed Feb 11, 2025
1 parent d2c4fd5 commit 4f2f10d
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions core/src/main/java/org/bsc/langgraph4j/Edge.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public Edge<State> withSourceAndTargetIdsUpdated(Node<State> node,

}

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

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

}

private void validate( EdgeValue<State> target, Collection<Node<State>> nodes ) throws GraphStateException {
private void validate( EdgeValue<State> target, StateGraph.Nodes<State> nodes ) throws GraphStateException {
if (target.id() != null) {
if (!Objects.equals(target.id(), StateGraph.END) && !nodes.contains(new Node<State>(target.id()))) {
if (!Objects.equals(target.id(), StateGraph.END) && !nodes.anyMatchById(target.id())) {
throw StateGraph.Errors.missingNodeReferencedByEdge.exception(target.id());
}
} else if (target.value() != null) {
for (String nodeId : target.value().mappings().values()) {
if (!Objects.equals(nodeId, StateGraph.END) && !nodes.contains(new Node<State>(nodeId))) {
if (!Objects.equals(nodeId, StateGraph.END) && !nodes.anyMatchById(nodeId)) {
throw StateGraph.Errors.missingNodeInEdgeMapping.exception(sourceId(), nodeId);
}
}
Expand Down

0 comments on commit 4f2f10d

Please sign in to comment.