| 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 atomic values where (a) equals is defined, and is implemented |
| 17 | * using the Java equals() method, and (b) ordering is not defined, and results in a dynamic error. |
| 18 | */ |
| 19 | public class EqualityComparer implements AtomicComparer { |
| 20 | |
| 21 | public static EqualityComparer THE_INSTANCE = new EqualityComparer(); |
| 22 | |
| 23 | /** |
| 24 | * Get the singleton instance of this class |
| 25 | * |
| 26 | * @return the singleton instance of this class |
| 27 | */ |
| 28 | |
| 29 | public static EqualityComparer getInstance() { |
| 30 | return THE_INSTANCE; |
| 31 | } |
| 32 | |
| 33 | private EqualityComparer() { |
| 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 AtomicValue objects according to the rules for their data type. UntypedAtomic |
| 55 | * values are compared as if they were strings; if different semantics are wanted, the conversion |
| 56 | * must be done by the caller. |
| 57 | * |
| 58 | * @param a the first object to be compared. It is intended that this should be an instance |
| 59 | * of AtomicValue, though this restriction is not enforced. If it is a StringValue, the |
| 60 | * collator is used to compare the values, otherwise the value must implement the java.util.Comparable |
| 61 | * interface. |
| 62 | * @param b the second object to be compared. This must be comparable with the first object: for |
| 63 | * example, if one is a string, they must both be strings. |
| 64 | * @return <0 if a<b, 0 if a=b, >0 if a>b |
| 65 | * @throws ClassCastException if the objects are not comparable |
| 66 | */ |
| 67 | |
| 68 | public int compareAtomicValues(AtomicValue a, AtomicValue b) { |
| 69 | throw new ClassCastException("Values are not comparable"); |
| 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. This must be an AtomicValue and it must implement |
| 77 | * equals() with context-free XPath comparison semantics |
| 78 | * @param b the second object to be compared. This must be an AtomicValue and it must implement |
| 79 | * equals() with context-free XPath comparison semantics |
| 80 | * @return true if the values are equal, false if not |
| 81 | * @throws ClassCastException if the objects are not comparable |
| 82 | */ |
| 83 | |
| 84 | public boolean comparesEqual(AtomicValue a, AtomicValue b) { |
| 85 | return a.equals(b); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Create a string representation of this AtomicComparer that can be saved in a compiled |
| 90 | * package and used to reconstitute the AtomicComparer when the package is reloaded |
| 91 | * |
| 92 | * @return a string representation of the AtomicComparer |
| 93 | */ |
| 94 | public String save() { |
| 95 | return "EQC"; |
| 96 | } |
| 97 | } |
| 98 |