KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > completion > CompletionItemComparator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.completion;
21
22 import java.util.Comparator JavaDoc;
23 import org.netbeans.spi.editor.completion.CompletionItem;
24 import org.netbeans.spi.editor.completion.CompletionResultSet;
25
26 /**
27  * Comparator for completion items either by sort priority or by sort text.
28  *
29  * @author Dusan Balek, Miloslav Metelka
30  */

31
32 public class CompletionItemComparator implements Comparator JavaDoc<CompletionItem> {
33
34     public static final Comparator JavaDoc<CompletionItem> BY_PRIORITY = new CompletionItemComparator(true);
35
36     public static final Comparator JavaDoc<CompletionItem> ALPHABETICAL = new CompletionItemComparator(false);
37     
38     private final boolean byPriority;
39     
40     private CompletionItemComparator(boolean byPriority) {
41         this.byPriority = byPriority;
42     }
43     
44     public static final Comparator JavaDoc<CompletionItem> get(int sortType) {
45         if (sortType == CompletionResultSet.PRIORITY_SORT_TYPE)
46             return BY_PRIORITY;
47         if (sortType == CompletionResultSet.TEXT_SORT_TYPE)
48             return ALPHABETICAL;
49         throw new IllegalArgumentException JavaDoc();
50     }
51     
52     public int compare(CompletionItem i1, CompletionItem i2) {
53         if (i1 == i2)
54             return 0;
55         if (byPriority) {
56             int importanceDiff = i1.getSortPriority() - i2.getSortPriority();
57             if (importanceDiff != 0)
58                 return importanceDiff;
59             int alphabeticalDiff = compareText(i1.getSortText(), i2.getSortText());
60             if (alphabeticalDiff != 0)
61                 return alphabeticalDiff;
62         } else {
63             int alphabeticalDiff = compareText(i1.getSortText(), i2.getSortText());
64             if (alphabeticalDiff != 0)
65                 return alphabeticalDiff;
66             int importanceDiff = i1.getSortPriority() - i2.getSortPriority();
67             if (importanceDiff != 0)
68                 return importanceDiff;
69         }
70         return -1;
71     }
72     
73     private static int compareText(CharSequence JavaDoc text1, CharSequence JavaDoc text2) {
74         int len = Math.min(text1.length(), text2.length());
75         for (int i = 0; i < len; i++) {
76             char ch1 = text1.charAt(i);
77             char ch2 = text2.charAt(i);
78             if (ch1 != ch2) {
79                 return ch1 - ch2;
80             }
81         }
82         return text1.length() - text2.length();
83     }
84
85 }
86
Popular Tags