| 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 | import net.sf.saxon.value.StringValue; |
| 14 | |
| 15 | /** |
| 16 | * An AtomicComparer used for comparing strings, untypedAtomic values, and URIs using the Unicode codepoint |
| 17 | * collation. |
| 18 | * A CodepointCollatingComparer is used when it is known in advance that both operands will be of these |
| 19 | * types, and when the collation is the unicode codepoint collation. |
| 20 | * This enables all conversions and promotions to be bypassed: the string values of both operands |
| 21 | * are simply extracted and passed to the collator for comparison. |
| 22 | * <p>The difference between using this class and using the underlying CodepointCollator directly is that |
| 23 | * the compare() method in this class expects two instances of AtomicValue as its operands, whereas the |
| 24 | * underlying class expects two instances of java.lang.String. This class makes use of the extra information |
| 25 | * held in the wrapping StringValue object, specifically, the knowledge of whether the string contains |
| 26 | * surrogate pairs.</p> |
| 27 | * |
| 28 | * @author Michael H. Kay |
| 29 | */ |
| 30 | |
| 31 | public class CodepointCollatingComparer implements AtomicComparer { |
| 32 | |
| 33 | private static CodepointCollator collator = CodepointCollator.getInstance(); |
| 34 | |
| 35 | private static CodepointCollatingComparer THE_INSTANCE = new CodepointCollatingComparer(); |
| 36 | |
| 37 | /** |
| 38 | * Get the singular instance of this class |
| 39 | * |
| 40 | * @return the singleton instance |
| 41 | */ |
| 42 | |
| 43 | public static CodepointCollatingComparer getInstance() { |
| 44 | return THE_INSTANCE; |
| 45 | } |
| 46 | |
| 47 | private CodepointCollatingComparer() { |
| 48 | } |
| 49 | |
| 50 | public StringCollator getCollator() { |
| 51 | return collator; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Supply the dynamic context in case this is needed for the comparison |
| 56 | * |
| 57 | * @param context the dynamic evaluation context |
| 58 | * @return either the original AtomicComparer, or a new AtomicComparer in which the context |
| 59 | * is known. The original AtomicComparer is not modified |
| 60 | */ |
| 61 | |
| 62 | public AtomicComparer provideContext(XPathContext context) { |
| 63 | return this; |
| 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. This must be either be an instance |
| 72 | * of AtomicValue, or null to represent an empty sequence. Empty collates before non-empty. |
| 73 | * @param b the second object to be compared. This must be either be an instance |
| 74 | * of AtomicValue, or null to represent an empty sequence. |
| 75 | * @return <0 if a<b, 0 if a=b, >0 if a>b |
| 76 | * @throws ClassCastException if the objects are not comparable |
| 77 | */ |
| 78 | |
| 79 | public int compareAtomicValues(/*@Nullable*/ AtomicValue a, AtomicValue b) { |
| 80 | if (a == null) { |
| 81 | return (b == null ? 0 : -1); |
| 82 | } else if (b == null) { |
| 83 | return +1; |
| 84 | } |
| 85 | StringValue as = (StringValue) a; |
| 86 | StringValue bs = (StringValue) b; |
| 87 | //if (as.containsSurrogatePairs() || bs.containsSurrogatePairs()) { |
| 88 | return CodepointCollator.compareCS(as.getStringValueCS(), bs.getStringValueCS()); |
| 89 | //} else { |
| 90 | // optimize to use UTF-16 binary comparison |
| 91 | // return as.getStringValue().compareTo(bs.getStringValue()); |
| 92 | //} |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Compare two AtomicValue objects for equality. The values must be instances of xs:string or a type |
| 97 | * derived from xs:string. The method will also handle xs:untypedAtomic and xs:anyURI values. |
| 98 | * |
| 99 | * @param a the first object to be compared. |
| 100 | * @param b the second object to be compared. |
| 101 | * @return true if the values are equal, false if not |
| 102 | * @throws ClassCastException if either value is not xs:string or a subtype |
| 103 | */ |
| 104 | |
| 105 | public boolean comparesEqual(AtomicValue a, AtomicValue b) { |
| 106 | return ((StringValue)a).codepointEquals((StringValue)b); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Create a string representation of this AtomicComparer that can be saved in a compiled |
| 111 | * package and used to reconstitute the AtomicComparer when the package is reloaded |
| 112 | * |
| 113 | * @return a string representation of the AtomicComparer |
| 114 | */ |
| 115 | public String save() { |
| 116 | return "CCC"; |
| 117 | } |
| 118 | } |
| 119 |