1 package net.sourceforge.importscrubber; 2 3 public class ImportStatement implements Comparable 4 { 5 private String fqClassName; 6 private String packageName; 8 private String className; 9 10 public ImportStatement(String fqClassName) 11 { 12 this.fqClassName = fqClassName; 13 int lastDot = fqClassName.lastIndexOf("."); 14 className = fqClassName.substring(lastDot + 1); 15 packageName = (lastDot == -1) ? null : fqClassName.substring(0, lastDot); 18 } 19 20 public String getFullyQualifiedClassName() 21 { 22 return fqClassName; 23 } 24 25 public int hashCode() 26 { 27 return fqClassName.hashCode(); 28 } 29 30 public String getPackage() 31 { 32 return packageName; 33 } 34 35 public String getClassName() 36 { 37 return className; 38 } 39 40 public boolean equals(Object 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 getFormattedStmt() 60 { 61 StringBuffer result = new StringBuffer (); 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 public int compareTo(Object in) 72 { 73 if(in == null) { 74 throw new IllegalArgumentException ("Can't compare this to null!"); 75 } 76 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 return fqClassName.toLowerCase().compareTo(otherImport.getFullyQualifiedClassName().toLowerCase()); 92 } 93 } 94 95 96 | Popular Tags |