|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| Sort.java | 55,6% | 51,9% | 50% | 52,4% |
|
||||||||||||||
| 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;
|
|
| 10 |
import java.lang.reflect.Method;
|
|
| 11 |
import java.util.Collections;
|
|
| 12 |
import java.util.Comparator;
|
|
| 13 |
import java.util.Iterator;
|
|
| 14 |
import java.util.Vector;
|
|
| 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 | 0 |
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 | 24 |
public static Iterator getChildrenByClass(Iterator iterator, Class clazz) |
| 36 |
{
|
|
| 37 | 24 |
Object object; |
| 38 | 24 |
Vector vector = new Vector();
|
| 39 |
|
|
| 40 | 24 |
while (iterator.hasNext())
|
| 41 |
{
|
|
| 42 |
// Check if the object is instance of class
|
|
| 43 | ? |
if (clazz.isAssignableFrom((object = iterator.next()).getClass()))
|
| 44 |
{
|
|
| 45 | 112 |
vector.addElement(object); |
| 46 |
} |
|
| 47 |
} |
|
| 48 |
|
|
| 49 |
// Return the result
|
|
| 50 | 24 |
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 | 8 |
public static Iterator sortByClass(Iterator iterator) |
| 61 |
{
|
|
| 62 |
// Create a vector from the iterator
|
|
| 63 | 8 |
Vector vector = new Vector();
|
| 64 | 8 |
while (iterator.hasNext())
|
| 65 |
{
|
|
| 66 | 64 |
vector.addElement(iterator.next()); |
| 67 |
} |
|
| 68 |
|
|
| 69 |
// Apply the comparator
|
|
| 70 | 8 |
Collections.sort(vector, |
| 71 |
new Comparator()
|
|
| 72 |
{
|
|
| 73 |
// Compare by class name
|
|
| 74 | 136 |
public int compare(Object o1, Object o2) |
| 75 |
{
|
|
| 76 | 136 |
return o1.getClass().getName().compareTo(o2.getClass().getName());
|
| 77 |
} |
|
| 78 |
|
|
| 79 |
|
|
| 80 | 0 |
public boolean equals(Object obj) |
| 81 |
{
|
|
| 82 | 0 |
return true; |
| 83 |
// Ever called?
|
|
| 84 |
} |
|
| 85 |
}); |
|
| 86 |
|
|
| 87 |
// Return the result
|
|
| 88 | 8 |
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 | 8 |
public static Iterator sortByClassAndName(Iterator iterator) |
| 99 |
{
|
|
| 100 |
// Create a vector from the iterator
|
|
| 101 | 8 |
Vector vector = new Vector();
|
| 102 | 8 |
while (iterator.hasNext())
|
| 103 |
{
|
|
| 104 | 64 |
vector.addElement(iterator.next()); |
| 105 |
} |
|
| 106 |
|
|
| 107 |
// Apply the comparator
|
|
| 108 | 8 |
Collections.sort(vector, |
| 109 |
new Comparator()
|
|
| 110 |
{
|
|
| 111 | 136 |
public int compare(Object o1, Object o2) |
| 112 |
{
|
|
| 113 | 136 |
int ret = o1.getClass().getName().compareTo(o2.getClass().getName());
|
| 114 | 136 |
if (ret == 0)
|
| 115 |
{
|
|
| 116 | 48 |
ret = o1.toString().compareTo(o2.toString()); |
| 117 |
} |
|
| 118 | 136 |
return ret;
|
| 119 |
} |
|
| 120 |
|
|
| 121 |
|
|
| 122 | 0 |
public boolean equals(Object obj) |
| 123 |
{
|
|
| 124 | 0 |
return true; |
| 125 |
// Ever called?
|
|
| 126 |
} |
|
| 127 |
}); |
|
| 128 |
|
|
| 129 |
// Return the result
|
|
| 130 | 8 |
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 | 8 |
public static Iterator sortByName(Iterator iterator) |
| 141 |
{
|
|
| 142 |
// Create a vector from the iterator
|
|
| 143 | 8 |
Vector vector = new Vector();
|
| 144 | 8 |
while (iterator.hasNext())
|
| 145 |
{
|
|
| 146 | 64 |
vector.addElement(iterator.next()); |
| 147 |
} |
|
| 148 |
|
|
| 149 |
// Apply the comparator
|
|
| 150 | 8 |
Collections.sort(vector, |
| 151 |
new Comparator()
|
|
| 152 |
{
|
|
| 153 | 136 |
public int compare(Object o1, Object o2) |
| 154 |
{
|
|
| 155 | 136 |
return o1.toString().compareTo(o2.toString());
|
| 156 |
} |
|
| 157 |
|
|
| 158 |
|
|
| 159 | 0 |
public boolean equals(Object obj) |
| 160 |
{
|
|
| 161 | 0 |
return true; |
| 162 |
// Ever called?
|
|
| 163 |
} |
|
| 164 |
}); |
|
| 165 |
|
|
| 166 |
// Return the result
|
|
| 167 | 8 |
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 | 0 |
public static Iterator sortByProperty(Iterator iterator, Class clazz, String propertyName) |
| 180 |
{
|
|
| 181 |
// Create a vector from the iterator
|
|
| 182 | 0 |
Vector vector = new Vector();
|
| 183 | 0 |
while (iterator.hasNext())
|
| 184 |
{
|
|
| 185 | 0 |
vector.addElement(iterator.next()); |
| 186 |
} |
|
| 187 |
|
|
| 188 | 0 |
try
|
| 189 |
{
|
|
| 190 |
// Create the descriptor of the getter
|
|
| 191 | 0 |
final PropertyDescriptor pd = new PropertyDescriptor(propertyName, clazz);
|
| 192 | 0 |
final Method m = pd.getReadMethod(); |
| 193 |
|
|
| 194 |
// Apply the comparator
|
|
| 195 | 0 |
Collections.sort(vector, |
| 196 |
new Comparator()
|
|
| 197 |
{
|
|
| 198 | 0 |
public int compare(Object o1, Object o2) |
| 199 |
{
|
|
| 200 | 0 |
Comparable val1; |
| 201 | 0 |
Comparable val2; |
| 202 |
|
|
| 203 | 0 |
try
|
| 204 |
{
|
|
| 205 | 0 |
val1 = (Comparable) m.invoke(o1, new Object[0]);
|
| 206 | 0 |
val2 = (Comparable) m.invoke(o2, new Object[0]);
|
| 207 |
} |
|
| 208 |
catch (Exception e)
|
|
| 209 |
{
|
|
| 210 | 0 |
return 0;
|
| 211 |
} |
|
| 212 |
|
|
| 213 | 0 |
if (val1 == null) |
| 214 |
{
|
|
| 215 | 0 |
return -1;
|
| 216 |
} |
|
| 217 | 0 |
if (val2 == null) |
| 218 |
{
|
|
| 219 | 0 |
return 1;
|
| 220 |
} |
|
| 221 |
|
|
| 222 | 0 |
try
|
| 223 |
{
|
|
| 224 | 0 |
return val1.compareTo(val2);
|
| 225 |
} |
|
| 226 |
catch (Exception e)
|
|
| 227 |
{
|
|
| 228 |
// Ignore it
|
|
| 229 |
} |
|
| 230 | 0 |
return 0;
|
| 231 |
} |
|
| 232 |
|
|
| 233 |
|
|
| 234 | 0 |
public boolean equals(Object obj) |
| 235 |
{
|
|
| 236 | 0 |
return true; |
| 237 |
// Ever called?
|
|
| 238 |
} |
|
| 239 |
}); |
|
| 240 |
} |
|
| 241 |
catch (Exception e)
|
|
| 242 |
{
|
|
| 243 |
// Ignore it
|
|
| 244 |
} |
|
| 245 |
|
|
| 246 |
// Return the result
|
|
| 247 | 0 |
return vector.iterator();
|
| 248 |
} |
|
| 249 |
} |
|
| 250 |
|
|
| 251 |
|
|
||||||||||