23
23
*/
24
24
package org .opengrok .indexer .web ;
25
25
26
- import java .util .ArrayList ;
27
- import java .util .Collections ;
28
26
import java .util .Comparator ;
29
27
import java .util .Iterator ;
30
28
import java .util .List ;
31
29
import java .util .Map ;
30
+ import java .util .NavigableSet ;
32
31
import java .util .TreeMap ;
32
+ import java .util .TreeSet ;
33
33
34
34
/**
35
35
* A list-like container for javascripts in the page.
@@ -111,11 +111,14 @@ public String toHtml() {
111
111
SCRIPTS .put ("jquery-caret" , new FileScript ("js/jquery.caret-1.5.2.min.js" , 25 ));
112
112
}
113
113
114
+ private static final Comparator <Script > SCRIPTS_COMPARATOR = Comparator
115
+ .comparingInt (Script ::getPriority )
116
+ .thenComparing (Script ::getScriptData );
117
+
114
118
/**
115
- * Scripts which will be written to the page. We assume that the length
116
- * could be the same as for {@link #SCRIPTS}.
119
+ * Scripts which will be written to the page.
117
120
*/
118
- private final List <Script > outputScripts = new ArrayList <>(SCRIPTS . size () );
121
+ private final NavigableSet <Script > outputScripts = new TreeSet <>(SCRIPTS_COMPARATOR );
119
122
120
123
/**
121
124
* Convert the page scripts into HTML.
@@ -134,7 +137,6 @@ public String toHtml() {
134
137
* Return the HTML representation of the page scripts.
135
138
*
136
139
* @return the HTML
137
- *
138
140
* @see #toHtml()
139
141
*/
140
142
@ Override
@@ -146,7 +148,6 @@ public String toString() {
146
148
* Return the size of the page scripts.
147
149
*
148
150
* @return the size
149
- *
150
151
* @see List#size()
151
152
*/
152
153
public int size () {
@@ -157,7 +158,6 @@ public int size() {
157
158
* Check if there is any script for this page.
158
159
*
159
160
* @return true if there is not; false otherwise
160
- *
161
161
* @see List#isEmpty()
162
162
*/
163
163
public boolean isEmpty () {
@@ -179,7 +179,7 @@ public Iterator<Script> iterator() {
179
179
* Add a script which is identified by the name.
180
180
*
181
181
* @param contextPath given context path for the used URL
182
- * @param scriptName name of the script
182
+ * @param scriptName name of the script
183
183
* @return true if script was added; false otherwise
184
184
*/
185
185
public boolean addScript (String contextPath , String scriptName ) {
@@ -195,46 +195,11 @@ public boolean addScript(String contextPath, String scriptName) {
195
195
}
196
196
197
197
/**
198
- * Add a script to the page, taking the script priority into account. The
199
- * position is determined as the upper bound for the given priority.
198
+ * Add a script to the page, taking the script priority into account.
200
199
*
201
200
* @param script the script
202
201
*/
203
202
public void addScript (Script script ) {
204
- int index = Collections .binarySearch (outputScripts , script , new Comparator <Script >() {
205
- @ Override
206
- public int compare (Script a , Script b ) {
207
- return a .getPriority () - b .getPriority ();
208
- }
209
- });
210
- if (index < 0 ) {
211
- /**
212
- * Key is not found in the list the index<br>
213
- * equals to -(insertion index) - 1.
214
- */
215
- this .outputScripts .add (Math .abs (index + 1 ), script );
216
- } else {
217
- /**
218
- * Key found in the list, append it after the last element with the
219
- * same priority => insert it at the upper bound index.
220
- */
221
- while (index < this .outputScripts .size ()
222
- && this .outputScripts .get (index ).getPriority () == script .getPriority ()) {
223
- index ++;
224
- }
225
- this .outputScripts .add (index , script );
226
- }
227
- }
228
-
229
- /**
230
- * Get the page script by the index
231
- *
232
- * @param index index of the script
233
- * @return the script
234
- *
235
- * @see List#get(int)
236
- */
237
- public Script get (int index ) {
238
- return outputScripts .get (index );
203
+ this .outputScripts .add (script );
239
204
}
240
205
}
0 commit comments