File tree Expand file tree Collapse file tree 1 file changed +13
-5
lines changed
server-common/src/main/java/io/a2a/server/tasks Expand file tree Collapse file tree 1 file changed +13
-5
lines changed Original file line number Diff line number Diff line change 11package io .a2a .server .tasks ;
22
33import java .util .ArrayList ;
4+ import java .util .Collections ;
45import java .util .Comparator ;
56import java .util .List ;
67import 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
You can’t perform that action at this time.
0 commit comments