KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > deployment > scanner > AlphaNumericDeploymentSorter


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.deployment.scanner;
23
24 import org.jboss.deployment.DefaultDeploymentSorter;
25
26 import java.util.Comparator JavaDoc;
27 import java.net.URL JavaDoc;
28
29 /**
30  * This is simialr to the PrefixDeploymentSorter in that it will order
31  * files that do not start with a numeric value before those that do.
32  * If two files begin with a number, will compare the numeric values.
33  * However, if the two files do not have numeric prefixes, will
34  * compare them using compareToIgnoreCase.
35  *
36  * @author <a HREF="mailto:tom@jboss.org">Tom Elrod</a>
37  */

38 public class AlphaNumericDeploymentSorter implements Comparator JavaDoc, DefaultDeploymentSorter
39 {
40
41    private PrefixDeploymentSorter sorter = new PrefixDeploymentSorter();
42
43    public String JavaDoc[] getSuffixOrder()
44    {
45       return sorter.getSuffixOrder();
46    }
47
48    public void setSuffixOrder(String JavaDoc[] suffixOrder)
49    {
50       sorter.setSuffixOrder(suffixOrder);
51    }
52
53    public int compare(Object JavaDoc o1, Object JavaDoc o2)
54    {
55       int comp = sorter.compare(o1, o2);
56
57       return comp == 0 ? alphaCompare(o1, o2) : comp;
58    }
59
60    private String JavaDoc convertURLToString(URL JavaDoc url)
61    {
62       String JavaDoc path = url.getPath();
63       int nameEnd = path.length() - 1;
64       if (nameEnd <= 0) {
65           return "";
66       }
67
68       // ignore a trailing '/'
69
if (path.charAt(nameEnd) == '/') {
70           nameEnd--;
71       }
72
73       // find the previous URL separator: '/'
74
int nameStart = path.lastIndexOf('/', nameEnd) + 1;
75
76       return path.substring(nameStart);
77
78    }
79
80    public int alphaCompare(Object JavaDoc o1, Object JavaDoc o2)
81    {
82       String JavaDoc s1 = convertURLToString((URL JavaDoc)o1);
83       boolean s1IsDigit = Character.isDigit(s1.charAt(0));
84       String JavaDoc s2 = convertURLToString((URL JavaDoc)o2);
85       boolean s2IsDigit = Character.isDigit(s2.charAt(0));
86
87       if(s1IsDigit && !s2IsDigit)
88       {
89          return 1; // o1 is greater than o2, since has number and o2 does not
90
}
91       else if(!s1IsDigit && s2IsDigit)
92       {
93          return -1; //o1 is less than o2, since does not have number and o2 does
94
}
95       if(s1IsDigit && s2IsDigit) // numeric comapre
96
{
97          int num1 = getNumericPrefix(s1);
98          int num2 = getNumericPrefix(s2);
99          int diff = num1 - num2;
100          if(diff != 0) // do not begin with same number
101
{
102             return diff;
103          }
104          else //numbers are the same, so have to compare rest of the string
105
{
106             String JavaDoc s1Suf = getAlphaSuffix(s1);
107             String JavaDoc s2Sef = getAlphaSuffix(s2);
108             return s1Suf.compareToIgnoreCase(s2Sef);
109          }
110       }
111       else // alpha compare
112
{
113          return s1.compareToIgnoreCase(s2);
114       }
115    }
116
117    private String JavaDoc getAlphaSuffix(String JavaDoc 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 JavaDoc s1)
125    {
126       int x = 0;
127       String JavaDoc 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