1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2026 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.checks.imports;
21
22 import java.util.Locale;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25
26 import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
27 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
28 import com.puppycrawl.tools.checkstyle.api.DetailAST;
29 import com.puppycrawl.tools.checkstyle.api.FullIdent;
30 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
31 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
32 import com.puppycrawl.tools.checkstyle.utils.UnmodifiableCollectionUtil;
33
34 /**
35 * <div>
36 * Checks the ordering/grouping of imports. Features are:
37 * </div>
38 * <ul>
39 * <li>
40 * groups type/static imports: ensures that groups of imports come in a specific order
41 * (e.g., java. comes first, javax. comes second, then everything else)
42 * </li>
43 * <li>
44 * adds a separation between type import groups : ensures that a blank line sit between each group
45 * </li>
46 * <li>
47 * type/static import groups aren't separated internally: ensures that each group aren't separated
48 * internally by blank line or comment
49 * </li>
50 * <li>
51 * sorts type/static imports inside each group: ensures that imports within each group are in
52 * lexicographic order
53 * </li>
54 * <li>
55 * sorts according to case: ensures that the comparison between imports is case-sensitive, in
56 * <a href="https://en.wikipedia.org/wiki/ASCII#Order">ASCII sort order</a>
57 * </li>
58 * <li>
59 * arrange static imports: ensures the relative order between type imports and static imports
60 * (see
61 * <a href="https://checkstyle.org/property_types.html#ImportOrderOption">ImportOrderOption</a>)
62 * </li>
63 * </ul>
64 *
65 * @since 3.2
66 */
67 @FileStatefulCheck
68 public class ImportOrderCheck
69 extends AbstractCheck {
70
71 /**
72 * A key is pointing to the warning message text in "messages.properties"
73 * file.
74 */
75 public static final String MSG_SEPARATION = "import.separation";
76
77 /**
78 * A key is pointing to the warning message text in "messages.properties"
79 * file.
80 */
81 public static final String MSG_SEPARATED_IN_GROUP = "import.groups.separated.internally";
82
83 /**
84 * A key is pointing to the warning message text in "messages.properties"
85 * file.
86 */
87 public static final String MSG_ORDERING_LEX = "import.ordering.lex";
88
89 /**
90 * A key is pointing to the warning message text in "messages.properties"
91 * file.
92 */
93 public static final String MSG_ORDERING_STATIC = "import.ordering.static";
94
95 /**
96 * A key is pointing to the warning message text in "messages.properties"
97 * file.
98 */
99 public static final String MSG_ORDERING_GROUP = "import.ordering.group";
100
101 /** The special wildcard that catches all remaining groups. */
102 private static final String WILDCARD_GROUP_NAME = "*";
103
104 /** The Forward slash. */
105 private static final String FORWARD_SLASH = "/";
106
107 /** Empty array of pattern type needed to initialize check. */
108 private static final Pattern[] EMPTY_PATTERN_ARRAY = new Pattern[0];
109
110 /**
111 * Specify list of <b>type import</b> groups. Every group identified either by a common prefix
112 * string, or by a regular expression enclosed in forward slashes (e.g. {@code /regexp/}).
113 * If an import matches two or more groups,
114 * the best match is selected (closest to the start, and the longest match).
115 * All type imports, which does not match any group, falls into an additional group,
116 * located at the end. Thus, the empty list of type groups (the default value) means one group
117 * for all type imports.
118 */
119 private String[] groups = CommonUtil.EMPTY_STRING_ARRAY;
120
121 /**
122 * Specify list of <b>static</b> import groups. Every group identified either by a common prefix
123 * string, or by a regular expression enclosed in forward slashes (e.g. {@code /regexp/}).
124 * If an import matches two or more groups,
125 * the best match is selected (closest to the start, and the longest match).
126 * All static imports, which does not match any group, fall into an additional group, located
127 * at the end. Thus, the empty list of static groups (the default value) means one group for all
128 * static imports. This property has effect only when the property {@code option} is set to
129 * {@code top} or {@code bottom}.
130 */
131 private String[] staticGroups = CommonUtil.EMPTY_STRING_ARRAY;
132
133 /**
134 * Control whether type import groups should be separated by, at least, one blank
135 * line or comment and aren't separated internally. It doesn't affect separations for static
136 * imports.
137 */
138 private boolean separated;
139
140 /**
141 * Control whether static import groups should be separated by, at least, one blank
142 * line or comment and aren't separated internally. When the property {@code option}
143 * is set to {@code top} or {@code bottom}, this property requires {@code staticGroups}
144 * to be enabled. When the property {@code option} is set to {@code under} or
145 * {@code above}, this property allows blank lines between static and non-static
146 * imports within the same group.
147 */
148 private boolean separatedStaticGroups;
149
150 /**
151 * Control whether type imports within each group should be sorted.
152 * It doesn't affect sorting for static imports.
153 */
154 private boolean ordered = true;
155
156 /**
157 * Control whether string comparison should be case-sensitive or not. Case-sensitive
158 * sorting is in <a href="https://en.wikipedia.org/wiki/ASCII#Order">ASCII sort order</a>.
159 * It affects both type imports and static imports.
160 */
161 private boolean caseSensitive = true;
162
163 /** Last imported group. */
164 private int lastGroup;
165 /** Line number of last import. */
166 private int lastImportLine;
167 /** Name of last import. */
168 private String lastImport;
169 /** If last import was static. */
170 private boolean lastImportStatic;
171 /** Whether there were any imports. */
172 private boolean beforeFirstImport;
173 /**
174 * Whether static and type import groups should be split apart.
175 * When the {@code option} property is set to {@code INFLOW}, {@code ABOVE} or {@code UNDER},
176 * both the type and static imports use the properties {@code groups} and {@code separated}.
177 * When the {@code option} property is set to {@code TOP} or {@code BOTTOM}, static imports
178 * uses the properties {@code staticGroups} and {@code separatedStaticGroups}.
179 **/
180 private boolean staticImportsApart;
181
182 /**
183 * Control whether <b>static imports</b> located at <b>top</b> or <b>bottom</b> are
184 * sorted within the group.
185 */
186 private boolean sortStaticImportsAlphabetically;
187
188 /**
189 * Control whether to use container ordering (Eclipse IDE term) for static imports
190 * or not.
191 */
192 private boolean useContainerOrderingForStatic;
193
194 /**
195 * Specify policy on the relative order between type imports and static imports.
196 */
197 private ImportOrderOption option = ImportOrderOption.UNDER;
198
199 /**
200 * Complied array of patterns for property {@code groups}.
201 */
202 private Pattern[] groupsReg = EMPTY_PATTERN_ARRAY;
203
204 /**
205 * Complied array of patterns for property {@code staticGroups}.
206 */
207 private Pattern[] staticGroupsReg = EMPTY_PATTERN_ARRAY;
208
209 /**
210 * Setter to specify policy on the relative order between type imports and static imports.
211 *
212 * @param optionStr string to decode option from
213 * @throws IllegalArgumentException if unable to decode
214 * @since 5.0
215 */
216 public void setOption(String optionStr) {
217 option = ImportOrderOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
218 }
219
220 /**
221 * Setter to specify list of <b>type import</b> groups. Every group identified either by a
222 * common prefix string, or by a regular expression enclosed in forward slashes
223 * (e.g. {@code /regexp/}). If an import matches two or more groups,
224 * the best match is selected (closest to the start, and the longest match).
225 * All type imports, which does not match any group, falls into an
226 * additional group, located at the end. Thus, the empty list of type groups (the default value)
227 * means one group for all type imports.
228 *
229 * @param packageGroups a comma-separated list of package names/prefixes.
230 * @since 3.2
231 */
232 public void setGroups(String... packageGroups) {
233 groups = UnmodifiableCollectionUtil.copyOfArray(packageGroups, packageGroups.length);
234 groupsReg = compilePatterns(packageGroups);
235 }
236
237 /**
238 * Setter to specify list of <b>static</b> import groups. Every group identified either by a
239 * common prefix string, or by a regular expression enclosed in forward slashes
240 * (e.g. {@code /regexp/}). If an import matches two or more groups,
241 * the best match is selected (closest to the start, and the longest match).
242 * All static imports, which does not match any group, fall into an
243 * additional group, located at the end. Thus, the empty list of static groups (the default
244 * value) means one group for all static imports. This property has effect only when
245 * the property {@code option} is set to {@code top} or {@code bottom}.
246 *
247 * @param packageGroups a comma-separated list of package names/prefixes.
248 * @since 8.12
249 */
250 public void setStaticGroups(String... packageGroups) {
251 staticGroups = UnmodifiableCollectionUtil.copyOfArray(packageGroups, packageGroups.length);
252 staticGroupsReg = compilePatterns(packageGroups);
253 }
254
255 /**
256 * Setter to control whether type imports within each group should be sorted.
257 * It doesn't affect sorting for static imports.
258 *
259 * @param ordered
260 * whether lexicographic ordering of imports within a group
261 * required or not.
262 * @since 3.2
263 */
264 public void setOrdered(boolean ordered) {
265 this.ordered = ordered;
266 }
267
268 /**
269 * Setter to control whether type import groups should be separated by, at least,
270 * one blank line or comment and aren't separated internally.
271 * It doesn't affect separations for static imports.
272 *
273 * @param separated
274 * whether groups should be separated by one blank line or comment.
275 * @since 3.2
276 */
277 public void setSeparated(boolean separated) {
278 this.separated = separated;
279 }
280
281 /**
282 * Setter to control whether static import groups should be separated by, at least,
283 * one blank line or comment and aren't separated internally.
284 * When the property {@code option} is set to {@code top} or {@code bottom},
285 * this property requires {@code staticGroups} to be enabled.
286 * When the property {@code option} is set to {@code under} or {@code above},
287 * this property allows blank lines between static and non-static imports
288 * within the same group.
289 *
290 * @param separatedStaticGroups
291 * whether groups should be separated by one blank line or comment.
292 * @since 8.12
293 */
294 public void setSeparatedStaticGroups(boolean separatedStaticGroups) {
295 this.separatedStaticGroups = separatedStaticGroups;
296 }
297
298 /**
299 * Setter to control whether string comparison should be case-sensitive or not.
300 * Case-sensitive sorting is in
301 * <a href="https://en.wikipedia.org/wiki/ASCII#Order">ASCII sort order</a>.
302 * It affects both type imports and static imports.
303 *
304 * @param caseSensitive
305 * whether string comparison should be case-sensitive.
306 * @since 3.3
307 */
308 public void setCaseSensitive(boolean caseSensitive) {
309 this.caseSensitive = caseSensitive;
310 }
311
312 /**
313 * Setter to control whether <b>static imports</b> located at <b>top</b> or
314 * <b>bottom</b> are sorted within the group.
315 *
316 * @param sortAlphabetically true or false.
317 * @since 6.5
318 */
319 public void setSortStaticImportsAlphabetically(boolean sortAlphabetically) {
320 sortStaticImportsAlphabetically = sortAlphabetically;
321 }
322
323 /**
324 * Setter to control whether to use container ordering (Eclipse IDE term) for static
325 * imports or not.
326 *
327 * @param useContainerOrdering whether to use container ordering for static imports or not.
328 * @since 7.1
329 */
330 public void setUseContainerOrderingForStatic(boolean useContainerOrdering) {
331 useContainerOrderingForStatic = useContainerOrdering;
332 }
333
334 @Override
335 public int[] getDefaultTokens() {
336 return getRequiredTokens();
337 }
338
339 @Override
340 public int[] getAcceptableTokens() {
341 return getRequiredTokens();
342 }
343
344 @Override
345 public int[] getRequiredTokens() {
346 return new int[] {TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
347 }
348
349 @Override
350 public void beginTree(DetailAST rootAST) {
351 lastGroup = Integer.MIN_VALUE;
352 lastImportLine = Integer.MIN_VALUE;
353 lastImportStatic = false;
354 beforeFirstImport = true;
355 staticImportsApart =
356 option == ImportOrderOption.TOP || option == ImportOrderOption.BOTTOM;
357 }
358
359 // -@cs[CyclomaticComplexity] SWITCH was transformed into IF-ELSE.
360 @Override
361 public void visitToken(DetailAST ast) {
362 final FullIdent ident;
363 final boolean isStatic;
364
365 if (ast.getType() == TokenTypes.IMPORT) {
366 ident = FullIdent.createFullIdentBelow(ast);
367 isStatic = false;
368 }
369 else {
370 ident = FullIdent.createFullIdent(ast.getFirstChild()
371 .getNextSibling());
372 isStatic = true;
373 }
374
375 // using set of IF instead of SWITCH to analyze Enum options to satisfy coverage.
376 // https://github.com/checkstyle/checkstyle/issues/1387
377 if (option == ImportOrderOption.TOP || option == ImportOrderOption.ABOVE) {
378 final boolean isStaticAndNotLastImport = isStatic && !lastImportStatic;
379 doVisitToken(ident, isStatic, isStaticAndNotLastImport, ast);
380 }
381 else if (option == ImportOrderOption.BOTTOM || option == ImportOrderOption.UNDER) {
382 final boolean isLastImportAndNonStatic = lastImportStatic && !isStatic;
383 doVisitToken(ident, isStatic, isLastImportAndNonStatic, ast);
384 }
385 else if (option == ImportOrderOption.INFLOW) {
386 // "previous" argument is useless here
387 doVisitToken(ident, isStatic, true, ast);
388 }
389 else {
390 throw new IllegalStateException(
391 String.format(Locale.ROOT, "Unexpected option for static imports: %s", option));
392 }
393
394 lastImportLine = ast.findFirstToken(TokenTypes.SEMI).getLineNo();
395 lastImportStatic = isStatic;
396 beforeFirstImport = false;
397 }
398
399 /**
400 * Shares processing...
401 *
402 * @param ident the import to process.
403 * @param isStatic whether the token is static or not.
404 * @param previous previous non-static but current is static (above), or
405 * previous static but current is non-static (under).
406 * @param ast node of the AST.
407 */
408 private void doVisitToken(FullIdent ident, boolean isStatic, boolean previous, DetailAST ast) {
409 final String name = ident.getText();
410 final int groupIdx = getGroupNumber(isStatic && staticImportsApart, name);
411
412 if (groupIdx > lastGroup) {
413 handleGreaterGroup(isStatic, ast, name);
414 }
415 else if (groupIdx == lastGroup) {
416 doVisitTokenInSameGroup(isStatic, previous, name, ast);
417
418 if (separatedStaticGroups
419 && isStatic != lastImportStatic
420 && !isSeparatorBeforeImport(ast.getLineNo())) {
421 log(ast, MSG_SEPARATION, name);
422 }
423 }
424 else {
425 handleLowerGroup(previous, ast, name);
426 }
427 if (isSeparatorInGroup(groupIdx, isStatic, ast.getLineNo())) {
428 log(ast, MSG_SEPARATED_IN_GROUP, name);
429 }
430
431 lastGroup = groupIdx;
432 lastImport = name;
433 }
434
435 /**
436 * Handles the case when the current import belongs to a group
437 * that comes after the previous group. Verifies whether a
438 * separator between groups is required.
439 *
440 * @param isStatic whether the current import is static
441 * @param ast the AST node of the current import
442 * @param name the fully qualified name of the current import
443 */
444 private void handleGreaterGroup(boolean isStatic, DetailAST ast, String name) {
445 if (!beforeFirstImport
446 && ast.getLineNo() - lastImportLine < 2
447 && needSeparator(isStatic)) {
448 log(ast, MSG_SEPARATION, name);
449 }
450 }
451
452 /**
453 * Handles the case when the current import belongs to a group
454 * that should appear before the previous group. Logs either
455 * a static-order violation or a group-order violation.
456 *
457 * @param previous indicates a static/non-static ordering transition
458 * @param ast the AST node of the current import
459 * @param name the fully qualified name of the current import
460 */
461 private void handleLowerGroup(boolean previous, DetailAST ast, String name) {
462 if (previous
463 && (option == ImportOrderOption.TOP || option == ImportOrderOption.BOTTOM)) {
464 log(ast, MSG_ORDERING_STATIC, name);
465 }
466 else {
467 log(ast, MSG_ORDERING_GROUP, name);
468 }
469 }
470
471 /**
472 * Checks whether import groups should be separated.
473 *
474 * @param isStatic whether the token is static or not.
475 * @return true if imports groups should be separated.
476 */
477 private boolean needSeparator(boolean isStatic) {
478 final boolean typeImportSeparator = !isStatic && separated;
479 final boolean staticImportSeparator;
480 if (staticImportsApart) {
481 staticImportSeparator = isStatic && separatedStaticGroups;
482 }
483 else {
484 staticImportSeparator = separated;
485 }
486 final boolean separatorBetween = isStatic != lastImportStatic
487 && (separated || separatedStaticGroups);
488
489 return typeImportSeparator || staticImportSeparator || separatorBetween;
490 }
491
492 /**
493 * Checks whether imports group separated internally.
494 *
495 * @param groupIdx group number.
496 * @param isStatic whether the token is static or not.
497 * @param line the line of the current import.
498 * @return true if imports group are separated internally.
499 */
500 private boolean isSeparatorInGroup(int groupIdx, boolean isStatic, int line) {
501 final boolean inSameGroup = groupIdx == lastGroup;
502 final boolean result;
503 if (inSameGroup) {
504 result = (isStatic == lastImportStatic || !separatedStaticGroups)
505 && isSeparatorBeforeImport(line);
506 }
507 else {
508 result = !needSeparator(isStatic) && isSeparatorBeforeImport(line);
509 }
510 return result;
511 }
512
513 /**
514 * Checks whether there is any separator before current import.
515 *
516 * @param line the line of the current import.
517 * @return true if there is separator before current import which isn't the first import.
518 */
519 private boolean isSeparatorBeforeImport(int line) {
520 return line - lastImportLine > 1;
521 }
522
523 /**
524 * Shares processing...
525 *
526 * @param isStatic whether the token is static or not.
527 * @param previous previous non-static but current is static (above), or
528 * previous static but current is non-static (under).
529 * @param name the name of the current import.
530 * @param ast node of the AST.
531 */
532 private void doVisitTokenInSameGroup(boolean isStatic,
533 boolean previous, String name, DetailAST ast) {
534 if (ordered) {
535 if (option == ImportOrderOption.INFLOW) {
536 if (isWrongOrder(name, isStatic)) {
537 log(ast, MSG_ORDERING_LEX, name, lastImport);
538 }
539 }
540 else {
541 if (previous) {
542 // static vs non-static placement violation
543 log(ast, MSG_ORDERING_STATIC, name);
544 }
545 else if (lastImportStatic == isStatic && isWrongOrder(name, isStatic)) {
546 // lexicographical violation
547 log(ast, MSG_ORDERING_LEX, name, lastImport);
548 }
549 }
550 }
551 }
552
553 /**
554 * Checks whether import name is in wrong order.
555 *
556 * @param name import name.
557 * @param isStatic whether it is a static import name.
558 * @return true if import name is in wrong order.
559 */
560 private boolean isWrongOrder(String name, boolean isStatic) {
561 final boolean result;
562 if (isStatic) {
563 if (useContainerOrderingForStatic) {
564 result = compareContainerOrder(lastImport, name, caseSensitive) > 0;
565 }
566 else if (staticImportsApart) {
567 result = sortStaticImportsAlphabetically
568 && compare(lastImport, name, caseSensitive) > 0;
569 }
570 else {
571 result = compare(lastImport, name, caseSensitive) > 0;
572 }
573 }
574 else {
575 // out of lexicographic order
576 result = compare(lastImport, name, caseSensitive) > 0;
577 }
578 return result;
579 }
580
581 /**
582 * Compares two import strings.
583 * We first compare the container of the static import, container being the type enclosing
584 * the static element being imported. When this returns 0, we compare the qualified
585 * import name. For e.g. this is what is considered to be container names:
586 * <pre>
587 * import static HttpConstants.COLON => HttpConstants
588 * import static HttpHeaders.addHeader => HttpHeaders
589 * import static HttpHeaders.setHeader => HttpHeaders
590 * import static HttpHeaders.Names.DATE => HttpHeaders.Names
591 * </pre>
592 *
593 * <p>
594 * According to this logic, HttpHeaders.Names would come after HttpHeaders.
595 * For more details, see <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=473629#c3">
596 * static imports comparison method</a> in Eclipse.
597 * </p>
598 *
599 * @param importName1 first import name
600 * @param importName2 second import name
601 * @param caseSensitive whether the comparison of fully qualified import names is
602 * case-sensitive
603 * @return the value {@code 0} if str1 is equal to str2; a value
604 * less than {@code 0} if str is less than the str2 (container order
605 * or lexicographical); and a value greater than {@code 0} if str1 is greater than str2
606 * (container order or lexicographically)
607 */
608 private static int compareContainerOrder(String importName1, String importName2,
609 boolean caseSensitive) {
610 final String container1 = getImportContainer(importName1);
611 final String container2 = getImportContainer(importName2);
612 final int compareContainersOrderResult;
613 if (caseSensitive) {
614 compareContainersOrderResult = container1.compareTo(container2);
615 }
616 else {
617 compareContainersOrderResult = container1.compareToIgnoreCase(container2);
618 }
619 final int result;
620 if (compareContainersOrderResult == 0) {
621 result = compare(importName1, importName2, caseSensitive);
622 }
623 else {
624 result = compareContainersOrderResult;
625 }
626 return result;
627 }
628
629 /**
630 * Extracts import container name from fully qualified import name.
631 * An import container name is the type which encloses the static element being imported.
632 * For example, HttpConstants, HttpHeaders, HttpHeaders.Names are import container names:
633 * <pre>
634 * import static HttpConstants.COLON => HttpConstants
635 * import static HttpHeaders.addHeader => HttpHeaders
636 * import static HttpHeaders.setHeader => HttpHeaders
637 * import static HttpHeaders.Names.DATE => HttpHeaders.Names
638 * </pre>
639 *
640 * @param qualifiedImportName fully qualified import name.
641 * @return import container name.
642 */
643 private static String getImportContainer(String qualifiedImportName) {
644 final int lastDotIndex = qualifiedImportName.lastIndexOf('.');
645 return qualifiedImportName.substring(0, lastDotIndex);
646 }
647
648 /**
649 * Finds out what group the specified import belongs to.
650 *
651 * @param isStatic whether the token is static or not.
652 * @param name the import name to find.
653 * @return group number for given import name.
654 */
655 private int getGroupNumber(boolean isStatic, String name) {
656 final Pattern[] patterns;
657 if (isStatic) {
658 patterns = staticGroupsReg;
659 }
660 else {
661 patterns = groupsReg;
662 }
663
664 int number = getGroupNumber(patterns, name);
665
666 if (isStatic && option == ImportOrderOption.BOTTOM) {
667 number += groups.length + 1;
668 }
669 else if (!isStatic && option == ImportOrderOption.TOP) {
670 number += staticGroups.length + 1;
671 }
672 return number;
673 }
674
675 /**
676 * Finds out what group the specified import belongs to.
677 *
678 * @param patterns groups to check.
679 * @param name the import name to find.
680 * @return group number for given import name.
681 */
682 private static int getGroupNumber(Pattern[] patterns, String name) {
683 int bestIndex = patterns.length;
684 int bestEnd = -1;
685 int bestPos = Integer.MAX_VALUE;
686
687 // find out what group this belongs in
688 // loop over patterns and get index
689 for (int i = 0; i < patterns.length; i++) {
690 final Matcher matcher = patterns[i].matcher(name);
691 if (matcher.find()) {
692 if (matcher.start() < bestPos) {
693 bestIndex = i;
694 bestEnd = matcher.end();
695 bestPos = matcher.start();
696 }
697 else if (matcher.start() == bestPos && matcher.end() > bestEnd) {
698 bestIndex = i;
699 bestEnd = matcher.end();
700 }
701 }
702 }
703 return bestIndex;
704 }
705
706 /**
707 * Compares two strings.
708 *
709 * @param string1
710 * the first string
711 * @param string2
712 * the second string
713 * @param caseSensitive
714 * whether the comparison is case-sensitive
715 * @return the value {@code 0} if string1 is equal to string2; a value
716 * less than {@code 0} if string1 is lexicographically less
717 * than the string2; and a value greater than {@code 0} if
718 * string1 is lexicographically greater than string2
719 */
720 private static int compare(String string1, String string2,
721 boolean caseSensitive) {
722 final int result;
723 if (caseSensitive) {
724 result = string1.compareTo(string2);
725 }
726 else {
727 result = string1.compareToIgnoreCase(string2);
728 }
729
730 return result;
731 }
732
733 /**
734 * Compiles the list of package groups and the order they should occur in the file.
735 *
736 * @param packageGroups a comma-separated list of package names/prefixes.
737 * @return array of compiled patterns.
738 * @throws IllegalArgumentException if any of the package groups are not valid.
739 */
740 private static Pattern[] compilePatterns(String... packageGroups) {
741 final Pattern[] patterns = new Pattern[packageGroups.length];
742 for (int i = 0; i < packageGroups.length; i++) {
743 String pkg = packageGroups[i];
744 final Pattern grp;
745
746 // if the pkg name is the wildcard, make it match zero chars
747 // from any name, so it will always be used as last resort.
748 if (WILDCARD_GROUP_NAME.equals(pkg)) {
749 // matches any package
750 grp = Pattern.compile("");
751 }
752 else if (pkg.startsWith(FORWARD_SLASH)) {
753 if (!pkg.endsWith(FORWARD_SLASH)) {
754 throw new IllegalArgumentException("Invalid group: " + pkg);
755 }
756 pkg = pkg.substring(1, pkg.length() - 1);
757 grp = Pattern.compile(pkg);
758 }
759 else {
760 final StringBuilder pkgBuilder = new StringBuilder(pkg);
761 if (!pkg.endsWith(".")) {
762 pkgBuilder.append('.');
763 }
764 grp = Pattern.compile("^" + Pattern.quote(pkgBuilder.toString()));
765 }
766
767 patterns[i] = grp;
768 }
769 return patterns;
770 }
771
772 }