Skip to content

Commit db05ad0

Browse files
committed
More review fixes
1 parent e7a8e58 commit db05ad0

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

server-common/src/main/java/io/a2a/server/tasks/InMemoryTaskStore.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.a2a.server.tasks;
22

33
import java.util.ArrayList;
4+
import java.util.Collections;
45
import java.util.Comparator;
56
import java.util.List;
67
import java.util.concurrent.ConcurrentHashMap;
@@ -64,12 +65,19 @@ public ListTasksResult list(ListTasksParams params) {
6465

6566
// Handle page token (simple cursor: last task ID from previous page)
6667
if (params.pageToken() != null && !params.pageToken().isEmpty()) {
67-
for (int i = 0; i < allFilteredTasks.size(); i++) {
68-
if (allFilteredTasks.get(i).getId().equals(params.pageToken())) {
69-
startIndex = i + 1;
70-
break;
71-
}
68+
// Use binary search since list is sorted by task ID (O(log N) vs O(N))
69+
int index = Collections.binarySearch(allFilteredTasks, null,
70+
(t1, t2) -> {
71+
// Handle null key comparisons (binarySearch passes null as one argument)
72+
if (t1 == null && t2 == null) return 0;
73+
if (t1 == null) return params.pageToken().compareTo(t2.getId());
74+
if (t2 == null) return t1.getId().compareTo(params.pageToken());
75+
return t1.getId().compareTo(t2.getId());
76+
});
77+
if (index >= 0) {
78+
startIndex = index + 1;
7279
}
80+
// If not found (index < 0), startIndex remains 0 (start from beginning)
7381
}
7482

7583
// Get the page of tasks

0 commit comments

Comments
 (0)