| 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 used for comparing descending keys. This simply returns the inverse of the result |
| 17 | * delivered by the base comparer. |
| 18 | */ |
| 19 | |
| 20 | public class DescendingComparer implements AtomicComparer, java.io.Serializable { |
| 21 | |
| 22 | private AtomicComparer baseComparer; |
| 23 | |
| 24 | public DescendingComparer(AtomicComparer base) { |
| 25 | baseComparer = base; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Get the underlying (ascending) comparer |
| 30 | * |
| 31 | * @return the underlying (ascending) comparer |
| 32 | */ |
| 33 | |
| 34 | public AtomicComparer getBaseComparer() { |
| 35 | return baseComparer; |
| 36 | } |
| 37 | |
| 38 | public StringCollator getCollator() { |
| 39 | return baseComparer.getCollator(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Supply the dynamic context in case this is needed for the comparison |
| 44 | * |
| 45 | * @param context the dynamic evaluation context |
| 46 | * @return either the original AtomicComparer, or a new AtomicComparer in which the context |
| 47 | * is known. The original AtomicComparer is not modified |
| 48 | * @throws NoDynamicContextException if the context is an "early evaluation" (compile-time) context |
| 49 | */ |
| 50 | |
| 51 | /*@NotNull*/ |
| 52 | public AtomicComparer provideContext(XPathContext context) { |
| 53 | AtomicComparer newBase = baseComparer.provideContext(context); |
| 54 | if (newBase != baseComparer) { |
| 55 | return new DescendingComparer(newBase); |
| 56 | } else { |
| 57 | return this; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Compare two objects. |
| 63 | * |
| 64 | * @return <0 if a<b, 0 if a=b, >0 if a>b |
| 65 | * @throws ClassCastException if the objects are of the wrong type for this Comparer |
| 66 | */ |
| 67 | |
| 68 | public int compareAtomicValues(AtomicValue a, AtomicValue b) throws NoDynamicContextException { |
| 69 | return 0 - baseComparer.compareAtomicValues(a, b); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Compare two AtomicValue objects for equality according to the rules for their data type. UntypedAtomic |
| 74 | * values are compared by converting to the type of the other operand. |
| 75 | * |
| 76 | * @param a the first object to be compared. It is intended that this should be an instance |
| 77 | * of AtomicValue, though this restriction is not enforced. If it is a StringValue, the |
| 78 | * collator is used to compare the values, otherwise the value must implement the equals() method. |
| 79 | * @param b the second object to be compared. This must be comparable with the first object: for |
| 80 | * example, if one is a string, they must both be strings. |
| 81 | * @return true if the values are equal, false if not |
| 82 | * @throws ClassCastException if the objects are not comparable |
| 83 | */ |
| 84 | |
| 85 | public boolean comparesEqual(AtomicValue a, AtomicValue b) throws NoDynamicContextException { |
| 86 | return baseComparer.comparesEqual(a, b); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Create a string representation of this AtomicComparer that can be saved in a compiled |
| 91 | * package and used to reconstitute the AtomicComparer when the package is reloaded |
| 92 | * |
| 93 | * @return a string representation of the AtomicComparer |
| 94 | */ |
| 95 | public String save() { |
| 96 | return "DESC|" + baseComparer.save(); |
| 97 | } |
| 98 | } |
| 99 |