| 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.value.AtomicValue; |
| 13 | |
| 14 | |
| 15 | /** |
| 16 | * A comparer for comparing two "ordinary" atomic values, where the values implement the Comparable |
| 17 | * interface and the equals() method with the appropriate XPath semantics. This rules out use of |
| 18 | * collations, conversion of untyped atomic values, and context dependencies such as implicit timezone. |
| 19 | */ |
| 20 | public class ComparableAtomicValueComparer implements AtomicComparer { |
| 21 | |
| 22 | private static ComparableAtomicValueComparer THE_INSTANCE = new ComparableAtomicValueComparer(); |
| 23 | |
| 24 | /** |
| 25 | * Get the singleton instance of this class |
| 26 | * |
| 27 | * @return the singleton instance of this class |
| 28 | */ |
| 29 | |
| 30 | public static ComparableAtomicValueComparer getInstance() { |
| 31 | return THE_INSTANCE; |
| 32 | } |
| 33 | |
| 34 | protected ComparableAtomicValueComparer() { |
| 35 | } |
| 36 | |
| 37 | /*@Nullable*/ |
| 38 | public StringCollator getCollator() { |
| 39 | return null; |
| 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 | */ |
| 49 | |
| 50 | public AtomicComparer provideContext(XPathContext context) { |
| 51 | return this; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Compare two AtomicValue objects according to the rules for their data type. UntypedAtomic |
| 56 | * values are compared as if they were strings; if different semantics are wanted, the conversion |
| 57 | * must be done by the caller. |
| 58 | * |
| 59 | * @param a the first object to be compared. This must be an AtomicValue and it must implement |
| 60 | * Comparable with context-free XPath comparison semantics |
| 61 | * @param b the second object to be compared. This must be an AtomicValue and it must implement |
| 62 | * Comparable with context-free XPath comparison semantics |
| 63 | * @return <0 if a<b, 0 if a=b, >0 if a>b |
| 64 | * @throws ClassCastException if the objects are not comparable |
| 65 | */ |
| 66 | |
| 67 | public int compareAtomicValues(AtomicValue a, AtomicValue b) { |
| 68 | if (a == null) { |
| 69 | return (b == null ? 0 : -1); |
| 70 | } else if (b == null) { |
| 71 | return +1; |
| 72 | } |
| 73 | return ((Comparable) a).compareTo(b); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Compare two AtomicValue objects for equality according to the rules for their data type. UntypedAtomic |
| 78 | * values are compared by converting to the type of the other operand. |
| 79 | * |
| 80 | * @param a the first object to be compared. This must be an AtomicValue and it must implement |
| 81 | * equals() with context-free XPath comparison semantics |
| 82 | * @param b the second object to be compared. This must be an AtomicValue and it must implement |
| 83 | * equals() with context-free XPath comparison semantics |
| 84 | * @return true if the values are equal, false if not |
| 85 | * @throws ClassCastException if the objects are not comparable |
| 86 | */ |
| 87 | |
| 88 | public boolean comparesEqual(AtomicValue a, AtomicValue b) { |
| 89 | return a.equals(b); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Create a string representation of this AtomicComparer that can be saved in a compiled |
| 94 | * package and used to reconstitute the AtomicComparer when the package is reloaded |
| 95 | * |
| 96 | * @return a string representation of the AtomicComparer |
| 97 | */ |
| 98 | public String save() { |
| 99 | return "CAVC"; |
| 100 | } |
| 101 | } |