| 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.type.StringToDouble; |
| 13 | import net.sf.saxon.value.AtomicValue; |
| 14 | import net.sf.saxon.value.NumericValue; |
| 15 | |
| 16 | /** |
| 17 | * A Comparer used for comparing sort keys when data-type="number". The items to be |
| 18 | * compared are converted to numbers, and the numbers are then compared directly. NaN values |
| 19 | * compare equal to each other, and equal to an empty sequence, but less than anything else. |
| 20 | * <p>This class is used in XSLT only, so there is no need to handle XQuery's "empty least" vs |
| 21 | * "empty greatest" options.</p> |
| 22 | */ |
| 23 | |
| 24 | public class NumericComparer implements AtomicComparer { |
| 25 | |
| 26 | private static NumericComparer THE_INSTANCE = new NumericComparer(); |
| 27 | protected StringToDouble converter = StringToDouble.getInstance(); |
| 28 | |
| 29 | public static NumericComparer getInstance() { |
| 30 | return THE_INSTANCE; |
| 31 | } |
| 32 | |
| 33 | protected NumericComparer() { |
| 34 | } |
| 35 | |
| 36 | /*@Nullable*/ |
| 37 | public StringCollator getCollator() { |
| 38 | return null; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Supply the dynamic context in case this is needed for the comparison |
| 43 | * |
| 44 | * @param context the dynamic evaluation context |
| 45 | * @return either the original AtomicComparer, or a new AtomicComparer in which the context |
| 46 | * is known. The original AtomicComparer is not modified |
| 47 | */ |
| 48 | |
| 49 | public AtomicComparer provideContext(XPathContext context) { |
| 50 | return this; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Compare two Items by converting them to numbers and comparing the numeric values. If either |
| 55 | * value cannot be converted to a number, it is treated as NaN, and compares less that the other |
| 56 | * (two NaN values compare equal). |
| 57 | * |
| 58 | * @param a the first Item to be compared. |
| 59 | * @param b the second Item to be compared. |
| 60 | * @return <0 if a<b, 0 if a=b, >0 if a>b |
| 61 | * @throws ClassCastException if the objects are not Items |
| 62 | */ |
| 63 | |
| 64 | public int compareAtomicValues(AtomicValue a, AtomicValue b) { |
| 65 | double d1, d2; |
| 66 | |
| 67 | if (a instanceof NumericValue) { |
| 68 | d1 = ((NumericValue) a).getDoubleValue(); |
| 69 | } else if (a == null) { |
| 70 | d1 = Double.NaN; |
| 71 | } else { |
| 72 | try { |
| 73 | d1 = converter.stringToNumber(a.getStringValueCS()); |
| 74 | } catch (NumberFormatException err) { |
| 75 | d1 = Double.NaN; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if (b instanceof NumericValue) { |
| 80 | d2 = ((NumericValue) b).getDoubleValue(); |
| 81 | } else if (b == null) { |
| 82 | d2 = Double.NaN; |
| 83 | } else { |
| 84 | try { |
| 85 | d2 = converter.stringToNumber(b.getStringValueCS()); |
| 86 | } catch (NumberFormatException err) { |
| 87 | d2 = Double.NaN; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if (Double.isNaN(d1)) { |
| 92 | if (Double.isNaN(d2)) { |
| 93 | return 0; |
| 94 | } else { |
| 95 | return -1; |
| 96 | } |
| 97 | } |
| 98 | if (Double.isNaN(d2)) { |
| 99 | return +1; |
| 100 | } |
| 101 | if (d1 < d2) { |
| 102 | return -1; |
| 103 | } |
| 104 | if (d1 > d2) { |
| 105 | return +1; |
| 106 | } |
| 107 | return 0; |
| 108 | |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Compare two AtomicValue objects for equality according to the rules for their data type. UntypedAtomic |
| 113 | * values are compared by converting to the type of the other operand. |
| 114 | * |
| 115 | * @param a the first object to be compared. |
| 116 | * @param b the second object to be compared. |
| 117 | * @return true if the values are equal, false if not |
| 118 | * @throws ClassCastException if the objects are not comparable |
| 119 | */ |
| 120 | |
| 121 | public boolean comparesEqual(AtomicValue a, AtomicValue b) { |
| 122 | return compareAtomicValues(a, b) == 0; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Create a string representation of this AtomicComparer that can be saved in a compiled |
| 127 | * package and used to reconstitute the AtomicComparer when the package is reloaded |
| 128 | * |
| 129 | * @return a string representation of the AtomicComparer |
| 130 | */ |
| 131 | public String save() { |
| 132 | return "NC"; |
| 133 | } |
| 134 | } |
| 135 |