KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > beans > Sort


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.beans;
8
9 import java.beans.PropertyDescriptor JavaDoc;
10 import java.lang.reflect.Method JavaDoc;
11 import java.util.Collections JavaDoc;
12 import java.util.Comparator JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Vector JavaDoc;
15
16 /**
17  * Utility class that provides different kind of sort strategies for Iterator.
18  *
19  * @author Laurent Etiemble
20  * @version $Revision: 1.7 $
21  */

22 public class Sort
23 {
24    /** Constructor for the Sort object */
25    private Sort() { }
26
27
28    /**
29     * Extract the elements of the specified class from the Iterator.
30     *
31     * @param iterator Iterator to scan
32     * @param clazz Class used for the extraction
33     * @return The elements of the specified class
34     */

35    public static Iterator JavaDoc getChildrenByClass(Iterator JavaDoc iterator, Class JavaDoc clazz)
36    {
37       Object JavaDoc object;
38       Vector JavaDoc vector = new Vector JavaDoc();
39
40       while (iterator.hasNext())
41       {
42          // Check if the object is instance of class
43
if (clazz.isAssignableFrom((object = iterator.next()).getClass()))
44          {
45             vector.addElement(object);
46          }
47       }
48
49       // Return the result
50
return vector.iterator();
51    }
52
53
54    /**
55     * Sort the iterator by class name
56     *
57     * @param iterator Iterator to sort
58     * @return The iterator sorted
59     */

60    public static Iterator JavaDoc sortByClass(Iterator JavaDoc iterator)
61    {
62       // Create a vector from the iterator
63
Vector JavaDoc vector = new Vector JavaDoc();
64       while (iterator.hasNext())
65       {
66          vector.addElement(iterator.next());
67       }
68
69       // Apply the comparator
70
Collections.sort(vector,
71          new Comparator JavaDoc()
72          {
73             // Compare by class name
74
public int compare(Object JavaDoc o1, Object JavaDoc o2)
75             {
76                return o1.getClass().getName().compareTo(o2.getClass().getName());
77             }
78
79
80             public boolean equals(Object JavaDoc obj)
81             {
82                return true;
83                // Ever called?
84
}
85          });
86
87       // Return the result
88
return vector.iterator();
89    }
90
91
92    /**
93     * Sort the iterator by class name and by toString method
94     *
95     * @param iterator Iterator to sort
96     * @return The iterator sorted
97     */

98    public static Iterator JavaDoc sortByClassAndName(Iterator JavaDoc iterator)
99    {
100       // Create a vector from the iterator
101
Vector JavaDoc vector = new Vector JavaDoc();
102       while (iterator.hasNext())
103       {
104          vector.addElement(iterator.next());
105       }
106
107       // Apply the comparator
108
Collections.sort(vector,
109          new Comparator JavaDoc()
110          {
111             public int compare(Object JavaDoc o1, Object JavaDoc o2)
112             {
113                int ret = o1.getClass().getName().compareTo(o2.getClass().getName());
114                if (ret == 0)
115                {
116                   ret = o1.toString().compareTo(o2.toString());
117                }
118                return ret;
119             }
120
121
122             public boolean equals(Object JavaDoc obj)
123             {
124                return true;
125                // Ever called?
126
}
127          });
128
129       // Return the result
130
return vector.iterator();
131    }
132
133
134    /**
135     * Sort the iterator by toString method
136     *
137     * @param iterator Iterator to sort
138     * @return The iterator sorted
139     */

140    public static Iterator JavaDoc sortByName(Iterator JavaDoc iterator)
141    {
142       // Create a vector from the iterator
143
Vector JavaDoc vector = new Vector JavaDoc();
144       while (iterator.hasNext())
145       {
146          vector.addElement(iterator.next());
147       }
148
149       // Apply the comparator
150
Collections.sort(vector,
151          new Comparator JavaDoc()
152          {
153             public int compare(Object JavaDoc o1, Object JavaDoc o2)
154             {
155                return o1.toString().compareTo(o2.toString());
156             }
157
158
159             public boolean equals(Object JavaDoc obj)
160             {
161                return true;
162                // Ever called?
163
}
164          });
165
166       // Return the result
167
return vector.iterator();
168    }
169
170
171    /**
172     * Sort and iterator of JavaBeans by comparing the given property
173     *
174     * @param iterator Iterator to sort
175     * @param clazz The class of the JavaBean
176     * @param propertyName The property used for sorting
177     * @return The iterator sorted
178     */

179    public static Iterator JavaDoc sortByProperty(Iterator JavaDoc iterator, Class JavaDoc clazz, String JavaDoc propertyName)
180    {
181       // Create a vector from the iterator
182
Vector JavaDoc vector = new Vector JavaDoc();
183       while (iterator.hasNext())
184       {
185          vector.addElement(iterator.next());
186       }
187
188       try
189       {
190          // Create the descriptor of the getter
191
final PropertyDescriptor JavaDoc pd = new PropertyDescriptor JavaDoc(propertyName, clazz);
192          final Method JavaDoc m = pd.getReadMethod();
193
194          // Apply the comparator
195
Collections.sort(vector,
196             new Comparator JavaDoc()
197             {
198                public int compare(Object JavaDoc o1, Object JavaDoc o2)
199                {
200                   Comparable JavaDoc val1;
201                   Comparable JavaDoc val2;
202
203                   try
204                   {
205                      val1 = (Comparable JavaDoc) m.invoke(o1, new Object JavaDoc[0]);
206                      val2 = (Comparable JavaDoc) m.invoke(o2, new Object JavaDoc[0]);
207                   }
208                   catch (Exception JavaDoc e)
209                   {
210                      return 0;
211                   }
212
213                   if (val1 == null)
214                   {
215                      return -1;
216                   }
217                   if (val2 == null)
218                   {
219                      return 1;
220                   }
221
222                   try
223                   {
224                      return val1.compareTo(val2);
225                   }
226                   catch (Exception JavaDoc e)
227                   {
228                      // Ignore it
229
}
230                   return 0;
231                }
232
233
234                public boolean equals(Object JavaDoc obj)
235                {
236                   return true;
237                   // Ever called?
238
}
239             });
240       }
241       catch (Exception JavaDoc e)
242       {
243          // Ignore it
244
}
245
246       // Return the result
247
return vector.iterator();
248    }
249 }
250
251
Popular Tags