@@ -69,30 +69,18 @@ private static int compareRanges(Range a, Range b) {
69
69
return Integer .compare (aStart .getCharacter (), bStart .getCharacter ());
70
70
}
71
71
72
- private @ Nullable T contains (@ Nullable Entry <Range , T > entry , Range from ) {
73
- if (entry != null ) {
74
- Range match = entry .getKey ();
75
- if (Ranges .containsRange (match , from )) {
76
- return entry .getValue ();
77
- }
78
- }
79
- return null ;
80
- }
81
-
82
72
@ Override
83
73
public @ Nullable T lookup (Range from ) {
84
74
// since we allow for overlapping ranges, it might be that we have to
85
75
// search all the way to the "bottom" of the tree to see if we are
86
76
// contained in something larger than the closest key
87
- var previousKeys = data .headMap (from , true ).descendingMap ();
88
- for (var candidate : previousKeys .entrySet ()) {
89
- T result = contains (candidate , from );
90
- if (result != null ) {
91
- return result ;
92
- }
93
- }
94
- // could be that it's at the start of the entry (so the entry it not in the head map)
95
- return contains (data .ceilingEntry (from ), from );
77
+ // if we could come up with a *valid* ordering such that `data.floorKey(from)` is always
78
+ // the smallest key containing `from` (or another key when none contain `from`), we could use `data.floorEntry` here instead of iterating
79
+ return data .headMap (from , true ).descendingMap ().entrySet ()
80
+ .stream ()
81
+ .filter (e -> Ranges .containsRange (e .getKey (), from ))
82
+ .map (Entry ::getValue )
83
+ .findFirst ().orElse (null );
96
84
}
97
85
98
86
@ Override
0 commit comments