Skip to content

Commit e8c32cc

Browse files
author
Pavel Rappo
committedFeb 26, 2020
8239876: Improve SearchIndexItem
Reviewed-by: jjg
1 parent 9b12c80 commit e8c32cc

10 files changed

+170
-70
lines changed
 

‎src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/AbstractIndexWriter.java

+35-11
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@
2727

2828
import java.io.IOException;
2929
import java.io.Writer;
30+
import java.util.ArrayList;
3031
import java.util.Collection;
32+
import java.util.HashMap;
3133
import java.util.List;
34+
import java.util.Map;
3235

3336
import javax.lang.model.element.Element;
3437
import javax.lang.model.element.ExecutableElement;
@@ -74,6 +77,8 @@ public class AbstractIndexWriter extends HtmlDocletWriter {
7477

7578
protected Navigation navBar;
7679

80+
protected final Map<Character, List<SearchIndexItem>> tagSearchIndexMap;
81+
7782
/**
7883
* This constructor will be used by {@link SplitIndexWriter}. Initializes
7984
* path to this file and relative path from this file.
@@ -88,6 +93,9 @@ protected AbstractIndexWriter(HtmlConfiguration configuration,
8893
super(configuration, path);
8994
this.indexBuilder = indexBuilder;
9095
this.navBar = new Navigation(null, configuration, PageMode.INDEX, path);
96+
Collection<SearchIndexItem> items =
97+
searchItems.get(SearchIndexItem.Category.SEARCH_TAGS);
98+
this.tagSearchIndexMap = buildSearchTagIndex(items);
9199
}
92100

93101
/**
@@ -182,29 +190,29 @@ protected void addDescription(Content dl, Element element) {
182190
public Void visitModule(ModuleElement e, Void p) {
183191
if (configuration.showModules) {
184192
addDescription(e, dl, si);
185-
configuration.moduleSearchIndex.add(si);
193+
searchItems.add(si);
186194
}
187195
return null;
188196
}
189197

190198
@Override
191199
public Void visitPackage(PackageElement e, Void p) {
192200
addDescription(e, dl, si);
193-
configuration.packageSearchIndex.add(si);
201+
searchItems.add(si);
194202
return null;
195203
}
196204

197205
@Override
198206
public Void visitType(TypeElement e, Void p) {
199207
addDescription(e, dl, si);
200-
configuration.typeSearchIndex.add(si);
208+
searchItems.add(si);
201209
return null;
202210
}
203211

204212
@Override
205213
protected Void defaultAction(Element e, Void p) {
206214
addDescription(e, dl, si);
207-
configuration.memberSearchIndex.add(si);
215+
searchItems.add(si);
208216
return null;
209217
}
210218

@@ -424,32 +432,32 @@ public String getNameForIndex(String unicode) {
424432
protected void createSearchIndexFiles() throws DocFileIOException {
425433
if (configuration.showModules) {
426434
createSearchIndexFile(DocPaths.MODULE_SEARCH_INDEX_JS,
427-
configuration.moduleSearchIndex,
435+
searchItems.get(SearchIndexItem.Category.MODULES),
428436
"moduleSearchIndex");
429437
}
430438
if (!configuration.packages.isEmpty()) {
431439
SearchIndexItem si = new SearchIndexItem();
432440
si.setCategory(SearchIndexItem.Category.PACKAGES);
433441
si.setLabel(resources.getText("doclet.All_Packages"));
434442
si.setUrl(DocPaths.ALLPACKAGES_INDEX.getPath());
435-
configuration.packageSearchIndex.add(si);
443+
searchItems.add(si);
436444
}
437445
createSearchIndexFile(DocPaths.PACKAGE_SEARCH_INDEX_JS,
438-
configuration.packageSearchIndex,
446+
searchItems.get(SearchIndexItem.Category.PACKAGES),
439447
"packageSearchIndex");
440448
SearchIndexItem si = new SearchIndexItem();
441449
si.setCategory(SearchIndexItem.Category.TYPES);
442450
si.setLabel(resources.getText("doclet.All_Classes"));
443451
si.setUrl(DocPaths.ALLCLASSES_INDEX.getPath());
444-
configuration.typeSearchIndex.add(si);
452+
searchItems.add(si);
445453
createSearchIndexFile(DocPaths.TYPE_SEARCH_INDEX_JS,
446-
configuration.typeSearchIndex,
454+
searchItems.get(SearchIndexItem.Category.TYPES),
447455
"typeSearchIndex");
448456
createSearchIndexFile(DocPaths.MEMBER_SEARCH_INDEX_JS,
449-
configuration.memberSearchIndex,
457+
searchItems.get(SearchIndexItem.Category.MEMBERS),
450458
"memberSearchIndex");
451459
createSearchIndexFile(DocPaths.TAG_SEARCH_INDEX_JS,
452-
configuration.tagSearchIndex,
460+
searchItems.get(SearchIndexItem.Category.SEARCH_TAGS),
453461
"tagSearchIndex");
454462
}
455463

@@ -466,6 +474,8 @@ protected void createSearchIndexFile(DocPath searchIndexJS,
466474
String varName)
467475
throws DocFileIOException
468476
{
477+
// The file needs to be created even if there are no searchIndex items
478+
// File could be written straight-through, without an intermediate StringBuilder
469479
if (!searchIndex.isEmpty()) {
470480
StringBuilder searchVar = new StringBuilder("[");
471481
boolean first = true;
@@ -488,4 +498,18 @@ protected void createSearchIndexFile(DocPath searchIndexJS,
488498
}
489499
}
490500
}
501+
502+
protected static Map<Character, List<SearchIndexItem>> buildSearchTagIndex(
503+
Collection<? extends SearchIndexItem> searchItems)
504+
{
505+
Map<Character, List<SearchIndexItem>> map = new HashMap<>();
506+
for (SearchIndexItem sii : searchItems) {
507+
String tagLabel = sii.getLabel();
508+
Character unicode = (tagLabel.length() == 0)
509+
? '*'
510+
: Character.toUpperCase(tagLabel.charAt(0));
511+
map.computeIfAbsent(unicode, k -> new ArrayList<>()).add(sii);
512+
}
513+
return map;
514+
}
491515
}

‎src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlConfiguration.java

+2-29
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,7 @@ public class HtmlConfiguration extends BaseConfiguration {
9191
*/
9292
public TypeElement currentTypeElement = null; // Set this TypeElement in the ClassWriter.
9393

94-
protected SortedSet<SearchIndexItem> memberSearchIndex;
95-
96-
protected SortedSet<SearchIndexItem> moduleSearchIndex;
97-
98-
protected SortedSet<SearchIndexItem> packageSearchIndex;
99-
100-
protected SortedSet<SearchIndexItem> tagSearchIndex;
101-
102-
protected SortedSet<SearchIndexItem> typeSearchIndex;
103-
104-
protected Map<Character, List<SearchIndexItem>> tagSearchIndexMap = new HashMap<>();
105-
106-
protected Set<Character> tagSearchIndexKeys;
94+
protected SearchIndexItems searchItems;
10795

10896
public final Contents contents;
10997

@@ -349,17 +337,6 @@ public boolean showMessage(Element e, String key) {
349337
return (e == null || workArounds.haveDocLint());
350338
}
351339

352-
protected void buildSearchTagIndex() {
353-
for (SearchIndexItem sii : tagSearchIndex) {
354-
String tagLabel = sii.getLabel();
355-
Character unicode = (tagLabel.length() == 0)
356-
? '*'
357-
: Character.toUpperCase(tagLabel.charAt(0));
358-
tagSearchIndexMap.computeIfAbsent(unicode, k -> new ArrayList<>()).add(sii);
359-
}
360-
tagSearchIndexKeys = tagSearchIndexMap.keySet();
361-
}
362-
363340
@Override
364341
protected boolean finishOptionSettings0() throws DocletException {
365342
if (options.docEncoding() == null) {
@@ -384,10 +361,6 @@ protected boolean finishOptionSettings0() throws DocletException {
384361
@Override
385362
protected void initConfiguration(DocletEnvironment docEnv) {
386363
super.initConfiguration(docEnv);
387-
memberSearchIndex = new TreeSet<>(utils.makeGenericSearchIndexComparator());
388-
moduleSearchIndex = new TreeSet<>(utils.makeGenericSearchIndexComparator());
389-
packageSearchIndex = new TreeSet<>(utils.makeGenericSearchIndexComparator());
390-
tagSearchIndex = new TreeSet<>(utils.makeGenericSearchIndexComparator());
391-
typeSearchIndex = new TreeSet<>(utils.makeTypeSearchIndexComparator());
364+
searchItems = new SearchIndexItems(utils);
392365
}
393366
}

‎src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlDoclet.java

-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ protected void generateOtherFiles(DocletEnvironment docEnv, ClassTree classtree)
169169

170170
if (options.createIndex()) {
171171
IndexBuilder indexBuilder = new IndexBuilder(configuration, nodeprecated);
172-
configuration.buildSearchTagIndex();
173172
if (options.splitIndex()) {
174173
SplitIndexWriter.generate(configuration, indexBuilder);
175174
} else {

‎src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlDocletWriter.java

+3
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ public class HtmlDocletWriter {
153153
*/
154154
public final HtmlConfiguration configuration;
155155

156+
protected final SearchIndexItems searchItems;
157+
156158
protected final HtmlOptions options;
157159

158160
protected final Utils utils;
@@ -210,6 +212,7 @@ public class HtmlDocletWriter {
210212
*/
211213
public HtmlDocletWriter(HtmlConfiguration configuration, DocPath path) {
212214
this.configuration = configuration;
215+
this.searchItems = configuration.searchItems;
213216
this.options = configuration.getOptions();
214217
this.contents = configuration.contents;
215218
this.messages = configuration.messages;

‎src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/SearchIndexItem.java

+4
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ public String getDescription() {
101101
return description;
102102
}
103103

104+
protected Category getCategory() {
105+
return category;
106+
}
107+
104108
public void setSystemProperty(boolean value) {
105109
systemProperty = value;
106110
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package jdk.javadoc.internal.doclets.formats.html;
27+
28+
import jdk.javadoc.internal.doclets.toolkit.util.Utils;
29+
30+
import java.util.Collection;
31+
import java.util.Collections;
32+
import java.util.Comparator;
33+
import java.util.HashMap;
34+
import java.util.Map;
35+
import java.util.Objects;
36+
import java.util.Set;
37+
import java.util.TreeSet;
38+
39+
/**
40+
* A container for organizing {@linkplain SearchIndexItem search items}
41+
* by {@linkplain SearchIndexItem.Category category}.
42+
*
43+
* <p><b>This is NOT part of any supported API.
44+
* If you write code that depends on this, you do so at your own risk.
45+
* This code and its internal interfaces are subject to change or
46+
* deletion without notice.</b>
47+
*/
48+
public final class SearchIndexItems {
49+
50+
private final Map<SearchIndexItem.Category, Set<SearchIndexItem>> items = new HashMap<>();
51+
private final Utils utils;
52+
53+
public SearchIndexItems(Utils utils) {
54+
this.utils = Objects.requireNonNull(utils);
55+
}
56+
57+
/**
58+
* Adds the specified item to this container.
59+
*
60+
* @param item
61+
* the item to add
62+
*/
63+
public void add(SearchIndexItem item) {
64+
Objects.requireNonNull(item);
65+
items.computeIfAbsent(item.getCategory(), this::newSetForCategory)
66+
.add(item);
67+
}
68+
69+
private Set<SearchIndexItem> newSetForCategory(SearchIndexItem.Category category) {
70+
final Comparator<SearchIndexItem> cmp;
71+
if (category == SearchIndexItem.Category.TYPES) {
72+
cmp = utils.makeTypeSearchIndexComparator();
73+
} else {
74+
cmp = utils.makeGenericSearchIndexComparator();
75+
}
76+
return new TreeSet<>(cmp);
77+
}
78+
79+
/**
80+
* Retrieves the items of the specified category from this container.
81+
*
82+
* <p> The returned collection is either empty, if there are no items
83+
* of the specified category, or contains only items {@code i} such that
84+
* {@code i.getCategory().equals(cat)}. In any case, the returned collection
85+
* is unmodifiable.
86+
*
87+
* @param cat
88+
* the category of the items to retrieve
89+
*
90+
* @return a collection of items of the specified category
91+
*/
92+
public Collection<SearchIndexItem> get(SearchIndexItem.Category cat) {
93+
Objects.requireNonNull(cat);
94+
Set<SearchIndexItem> col = items.getOrDefault(cat, Set.of());
95+
return Collections.unmodifiableCollection(col);
96+
}
97+
}

‎src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/SingleIndexWriter.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,16 @@ protected void generateIndexFile() throws DocFileIOException {
102102
HtmlTree divTree = new HtmlTree(HtmlTag.DIV);
103103
divTree.setStyle(HtmlStyle.contentContainer);
104104
elements = new TreeSet<>(indexBuilder.asMap().keySet());
105-
elements.addAll(configuration.tagSearchIndexKeys);
105+
elements.addAll(tagSearchIndexMap.keySet());
106106
addLinksForIndexes(divTree);
107107
for (Character unicode : elements) {
108-
if (configuration.tagSearchIndexMap.get(unicode) == null) {
108+
if (tagSearchIndexMap.get(unicode) == null) {
109109
addContents(unicode, indexBuilder.getMemberList(unicode), divTree);
110110
} else if (indexBuilder.getMemberList(unicode) == null) {
111-
addSearchContents(unicode, configuration.tagSearchIndexMap.get(unicode), divTree);
111+
addSearchContents(unicode, tagSearchIndexMap.get(unicode), divTree);
112112
} else {
113113
addContents(unicode, indexBuilder.getMemberList(unicode),
114-
configuration.tagSearchIndexMap.get(unicode), divTree);
114+
tagSearchIndexMap.get(unicode), divTree);
115115
}
116116
}
117117
addLinksForIndexes(divTree);
@@ -137,22 +137,22 @@ protected void generateIndexFile() throws DocFileIOException {
137137
* @param contentTree the content tree to which the links for indexes will be added
138138
*/
139139
protected void addLinksForIndexes(Content contentTree) {
140-
for (Object ch : elements) {
140+
for (Character ch : elements) {
141141
String unicode = ch.toString();
142142
contentTree.add(
143143
links.createLink(getNameForIndex(unicode),
144-
new StringContent(unicode)));
144+
new StringContent(unicode)));
145145
contentTree.add(Entity.NO_BREAK_SPACE);
146146
}
147147
contentTree.add(new HtmlTree(HtmlTag.BR));
148148
contentTree.add(links.createLink(DocPaths.ALLCLASSES_INDEX,
149-
contents.allClassesLabel));
149+
contents.allClassesLabel));
150150
if (!configuration.packages.isEmpty()) {
151151
contentTree.add(getVerticalSeparator());
152152
contentTree.add(links.createLink(DocPaths.ALLPACKAGES_INDEX,
153-
contents.allPackagesLabel));
153+
contents.allPackagesLabel));
154154
}
155-
if (!configuration.tagSearchIndex.isEmpty()) {
155+
if (!searchItems.get(SearchIndexItem.Category.SEARCH_TAGS).isEmpty()) {
156156
contentTree.add(getVerticalSeparator());
157157
contentTree.add(links.createLink(DocPaths.SYSTEM_PROPERTIES, contents.systemPropertiesLabel));
158158
}

‎src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/SplitIndexWriter.java

+17-17
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727

2828
import java.util.ArrayList;
2929
import java.util.Collection;
30+
import java.util.Iterator;
3031
import java.util.List;
3132
import java.util.ListIterator;
3233
import java.util.Set;
34+
import java.util.SortedSet;
3335
import java.util.TreeSet;
3436

3537
import jdk.javadoc.internal.doclets.formats.html.markup.BodyContents;
@@ -90,20 +92,18 @@ public SplitIndexWriter(HtmlConfiguration configuration,
9092
public static void generate(HtmlConfiguration configuration,
9193
IndexBuilder indexBuilder) throws DocFileIOException {
9294
DocPath path = DocPaths.INDEX_FILES;
93-
Set<Character> keys = new TreeSet<>(indexBuilder.asMap().keySet());
94-
keys.addAll(configuration.tagSearchIndexKeys);
95+
SortedSet<Character> keys = new TreeSet<>(indexBuilder.asMap().keySet());
96+
Collection<SearchIndexItem> searchItems =
97+
configuration.searchItems.get(SearchIndexItem.Category.SEARCH_TAGS);
98+
keys.addAll(buildSearchTagIndex(searchItems).keySet());
9599
ListIterator<Character> li = new ArrayList<>(keys).listIterator();
96-
int prev;
97-
int next;
98100
while (li.hasNext()) {
99-
prev = (li.hasPrevious()) ? li.previousIndex() + 1 : -1;
100-
Object ch = li.next();
101-
next = (li.hasNext()) ? li.nextIndex() + 1 : -1;
101+
Character ch = li.next();
102102
DocPath filename = DocPaths.indexN(li.nextIndex());
103103
SplitIndexWriter indexgen = new SplitIndexWriter(configuration,
104-
path.resolve(filename),
105-
indexBuilder, keys);
106-
indexgen.generateIndexFile((Character) ch);
104+
path.resolve(filename),
105+
indexBuilder, keys);
106+
indexgen.generateIndexFile(ch);
107107
if (!li.hasNext()) {
108108
indexgen.createSearchIndexFiles();
109109
}
@@ -133,13 +133,13 @@ protected void generateIndexFile(Character unicode) throws DocFileIOException {
133133
HtmlTree divTree = new HtmlTree(HtmlTag.DIV);
134134
divTree.setStyle(HtmlStyle.contentContainer);
135135
addLinksForIndexes(divTree);
136-
if (configuration.tagSearchIndexMap.get(unicode) == null) {
136+
if (tagSearchIndexMap.get(unicode) == null) {
137137
addContents(unicode, indexBuilder.getMemberList(unicode), divTree);
138138
} else if (indexBuilder.getMemberList(unicode) == null) {
139-
addSearchContents(unicode, configuration.tagSearchIndexMap.get(unicode), divTree);
139+
addSearchContents(unicode, tagSearchIndexMap.get(unicode), divTree);
140140
} else {
141141
addContents(unicode, indexBuilder.getMemberList(unicode),
142-
configuration.tagSearchIndexMap.get(unicode), divTree);
142+
tagSearchIndexMap.get(unicode), divTree);
143143
}
144144
addLinksForIndexes(divTree);
145145
main.add(divTree);
@@ -170,16 +170,16 @@ protected void addLinksForIndexes(Content contentTree) {
170170
}
171171
contentTree.add(new HtmlTree(HtmlTag.BR));
172172
contentTree.add(links.createLink(pathToRoot.resolve(DocPaths.ALLCLASSES_INDEX),
173-
contents.allClassesLabel));
173+
contents.allClassesLabel));
174174
if (!configuration.packages.isEmpty()) {
175175
contentTree.add(getVerticalSeparator());
176176
contentTree.add(links.createLink(pathToRoot.resolve(DocPaths.ALLPACKAGES_INDEX),
177-
contents.allPackagesLabel));
177+
contents.allPackagesLabel));
178178
}
179-
if (!configuration.tagSearchIndex.isEmpty()) {
179+
if (!searchItems.get(SearchIndexItem.Category.SEARCH_TAGS).isEmpty()) {
180180
contentTree.add(getVerticalSeparator());
181181
contentTree.add(links.createLink(pathToRoot.resolve(DocPaths.SYSTEM_PROPERTIES),
182-
contents.systemPropertiesLabel));
182+
contents.systemPropertiesLabel));
183183
}
184184
}
185185
}

‎src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/SystemPropertiesWriter.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -148,7 +148,7 @@ protected void addSystemProperties(Content content) {
148148

149149
private Map<String, List<SearchIndexItem>> groupSystemProperties() {
150150
Map<String, List<SearchIndexItem>> searchIndexMap = new TreeMap<>();
151-
for (SearchIndexItem searchIndex : configuration.tagSearchIndex) {
151+
for (SearchIndexItem searchIndex : searchItems.get(SearchIndexItem.Category.SEARCH_TAGS)) {
152152
if (searchIndex.isSystemProperty()) {
153153
List<SearchIndexItem> list = searchIndexMap
154154
.computeIfAbsent(searchIndex.getLabel(), k -> new ArrayList<>());

‎src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/TagletWriterImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ protected Void defaultAction(Element e, Void p) {
470470
}
471471
}.visit(element);
472472
si.setCategory(SearchIndexItem.Category.SEARCH_TAGS);
473-
configuration.tagSearchIndex.add(si);
473+
configuration.searchItems.add(si);
474474
}
475475
}
476476
return result;

0 commit comments

Comments
 (0)
Please sign in to comment.