| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.expr.sort; |
| 9 | |
| 10 | import net.sf.saxon.expr.XPathContext; |
| 11 | import net.sf.saxon.lib.StringCollator; |
| 12 | import net.sf.saxon.om.StandardNames; |
| 13 | import net.sf.saxon.trans.NoDynamicContextException; |
| 14 | import net.sf.saxon.trans.XPathException; |
| 15 | import net.sf.saxon.type.Type; |
| 16 | import net.sf.saxon.value.AtomicValue; |
| 17 | import net.sf.saxon.value.QNameValue; |
| 18 | import net.sf.saxon.value.StringValue; |
| 19 | import net.sf.saxon.value.UntypedAtomicValue; |
| 20 | |
| 21 | /** |
| 22 | * An AtomicComparer used for comparing atomic values of arbitrary item types. It encapsulates |
| 23 | * a collator that is used when the values to be compared are strings. It also supports |
| 24 | * a separate method for testing equality of items, which can be used for data types that |
| 25 | * are not ordered. |
| 26 | * <p>The AtomicSortComparer is identical to the GenericAtomicComparer except for its handling |
| 27 | * of NaN: it treats NaN values as lower than any other value, and as equal to each other.</p> |
| 28 | * |
| 29 | * @author Michael H. Kay |
| 30 | */ |
| 31 | |
| 32 | public class AtomicSortComparer implements AtomicComparer { |
| 33 | |
| 34 | private StringCollator collator; |
| 35 | private transient XPathContext context; |
| 36 | private int itemType; |
| 37 | |
| 38 | /** |
| 39 | * Factory method to get an atomic comparer suitable for sorting or for grouping (operations in which |
| 40 | * NaN is considered equal to NaN) |
| 41 | * |
| 42 | * @param collator Collating comparer to be used when comparing strings. This argument may be null |
| 43 | * if the itemType excludes the possibility of comparing strings. If the method is called at compile |
| 44 | * time, this should be a SimpleCollation so that it can be cloned at run-time. |
| 45 | * @param itemType the primitive item type of the values to be compared |
| 46 | * @param context Dynamic context (may be an EarlyEvaluationContext) |
| 47 | * @return a suitable AtomicComparer |
| 48 | */ |
| 49 | |
| 50 | public static AtomicComparer makeSortComparer(StringCollator collator, int itemType, XPathContext context) { |
| 51 | switch (itemType) { |
| 52 | case StandardNames.XS_STRING: |
| 53 | case StandardNames.XS_UNTYPED_ATOMIC: |
| 54 | case StandardNames.XS_ANY_URI: |
| 55 | if (collator instanceof CodepointCollator) { |
| 56 | return CodepointCollatingComparer.getInstance(); |
| 57 | } else { |
| 58 | return new CollatingAtomicComparer(collator); |
| 59 | } |
| 60 | case StandardNames.XS_INTEGER: |
| 61 | case StandardNames.XS_DECIMAL: |
| 62 | return DecimalSortComparer.getDecimalSortComparerInstance(); |
| 63 | case StandardNames.XS_DOUBLE: |
| 64 | case StandardNames.XS_FLOAT: |
| 65 | case StandardNames.XS_NUMERIC: |
| 66 | return DoubleSortComparer.getInstance(); |
| 67 | case StandardNames.XS_DATE_TIME: |
| 68 | case StandardNames.XS_DATE: |
| 69 | case StandardNames.XS_TIME: |
| 70 | return new CalendarValueComparer(context); |
| 71 | default: |
| 72 | // use the general-purpose comparer that handles all types |
| 73 | return new AtomicSortComparer(collator, itemType, context); |
| 74 | } |
| 75 | |
| 76 | } |
| 77 | |
| 78 | protected AtomicSortComparer(/*@Nullable*/ StringCollator collator, int itemType, XPathContext context) { |
| 79 | this.collator = collator; |
| 80 | if (collator == null) { |
| 81 | this.collator = CodepointCollator.getInstance(); |
| 82 | } |
| 83 | this.context = context; |
| 84 | this.itemType = itemType; |
| 85 | } |
| 86 | |
| 87 | public StringCollator getCollator() { |
| 88 | return collator; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Supply the dynamic context in case this is needed for the comparison |
| 93 | * |
| 94 | * @param context the dynamic evaluation context |
| 95 | * @return either the original AtomicComparer, or a new AtomicComparer in which the context |
| 96 | * is known. The original AtomicComparer is not modified |
| 97 | */ |
| 98 | |
| 99 | public AtomicComparer provideContext(XPathContext context) { |
| 100 | return new AtomicSortComparer(collator, itemType, context); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Get the underlying StringCollator |
| 105 | * |
| 106 | * @return the underlying collator |
| 107 | */ |
| 108 | |
| 109 | public StringCollator getStringCollator() { |
| 110 | return collator; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Get the requested item type |
| 115 | * |
| 116 | * @return the item type |
| 117 | */ |
| 118 | |
| 119 | public int getItemType() { |
| 120 | return itemType; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Compare two AtomicValue objects according to the rules for their data type. UntypedAtomic |
| 125 | * values are compared as if they were strings; if different semantics are wanted, the conversion |
| 126 | * must be done by the caller. |
| 127 | * |
| 128 | * @param a the first object to be compared. It is intended that this should normally be an instance |
| 129 | * of AtomicValue, though this restriction is not enforced. If it is a StringValue, the |
| 130 | * collator is used to compare the values, otherwise the value must implement the java.util.Comparable |
| 131 | * interface. |
| 132 | * @param b the second object to be compared. This must be comparable with the first object: for |
| 133 | * example, if one is a string, they must both be strings. |
| 134 | * @return <0 if a<b, 0 if a=b, >0 if a>b |
| 135 | * @throws ClassCastException if the objects are not comparable |
| 136 | */ |
| 137 | |
| 138 | public int compareAtomicValues(AtomicValue a, AtomicValue b) throws NoDynamicContextException { |
| 139 | |
| 140 | if (a == null) { |
| 141 | if (b == null) { |
| 142 | return 0; |
| 143 | } else { |
| 144 | return -1; |
| 145 | } |
| 146 | } else if (b == null) { |
| 147 | return +1; |
| 148 | } |
| 149 | |
| 150 | // System.err.println("Comparing " + a.getClass() + "(" + a + ") with " + b.getClass() + "(" + b + ") using " + collator); |
| 151 | |
| 152 | // Delete the following five lines to fix bug 3450 |
| 153 | //if (a instanceof UntypedAtomicValue) { |
| 154 | // return ((UntypedAtomicValue) a).compareTo(b, collator, context); |
| 155 | //} else if (b instanceof UntypedAtomicValue) { |
| 156 | // return -((UntypedAtomicValue) b).compareTo(a, collator, context); |
| 157 | //} else |
| 158 | // End of fix for 3450 |
| 159 | |
| 160 | if (a.isNaN()) { |
| 161 | return b.isNaN() ? 0 : -1; |
| 162 | } else if (b.isNaN()) { |
| 163 | return +1; |
| 164 | } else if (a instanceof StringValue && b instanceof StringValue) { |
| 165 | if (collator instanceof CodepointCollator) { |
| 166 | return CodepointCollator.compareCS(a.getStringValueCS(), b.getStringValueCS()); |
| 167 | } else { |
| 168 | return collator.compareStrings(a.getStringValue(), b.getStringValue()); |
| 169 | } |
| 170 | } else { |
| 171 | int implicitTimezone = context.getImplicitTimezone(); |
| 172 | Comparable ac = (Comparable) a.getXPathComparable(true, collator, implicitTimezone); |
| 173 | Comparable bc = (Comparable) b.getXPathComparable(true, collator, implicitTimezone); |
| 174 | if (ac == null || bc == null) { |
| 175 | return compareNonComparables(a, b); |
| 176 | } else { |
| 177 | try { |
| 178 | return ac.compareTo(bc); |
| 179 | } catch (ClassCastException e) { |
| 180 | String message = "Cannot compare " + a.getPrimitiveType().getDisplayName() + |
| 181 | " with " + b.getPrimitiveType().getDisplayName(); |
| 182 | // Direct users to bug 3450 which explains a 2017 bug fix that may cause previously |
| 183 | // working applications to fail |
| 184 | if (a instanceof UntypedAtomicValue || b instanceof UntypedAtomicValue) { |
| 185 | message += ". Further information: see http://saxonica.plan.io/issues/3450"; |
| 186 | } |
| 187 | throw new ClassCastException(message); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Compare two values that are known to be non-comparable. In the base class this method |
| 195 | * throws a ClassCastException. In a subclass it is overridden to return |
| 196 | * {@link net.sf.saxon.om.SequenceTool#INDETERMINATE_ORDERING} |
| 197 | * @param a the first value to be compared |
| 198 | * @param b the second value to be compared |
| 199 | * @return the result of the comparison |
| 200 | * @throws ClassCastException if the two values are not comparable |
| 201 | */ |
| 202 | |
| 203 | protected int compareNonComparables(AtomicValue a, AtomicValue b) { |
| 204 | XPathException err = new XPathException("Values are not comparable (" + |
| 205 | Type.displayTypeName(a) + ", " + Type.displayTypeName(b) + ')', |
| 206 | "XPTY0004"); |
| 207 | throw new ComparisonException(err); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Compare two AtomicValue objects for equality according to the rules for their data type. UntypedAtomic |
| 212 | * values are compared by converting to the type of the other operand. |
| 213 | * |
| 214 | * @param a the first object to be compared. It is intended that this should be an instance |
| 215 | * of AtomicValue, though this restriction is not enforced. If it is a StringValue, the |
| 216 | * collator is used to compare the values, otherwise the value must implement the equals() method. |
| 217 | * @param b the second object to be compared. This must be comparable with the first object: for |
| 218 | * example, if one is a string, they must both be strings. |
| 219 | * @return true if the values are equal, false if not |
| 220 | * @throws ClassCastException if the objects are not comparable |
| 221 | */ |
| 222 | |
| 223 | public boolean comparesEqual(AtomicValue a, AtomicValue b) throws NoDynamicContextException { |
| 224 | return compareAtomicValues(a, b) == 0; |
| 225 | } |
| 226 | |
| 227 | public static AtomicMatchKey COLLATION_KEY_NaN = new AtomicMatchKey() { |
| 228 | public AtomicValue asAtomic() { |
| 229 | // The logic here is to choose a value that compares equal to itself but not equal to any other |
| 230 | // number. We use StructuredQName because it has a simple equals() method. |
| 231 | |
| 232 | return new QNameValue("saxon", "http://saxon.sf.net/collation-key", "NaN"); |
| 233 | } |
| 234 | }; |
| 235 | |
| 236 | /** |
| 237 | * Create a string representation of this AtomicComparer that can be saved in a compiled |
| 238 | * package and used to reconstitute the AtomicComparer when the package is reloaded |
| 239 | * |
| 240 | * @return a string representation of the AtomicComparer |
| 241 | */ |
| 242 | public String save() { |
| 243 | return "AtSC|" + itemType + "|" + getCollator().getCollationURI(); |
| 244 | } |
| 245 | } |
| 246 |