| 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.trans.NoDynamicContextException; |
| 13 | import net.sf.saxon.value.AtomicValue; |
| 14 | |
| 15 | /** |
| 16 | * A Comparer that modifies a base comparer by sorting empty key values and NaN values last (greatest), |
| 17 | * as opposed to the default which sorts them first. |
| 18 | */ |
| 19 | |
| 20 | public class EmptyGreatestComparer implements AtomicComparer, java.io.Serializable { |
| 21 | |
| 22 | private AtomicComparer baseComparer; |
| 23 | |
| 24 | /** |
| 25 | * Create an EmptyGreatestComparer |
| 26 | * |
| 27 | * @param baseComparer the comparer used to compare non-empty values (which typically sorts empty |
| 28 | * as least) |
| 29 | */ |
| 30 | |
| 31 | public EmptyGreatestComparer(AtomicComparer baseComparer) { |
| 32 | this.baseComparer = baseComparer; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Get the underlying comparer (which compares empty least) |
| 37 | * |
| 38 | * @return the base comparer |
| 39 | */ |
| 40 | |
| 41 | public AtomicComparer getBaseComparer() { |
| 42 | return baseComparer; |
| 43 | } |
| 44 | |
| 45 | public StringCollator getCollator() { |
| 46 | return baseComparer.getCollator(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Supply the dynamic context in case this is needed for the comparison |
| 51 | * |
| 52 | * @param context the dynamic evaluation context |
| 53 | * @return either the original AtomicComparer, or a new AtomicComparer in which the context |
| 54 | * is known. The original AtomicComparer is not modified |
| 55 | */ |
| 56 | |
| 57 | public AtomicComparer provideContext(XPathContext context) { |
| 58 | AtomicComparer newBase = baseComparer.provideContext(context); |
| 59 | if (newBase != baseComparer) { |
| 60 | return new EmptyGreatestComparer(newBase); |
| 61 | } else { |
| 62 | return this; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Compare two AtomicValue objects according to the rules for their data type. UntypedAtomic |
| 68 | * values are compared as if they were strings; if different semantics are wanted, the conversion |
| 69 | * must be done by the caller. |
| 70 | * |
| 71 | * @param a the first object to be compared. It is intended that this should normally be an instance |
| 72 | * of AtomicValue, though this restriction is not enforced. If it is a StringValue, the |
| 73 | * collator is used to compare the values, otherwise the value must implement the java.util.Comparable |
| 74 | * interface. |
| 75 | * @param b the second object to be compared. This must be comparable with the first object: for |
| 76 | * example, if one is a string, they must both be strings. |
| 77 | * @return <0 if a<b, 0 if a=b, >0 if a>b |
| 78 | * @throws ClassCastException if the objects are not comparable |
| 79 | */ |
| 80 | |
| 81 | public int compareAtomicValues(/*@Nullable*/ AtomicValue a, AtomicValue b) throws NoDynamicContextException { |
| 82 | if (a == null) { |
| 83 | if (b == null) { |
| 84 | return 0; |
| 85 | } else { |
| 86 | return +1; |
| 87 | } |
| 88 | } else if (b == null) { |
| 89 | return -1; |
| 90 | } |
| 91 | |
| 92 | if (a.isNaN()) { |
| 93 | return b.isNaN() ? 0 : +1; |
| 94 | } else if (b.isNaN()) { |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | return baseComparer.compareAtomicValues(a, b); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Compare two AtomicValue objects for equality according to the rules for their data type. UntypedAtomic |
| 103 | * values are compared by converting to the type of the other operand. |
| 104 | * |
| 105 | * @param a the first object to be compared. |
| 106 | * @param b the second object to be compared. |
| 107 | * @return true if the values are equal, false if not |
| 108 | * @throws ClassCastException if the objects are not comparable |
| 109 | */ |
| 110 | |
| 111 | public boolean comparesEqual(AtomicValue a, AtomicValue b) throws NoDynamicContextException { |
| 112 | return (a == null && b == null) || baseComparer.comparesEqual(a, b); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Create a string representation of this AtomicComparer that can be saved in a compiled |
| 117 | * package and used to reconstitute the AtomicComparer when the package is reloaded |
| 118 | * |
| 119 | * @return a string representation of the AtomicComparer |
| 120 | */ |
| 121 | public String save() { |
| 122 | return "EG|" + baseComparer.save(); |
| 123 | } |
| 124 | } |
| 125 |