001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2026 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018///////////////////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.imports;
021
022import java.util.Locale;
023import java.util.regex.Matcher;
024import java.util.regex.Pattern;
025
026import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
027import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
028import com.puppycrawl.tools.checkstyle.api.DetailAST;
029import com.puppycrawl.tools.checkstyle.api.FullIdent;
030import com.puppycrawl.tools.checkstyle.api.TokenTypes;
031import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
032import com.puppycrawl.tools.checkstyle.utils.UnmodifiableCollectionUtil;
033
034/**
035 * <div>
036 * Checks the ordering/grouping of imports. Features are:
037 * </div>
038 * <ul>
039 * <li>
040 * groups type/static imports: ensures that groups of imports come in a specific order
041 * (e.g., java. comes first, javax. comes second, then everything else)
042 * </li>
043 * <li>
044 * adds a separation between type import groups : ensures that a blank line sit between each group
045 * </li>
046 * <li>
047 * type/static import groups aren't separated internally: ensures that each group aren't separated
048 * internally by blank line or comment
049 * </li>
050 * <li>
051 * sorts type/static imports inside each group: ensures that imports within each group are in
052 * lexicographic order
053 * </li>
054 * <li>
055 * sorts according to case: ensures that the comparison between imports is case-sensitive, in
056 * <a href="https://en.wikipedia.org/wiki/ASCII#Order">ASCII sort order</a>
057 * </li>
058 * <li>
059 * arrange static imports: ensures the relative order between type imports and static imports
060 * (see
061 * <a href="https://checkstyle.org/property_types.html#ImportOrderOption">ImportOrderOption</a>)
062 * </li>
063 * </ul>
064 *
065 * @since 3.2
066 */
067@FileStatefulCheck
068public class ImportOrderCheck
069    extends AbstractCheck {
070
071    /**
072     * A key is pointing to the warning message text in "messages.properties"
073     * file.
074     */
075    public static final String MSG_SEPARATION = "import.separation";
076
077    /**
078     * A key is pointing to the warning message text in "messages.properties"
079     * file.
080     */
081    public static final String MSG_SEPARATED_IN_GROUP = "import.groups.separated.internally";
082
083    /**
084     * A key is pointing to the warning message text in "messages.properties"
085     * file.
086     */
087    public static final String MSG_ORDERING_LEX = "import.ordering.lex";
088
089    /**
090     * A key is pointing to the warning message text in "messages.properties"
091     * file.
092     */
093    public static final String MSG_ORDERING_STATIC = "import.ordering.static";
094
095    /**
096     * A key is pointing to the warning message text in "messages.properties"
097     * file.
098     */
099    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        else {
419            handleLowerGroup(previous, ast, name);
420        }
421        if (isSeparatorInGroup(groupIdx, isStatic, ast.getLineNo())) {
422            log(ast, MSG_SEPARATED_IN_GROUP, name);
423        }
424
425        lastGroup = groupIdx;
426        lastImport = name;
427    }
428
429    /**
430     * Handles the case when the current import belongs to a group
431     * that comes after the previous group. Verifies whether a
432     * separator between groups is required.
433     *
434     * @param isStatic whether the current import is static
435     * @param ast the AST node of the current import
436     * @param name the fully qualified name of the current import
437     */
438    private void handleGreaterGroup(boolean isStatic, DetailAST ast, String name) {
439        if (!beforeFirstImport
440            && ast.getLineNo() - lastImportLine < 2
441            && needSeparator(isStatic)) {
442            log(ast, MSG_SEPARATION, name);
443        }
444    }
445
446    /**
447     * Handles the case when the current import belongs to a group
448     * that should appear before the previous group. Logs either
449     * a static-order violation or a group-order violation.
450     *
451     * @param previous indicates a static/non-static ordering transition
452     * @param ast the AST node of the current import
453     * @param name the fully qualified name of the current import
454     */
455    private void handleLowerGroup(boolean previous, DetailAST ast, String name) {
456        if (previous
457                && (option == ImportOrderOption.TOP || option == ImportOrderOption.BOTTOM)) {
458            log(ast, MSG_ORDERING_STATIC, name);
459        }
460        else {
461            log(ast, MSG_ORDERING_GROUP, name);
462        }
463    }
464
465    /**
466     * Checks whether import groups should be separated.
467     *
468     * @param isStatic whether the token is static or not.
469     * @return true if imports groups should be separated.
470     */
471    private boolean needSeparator(boolean isStatic) {
472        final boolean typeImportSeparator = !isStatic && separated;
473        final boolean staticImportSeparator;
474        if (staticImportsApart) {
475            staticImportSeparator = isStatic && separatedStaticGroups;
476        }
477        else {
478            staticImportSeparator = separated;
479        }
480        final boolean separatorBetween = isStatic != lastImportStatic
481            && (separated || separatedStaticGroups);
482
483        return typeImportSeparator || staticImportSeparator || separatorBetween;
484    }
485
486    /**
487     * Checks whether imports group separated internally.
488     *
489     * @param groupIdx group number.
490     * @param isStatic whether the token is static or not.
491     * @param line the line of the current import.
492     * @return true if imports group are separated internally.
493     */
494    private boolean isSeparatorInGroup(int groupIdx, boolean isStatic, int line) {
495        final boolean inSameGroup = groupIdx == lastGroup;
496        final boolean result;
497        if (inSameGroup) {
498            result = (isStatic == lastImportStatic || !separatedStaticGroups)
499                    && isSeparatorBeforeImport(line);
500        }
501        else {
502            result = !needSeparator(isStatic) && isSeparatorBeforeImport(line);
503        }
504        return result;
505    }
506
507    /**
508     * Checks whether there is any separator before current import.
509     *
510     * @param line the line of the current import.
511     * @return true if there is separator before current import which isn't the first import.
512     */
513    private boolean isSeparatorBeforeImport(int line) {
514        return line - lastImportLine > 1;
515    }
516
517    /**
518     * Shares processing...
519     *
520     * @param isStatic whether the token is static or not.
521     * @param previous previous non-static but current is static (above), or
522     *     previous static but current is non-static (under).
523     * @param name the name of the current import.
524     * @param ast node of the AST.
525     */
526    private void doVisitTokenInSameGroup(boolean isStatic,
527            boolean previous, String name, DetailAST ast) {
528        if (ordered) {
529            if (option == ImportOrderOption.INFLOW) {
530                if (isWrongOrder(name, isStatic)) {
531                    log(ast, MSG_ORDERING_LEX, name, lastImport);
532                }
533            }
534            else {
535                if (previous) {
536                    // static vs non-static placement violation
537                    log(ast, MSG_ORDERING_STATIC, name);
538                }
539                else if (lastImportStatic == isStatic && isWrongOrder(name, isStatic)) {
540                    // lexicographical violation
541                    log(ast, MSG_ORDERING_LEX, name, lastImport);
542                }
543            }
544        }
545    }
546
547    /**
548     * Checks whether import name is in wrong order.
549     *
550     * @param name import name.
551     * @param isStatic whether it is a static import name.
552     * @return true if import name is in wrong order.
553     */
554    private boolean isWrongOrder(String name, boolean isStatic) {
555        final boolean result;
556        if (isStatic) {
557            if (useContainerOrderingForStatic) {
558                result = compareContainerOrder(lastImport, name, caseSensitive) > 0;
559            }
560            else if (staticImportsApart) {
561                result = sortStaticImportsAlphabetically
562                    && compare(lastImport, name, caseSensitive) > 0;
563            }
564            else {
565                result = compare(lastImport, name, caseSensitive) > 0;
566            }
567        }
568        else {
569            // out of lexicographic order
570            result = compare(lastImport, name, caseSensitive) > 0;
571        }
572        return result;
573    }
574
575    /**
576     * Compares two import strings.
577     * We first compare the container of the static import, container being the type enclosing
578     * the static element being imported. When this returns 0, we compare the qualified
579     * import name. For e.g. this is what is considered to be container names:
580     * <pre>
581     * import static HttpConstants.COLON     =&gt; HttpConstants
582     * import static HttpHeaders.addHeader   =&gt; HttpHeaders
583     * import static HttpHeaders.setHeader   =&gt; HttpHeaders
584     * import static HttpHeaders.Names.DATE  =&gt; HttpHeaders.Names
585     * </pre>
586     *
587     * <p>
588     * According to this logic, HttpHeaders.Names would come after HttpHeaders.
589     * For more details, see <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=473629#c3">
590     * static imports comparison method</a> in Eclipse.
591     * </p>
592     *
593     * @param importName1 first import name
594     * @param importName2 second import name
595     * @param caseSensitive whether the comparison of fully qualified import names is
596     *                      case-sensitive
597     * @return the value {@code 0} if str1 is equal to str2; a value
598     *         less than {@code 0} if str is less than the str2 (container order
599     *         or lexicographical); and a value greater than {@code 0} if str1 is greater than str2
600     *         (container order or lexicographically)
601     */
602    private static int compareContainerOrder(String importName1, String importName2,
603                                             boolean caseSensitive) {
604        final String container1 = getImportContainer(importName1);
605        final String container2 = getImportContainer(importName2);
606        final int compareContainersOrderResult;
607        if (caseSensitive) {
608            compareContainersOrderResult = container1.compareTo(container2);
609        }
610        else {
611            compareContainersOrderResult = container1.compareToIgnoreCase(container2);
612        }
613        final int result;
614        if (compareContainersOrderResult == 0) {
615            result = compare(importName1, importName2, caseSensitive);
616        }
617        else {
618            result = compareContainersOrderResult;
619        }
620        return result;
621    }
622
623    /**
624     * Extracts import container name from fully qualified import name.
625     * An import container name is the type which encloses the static element being imported.
626     * For example, HttpConstants, HttpHeaders, HttpHeaders.Names are import container names:
627     * <pre>
628     * import static HttpConstants.COLON     =&gt; HttpConstants
629     * import static HttpHeaders.addHeader   =&gt; HttpHeaders
630     * import static HttpHeaders.setHeader   =&gt; HttpHeaders
631     * import static HttpHeaders.Names.DATE  =&gt; HttpHeaders.Names
632     * </pre>
633     *
634     * @param qualifiedImportName fully qualified import name.
635     * @return import container name.
636     */
637    private static String getImportContainer(String qualifiedImportName) {
638        final int lastDotIndex = qualifiedImportName.lastIndexOf('.');
639        return qualifiedImportName.substring(0, lastDotIndex);
640    }
641
642    /**
643     * Finds out what group the specified import belongs to.
644     *
645     * @param isStatic whether the token is static or not.
646     * @param name the import name to find.
647     * @return group number for given import name.
648     */
649    private int getGroupNumber(boolean isStatic, String name) {
650        final Pattern[] patterns;
651        if (isStatic) {
652            patterns = staticGroupsReg;
653        }
654        else {
655            patterns = groupsReg;
656        }
657
658        int number = getGroupNumber(patterns, name);
659
660        if (isStatic && option == ImportOrderOption.BOTTOM) {
661            number += groups.length + 1;
662        }
663        else if (!isStatic && option == ImportOrderOption.TOP) {
664            number += staticGroups.length + 1;
665        }
666        return number;
667    }
668
669    /**
670     * Finds out what group the specified import belongs to.
671     *
672     * @param patterns groups to check.
673     * @param name the import name to find.
674     * @return group number for given import name.
675     */
676    private static int getGroupNumber(Pattern[] patterns, String name) {
677        int bestIndex = patterns.length;
678        int bestEnd = -1;
679        int bestPos = Integer.MAX_VALUE;
680
681        // find out what group this belongs in
682        // loop over patterns and get index
683        for (int i = 0; i < patterns.length; i++) {
684            final Matcher matcher = patterns[i].matcher(name);
685            if (matcher.find()) {
686                if (matcher.start() < bestPos) {
687                    bestIndex = i;
688                    bestEnd = matcher.end();
689                    bestPos = matcher.start();
690                }
691                else if (matcher.start() == bestPos && matcher.end() > bestEnd) {
692                    bestIndex = i;
693                    bestEnd = matcher.end();
694                }
695            }
696        }
697        return bestIndex;
698    }
699
700    /**
701     * Compares two strings.
702     *
703     * @param string1
704     *            the first string
705     * @param string2
706     *            the second string
707     * @param caseSensitive
708     *            whether the comparison is case-sensitive
709     * @return the value {@code 0} if string1 is equal to string2; a value
710     *         less than {@code 0} if string1 is lexicographically less
711     *         than the string2; and a value greater than {@code 0} if
712     *         string1 is lexicographically greater than string2
713     */
714    private static int compare(String string1, String string2,
715            boolean caseSensitive) {
716        final int result;
717        if (caseSensitive) {
718            result = string1.compareTo(string2);
719        }
720        else {
721            result = string1.compareToIgnoreCase(string2);
722        }
723
724        return result;
725    }
726
727    /**
728     * Compiles the list of package groups and the order they should occur in the file.
729     *
730     * @param packageGroups a comma-separated list of package names/prefixes.
731     * @return array of compiled patterns.
732     * @throws IllegalArgumentException if any of the package groups are not valid.
733     */
734    private static Pattern[] compilePatterns(String... packageGroups) {
735        final Pattern[] patterns = new Pattern[packageGroups.length];
736        for (int i = 0; i < packageGroups.length; i++) {
737            String pkg = packageGroups[i];
738            final Pattern grp;
739
740            // if the pkg name is the wildcard, make it match zero chars
741            // from any name, so it will always be used as last resort.
742            if (WILDCARD_GROUP_NAME.equals(pkg)) {
743                // matches any package
744                grp = Pattern.compile("");
745            }
746            else if (pkg.startsWith(FORWARD_SLASH)) {
747                if (!pkg.endsWith(FORWARD_SLASH)) {
748                    throw new IllegalArgumentException("Invalid group: " + pkg);
749                }
750                pkg = pkg.substring(1, pkg.length() - 1);
751                grp = Pattern.compile(pkg);
752            }
753            else {
754                final StringBuilder pkgBuilder = new StringBuilder(pkg);
755                if (!pkg.endsWith(".")) {
756                    pkgBuilder.append('.');
757                }
758                grp = Pattern.compile("^" + Pattern.quote(pkgBuilder.toString()));
759            }
760
761            patterns[i] = grp;
762        }
763        return patterns;
764    }
765
766}