KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > importscrubber > ImportStatement


1 package net.sourceforge.importscrubber;
2
3 public class ImportStatement implements Comparable JavaDoc
4 {
5     private String JavaDoc fqClassName;
6     // pre-compute these for efficiency
7
private String JavaDoc packageName;
8     private String JavaDoc className;
9
10     public ImportStatement(String JavaDoc fqClassName)
11     {
12         this.fqClassName = fqClassName;
13         int lastDot = fqClassName.lastIndexOf(".");
14         className = fqClassName.substring(lastDot + 1);
15         // ImportStatements.add() will check for null package.
16
// everyone else can assume it's non-null by the time they see it.
17
packageName = (lastDot == -1) ? null : fqClassName.substring(0, lastDot);
18     }
19
20     public String JavaDoc getFullyQualifiedClassName()
21     {
22         return fqClassName;
23     }
24
25     public int hashCode()
26     {
27         return fqClassName.hashCode();
28     }
29
30     public String JavaDoc getPackage()
31     {
32         return packageName;
33     }
34
35     public String JavaDoc getClassName()
36     {
37         return className;
38     }
39
40     public boolean equals(Object JavaDoc other)
41     {
42         if(!(other instanceof ImportStatement) || other == null) {
43             return false;
44         }
45         ImportStatement otherStmt = (ImportStatement)other;
46         return otherStmt.getFullyQualifiedClassName().equals(fqClassName);
47     }
48
49     public boolean isInStdJavaLibrary()
50     {
51         return fqClassName.startsWith("java.");
52     }
53
54     public boolean isInStdJavaExtensionLibrary()
55     {
56         return fqClassName.startsWith("javax.");
57     }
58
59     public StringBuffer JavaDoc getFormattedStmt()
60     {
61         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
62         result.append(ImportStatements.MARKER);
63         result.append(" ");
64         result.append(fqClassName);
65         result.append(";");
66         result.append(ImportScrubber.LINE_SEPARATOR);
67         return result;
68     }
69
70     // Comparable
71
public int compareTo(Object JavaDoc in)
72     {
73         if(in == null) {
74             throw new IllegalArgumentException JavaDoc("Can't compare this to null!");
75         }
76         // If they are the same then simple comparison will suffice. If not,
77
// we'd like to put something like java.awt.Toolkit before
78
// java.awt.print.Pageable which a simple comparison won't do.
79
ImportStatement otherImport = (ImportStatement)in;
80
81         if (!getPackage().equals(otherImport.getPackage())) {
82             if (getPackage().startsWith(otherImport.getPackage())) {
83                 return 1;
84
85             } else if (otherImport.getPackage().startsWith(getPackage())) {
86                 return -1;
87             }
88         }
89
90         // same package, so just do simple comparison
91
return fqClassName.toLowerCase().compareTo(otherImport.getFullyQualifiedClassName().toLowerCase());
92     }
93 }
94
95
96
Popular Tags