1 19 20 package org.apache.cayenne.query; 21 22 import java.io.PrintWriter ; 23 import java.io.Serializable ; 24 import java.io.StringWriter ; 25 import java.util.Collections ; 26 import java.util.Comparator ; 27 import java.util.List ; 28 29 import org.apache.commons.collections.ComparatorUtils; 30 import org.apache.cayenne.exp.Expression; 31 import org.apache.cayenne.util.ConversionUtil; 32 import org.apache.cayenne.util.Util; 33 import org.apache.cayenne.util.XMLEncoder; 34 import org.apache.cayenne.util.XMLSerializable; 35 36 45 public class Ordering implements Comparator , Serializable , XMLSerializable { 46 47 50 public static final boolean ASC = true; 51 52 55 public static final boolean DESC = false; 56 57 protected String sortSpecString; 58 protected transient Expression sortSpec; 59 protected boolean ascending; 60 protected boolean caseInsensitive; 61 62 68 public static void orderList(List objects, List orderings) { 69 Collections.sort(objects, ComparatorUtils.chainedComparator(orderings)); 70 } 71 72 public Ordering() { 73 } 74 75 public Ordering(String sortPathSpec, boolean ascending) { 76 this(sortPathSpec, ascending, false); 77 } 78 79 public Ordering(String sortPathSpec, boolean ascending, boolean caseInsensitive) { 80 setSortSpecString(sortPathSpec); 81 this.ascending = ascending; 82 this.caseInsensitive = caseInsensitive; 83 } 84 85 public Ordering(Expression sortExpression, boolean ascending) { 86 this(sortExpression, ascending, false); 87 } 88 89 public Ordering(Expression sortExpression, boolean ascending, boolean caseInsensitive) { 90 setSortSpec(sortExpression); 91 this.ascending = ascending; 92 this.caseInsensitive = caseInsensitive; 93 } 94 95 100 public void setSortSpecString(String sortSpecString) { 101 if (!Util.nullSafeEquals(this.sortSpecString, sortSpecString)) { 102 this.sortSpecString = sortSpecString; 103 this.sortSpec = null; 104 } 105 } 106 107 112 public String getSortSpecString() { 113 return sortSpecString; 114 } 115 116 117 public boolean isAscending() { 118 return ascending; 119 } 120 121 122 public void setAscending(boolean ascending) { 123 this.ascending = ascending; 124 } 125 126 127 public boolean isCaseInsensitive() { 128 return caseInsensitive; 129 } 130 131 132 public void setCaseInsensitive(boolean caseInsensitive) { 133 this.caseInsensitive = caseInsensitive; 134 } 135 136 139 public Expression getSortSpec() { 140 if (sortSpecString == null) { 141 return null; 142 } 143 144 if (sortSpec == null) { 146 sortSpec = Expression.fromString(sortSpecString); 147 } 148 149 return sortSpec; 150 } 151 152 155 public void setSortSpec(Expression sortSpec) { 156 this.sortSpec = sortSpec; 157 this.sortSpecString = (sortSpec != null) ? sortSpec.toString() : null; 158 } 159 160 166 public void orderList(List objects) { 167 Collections.sort(objects, this); 168 } 169 170 174 public int compare(Object o1, Object o2) { 175 Expression exp = getSortSpec(); 176 Object value1 = exp.evaluate(o1); 177 Object value2 = exp.evaluate(o2); 178 179 if (value1 == null) { 181 return (value2 == null) ? 0 : -1; 182 } 183 else if (value2 == null) { 184 return 1; 185 } 186 187 if (this.caseInsensitive) { 188 value1 = ConversionUtil.toUpperCase(value1); 191 value2 = ConversionUtil.toUpperCase(value2); 192 } 193 194 int compareResult = ConversionUtil.toComparable(value1).compareTo( 195 ConversionUtil.toComparable(value2)); 196 return (ascending) ? compareResult : -compareResult; 197 } 198 199 204 public void encodeAsXML(XMLEncoder encoder) { 205 encoder.print("<ordering"); 206 207 if (!ascending) { 208 encoder.print(" descending=\"true\""); 209 } 210 211 if (caseInsensitive) { 212 encoder.print(" ignore-case=\"true\""); 213 } 214 215 encoder.print(">"); 216 if (getSortSpec() != null) { 217 getSortSpec().encodeAsXML(encoder); 218 } 219 encoder.println("</ordering>"); 220 } 221 222 public String toString() { 223 StringWriter buffer = new StringWriter (); 224 PrintWriter pw = new PrintWriter (buffer); 225 XMLEncoder encoder = new XMLEncoder(pw); 226 encodeAsXML(encoder); 227 pw.close(); 228 buffer.flush(); 229 return buffer.toString(); 230 } 231 } 232 | Popular Tags |