1
1
/*
2
- * Copyright (c) 1998, 2022 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 1998, 2023 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
@@ -119,7 +119,7 @@ public IndexBuilder(BaseConfiguration configuration,
119
119
itemsByFirstChar = new TreeMap <>();
120
120
itemsByCategory = new EnumMap <>(IndexItem .Category .class );
121
121
122
- mainComparator = makeIndexComparator (classesOnly );
122
+ mainComparator = classesOnly ? makeClassComparator () : makeIndexComparator ();
123
123
}
124
124
125
125
/**
@@ -310,6 +310,13 @@ private static Character keyCharacter(String s) {
310
310
return '*' ;
311
311
}
312
312
313
+ /**
314
+ * Returns a comparator for the all-classes list.
315
+ * @return a comparator for class element items
316
+ */
317
+ private Comparator <IndexItem > makeClassComparator () {
318
+ return Comparator .comparing (IndexItem ::getElement , utils .comparators .makeAllClassesComparator ());
319
+ }
313
320
314
321
/**
315
322
* Returns a comparator for the {@code IndexItem}s in the index page.
@@ -318,15 +325,17 @@ private static Character keyCharacter(String s) {
318
325
*
319
326
* @return a comparator for index page items
320
327
*/
321
- private Comparator <IndexItem > makeIndexComparator (boolean classesOnly ) {
322
- Comparator <Element > elementComparator = classesOnly
323
- ? utils .comparators .makeAllClassesComparator ()
324
- : utils .comparators .makeIndexElementComparator ();
325
-
326
- Comparator <IndexItem > labelComparator =
327
- (ii1 , ii2 ) -> utils .compareStrings (ii1 .getLabel (), ii2 .getLabel ());
328
+ private Comparator <IndexItem > makeIndexComparator () {
329
+ // We create comparators specific to element and search tag items, and a
330
+ // base comparator used to compare between the two kinds of items.
331
+ // In order to produce consistent results, it is important that the base comparator
332
+ // uses the same primary sort keys as both the element and search tag comparators
333
+ // (see JDK-8311264).
334
+ Comparator <Element > elementComparator = utils .comparators .makeIndexElementComparator ();
335
+ Comparator <IndexItem > baseComparator =
336
+ (ii1 , ii2 ) -> utils .compareStrings (getIndexItemKey (ii1 ), getIndexItemKey (ii2 ));
328
337
Comparator <IndexItem > searchTagComparator =
329
- labelComparator
338
+ baseComparator
330
339
.thenComparing (IndexItem ::getHolder )
331
340
.thenComparing (IndexItem ::getDescription )
332
341
.thenComparing (IndexItem ::getUrl );
@@ -350,9 +359,9 @@ private Comparator<IndexItem> makeIndexComparator(boolean classesOnly) {
350
359
return d ;
351
360
}
352
361
353
- // If one is an element item, compare labels ; if equal, put element item last
362
+ // If one is an element item, compare item keys ; if equal, put element item last
354
363
if (ii1 .isElementItem () || ii2 .isElementItem ()) {
355
- int d = labelComparator .compare (ii1 , ii2 );
364
+ int d = baseComparator .compare (ii1 , ii2 );
356
365
return d != 0 ? d : ii1 .isElementItem () ? 1 : -1 ;
357
366
}
358
367
@@ -361,6 +370,14 @@ private Comparator<IndexItem> makeIndexComparator(boolean classesOnly) {
361
370
};
362
371
}
363
372
373
+ private String getIndexItemKey (IndexItem ii ) {
374
+ // For element items return the key used by the element comparator;
375
+ // for search tag items return the item's label.
376
+ return ii .isElementItem ()
377
+ ? utils .comparators .getIndexElementKey (ii .getElement ())
378
+ : ii .getLabel ();
379
+ }
380
+
364
381
/**
365
382
* Returns a Comparator for IndexItems in the types category of the search index.
366
383
* Items are compared by short name, falling back to the main comparator if names are equal.
0 commit comments