1 56 package org.objectstyle.cayenne.query; 57 58 import java.io.PrintWriter ; 59 import java.io.Serializable ; 60 import java.io.StringWriter ; 61 import java.util.Collections ; 62 import java.util.Comparator ; 63 import java.util.List ; 64 65 import org.apache.commons.collections.ComparatorUtils; 66 import org.objectstyle.cayenne.exp.Expression; 67 import org.objectstyle.cayenne.util.ConversionUtil; 68 import org.objectstyle.cayenne.util.XMLEncoder; 69 import org.objectstyle.cayenne.util.XMLSerializable; 70 71 79 public class Ordering implements Comparator , Serializable , XMLSerializable { 80 81 84 public static final boolean ASC = true; 85 86 89 public static final boolean DESC = false; 90 91 protected Expression sortSpec; 92 protected boolean ascending; 93 protected boolean caseInsensitive; 94 95 101 public static void orderList(List objects, List orderings) { 102 Collections.sort(objects, ComparatorUtils.chainedComparator(orderings)); 103 } 104 105 public Ordering() { 106 } 107 108 public Ordering(String sortPathSpec, boolean ascending) { 109 this(sortPathSpec, ascending, false); 110 } 111 112 public Ordering(String sortPathSpec, boolean ascending, boolean caseInsensitive) { 113 setSortSpecString(sortPathSpec); 114 this.ascending = ascending; 115 this.caseInsensitive = caseInsensitive; 116 } 117 118 public Ordering(Expression sortExpression, boolean ascending) { 119 this(sortExpression, ascending, false); 120 } 121 122 public Ordering( 123 Expression sortExpression, 124 boolean ascending, 125 boolean caseInsensitive) { 126 setSortSpec(sortExpression); 127 this.ascending = ascending; 128 this.caseInsensitive = caseInsensitive; 129 } 130 131 136 public void setSortSpecString(String sortSpecString) { 137 this.sortSpec = 138 (sortSpecString != null) ? Expression.fromString(sortSpecString) : null; 139 } 140 141 146 public String getSortSpecString() { 147 return (sortSpec != null) ? sortSpec.toString() : null; 148 } 149 150 151 public boolean isAscending() { 152 return ascending; 153 } 154 155 156 public void setAscending(boolean ascending) { 157 this.ascending = ascending; 158 } 159 160 161 public boolean isCaseInsensitive() { 162 return caseInsensitive; 163 } 164 165 166 public void setCaseInsensitive(boolean caseInsensitive) { 167 this.caseInsensitive = caseInsensitive; 168 } 169 170 173 public Expression getSortSpec() { 174 return sortSpec; 175 } 176 177 180 public void setSortSpec(Expression sortSpec) { 181 this.sortSpec = sortSpec; 182 } 183 184 190 public void orderList(List objects) { 191 Collections.sort(objects, this); 192 } 193 194 198 public int compare(Object o1, Object o2) { 199 Object value1 = sortSpec.evaluate(o1); 200 Object value2 = sortSpec.evaluate(o2); 201 202 if (value1 == null) { 204 return (value2 == null) ? 0 : -1; 205 } 206 else if (value2 == null) { 207 return 1; 208 } 209 210 if (this.caseInsensitive) { 211 value1 = ConversionUtil.toUpperCase(value1); 213 value2 = ConversionUtil.toUpperCase(value2); 214 } 215 216 int compareResult = 217 ConversionUtil.toComparable(value1).compareTo( 218 ConversionUtil.toComparable(value2)); 219 return (ascending) ? compareResult : -compareResult; 220 } 221 222 227 public void encodeAsXML(XMLEncoder encoder) { 228 encoder.print("<ordering"); 229 230 if (!ascending) { 231 encoder.print(" descending=\"true\""); 232 } 233 234 if (caseInsensitive) { 235 encoder.print(" ignore-case=\"true\""); 236 } 237 238 encoder.print(">"); 239 if (sortSpec != null) { 240 sortSpec.encodeAsXML(encoder); 241 } 242 encoder.println("</ordering>"); 243 } 244 245 public String toString() { 246 StringWriter buffer = new StringWriter (); 247 PrintWriter pw = new PrintWriter (buffer); 248 XMLEncoder encoder = new XMLEncoder(pw); 249 encodeAsXML(encoder); 250 pw.close(); 251 buffer.flush(); 252 return buffer.toString(); 253 } 254 } 255 | Popular Tags |