1 22 package org.jboss.deployment.scanner; 23 24 import org.jboss.deployment.DefaultDeploymentSorter; 25 26 import java.util.Comparator ; 27 import java.net.URL ; 28 29 38 public class AlphaNumericDeploymentSorter implements Comparator , DefaultDeploymentSorter 39 { 40 41 private PrefixDeploymentSorter sorter = new PrefixDeploymentSorter(); 42 43 public String [] getSuffixOrder() 44 { 45 return sorter.getSuffixOrder(); 46 } 47 48 public void setSuffixOrder(String [] suffixOrder) 49 { 50 sorter.setSuffixOrder(suffixOrder); 51 } 52 53 public int compare(Object o1, Object o2) 54 { 55 int comp = sorter.compare(o1, o2); 56 57 return comp == 0 ? alphaCompare(o1, o2) : comp; 58 } 59 60 private String convertURLToString(URL url) 61 { 62 String path = url.getPath(); 63 int nameEnd = path.length() - 1; 64 if (nameEnd <= 0) { 65 return ""; 66 } 67 68 if (path.charAt(nameEnd) == '/') { 70 nameEnd--; 71 } 72 73 int nameStart = path.lastIndexOf('/', nameEnd) + 1; 75 76 return path.substring(nameStart); 77 78 } 79 80 public int alphaCompare(Object o1, Object o2) 81 { 82 String s1 = convertURLToString((URL )o1); 83 boolean s1IsDigit = Character.isDigit(s1.charAt(0)); 84 String s2 = convertURLToString((URL )o2); 85 boolean s2IsDigit = Character.isDigit(s2.charAt(0)); 86 87 if(s1IsDigit && !s2IsDigit) 88 { 89 return 1; } 91 else if(!s1IsDigit && s2IsDigit) 92 { 93 return -1; } 95 if(s1IsDigit && s2IsDigit) { 97 int num1 = getNumericPrefix(s1); 98 int num2 = getNumericPrefix(s2); 99 int diff = num1 - num2; 100 if(diff != 0) { 102 return diff; 103 } 104 else { 106 String s1Suf = getAlphaSuffix(s1); 107 String s2Sef = getAlphaSuffix(s2); 108 return s1Suf.compareToIgnoreCase(s2Sef); 109 } 110 } 111 else { 113 return s1.compareToIgnoreCase(s2); 114 } 115 } 116 117 private String getAlphaSuffix(String s1) 118 { 119 int x = 0; 120 while(Character.isDigit(s1.charAt(x++))); 121 return s1.substring(x); 122 } 123 124 private int getNumericPrefix(String s1) 125 { 126 int x = 0; 127 String numS1 = null; 128 while(Character.isDigit(s1.charAt(x++))) 129 { 130 numS1 = s1.substring(0, x); 131 } 132 int number = Integer.parseInt(numS1); 133 return number; 134 } 135 136 137 } 138 | Popular Tags |