Debian Codegraph @

net/sf/saxon/expr/sort/CollatingAtomicComparer.java

raw symbols SCIP
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 * An AtomicComparer used for comparing strings, untypedAtomic values, and URIs using a collation.
16 * A CollatingAtomicComparer is used when it is known in advance that both operands will be of these
17 * types. This enables all conversions and promotions to be bypassed: the string values of both operands
18 * are simply extracted and passed to the collator for comparison.
19 *
20 * @author Michael H. Kay
21 */
22
23 public class CollatingAtomicComparer implements AtomicComparer {
24
25 private StringCollator collator;
26
27 /**
28 * Create an GenericAtomicComparer
29 *
30 * @param collator the collation to be used. If the method is called at compile time, this should
31 * be a SimpleCollation so that it can be cloned at run-time.
32 */
33
34 public CollatingAtomicComparer(/*@Nullable*/ StringCollator collator) {
35
36 if (collator == null) {
37 this.collator = CodepointCollator.getInstance();
38 } else {
39 this.collator = collator;
40 }
41 }
42
43
44 public StringCollator getCollator() {
45 return collator;
46 }
47
48 /**
49 * Supply the dynamic context in case this is needed for the comparison
50 *
51 * @param context the dynamic evaluation context
52 * @return either the original AtomicComparer, or a new AtomicComparer in which the context
53 * is known. The original AtomicComparer is not modified
54 */
55
56 public AtomicComparer provideContext(XPathContext context) {
57 return this;
58 }
59
60
61 /**
62 * Compare two AtomicValue objects according to the rules for their data type. UntypedAtomic
63 * values are compared as if they were strings; if different semantics are wanted, the conversion
64 * must be done by the caller.
65 *
66 * @param a the first object to be compared. It is intended that this should be an instance
67 * of AtomicValue, though this restriction is not enforced. If it is a StringValue, the
68 * collator is used to compare the values, otherwise the value must implement the java.util.Comparable
69 * interface.
70 * @param b the second object to be compared. This must be comparable with the first object: for
71 * example, if one is a string, they must both be strings.
72 * @return <0 if a<b, 0 if a=b, >0 if a>b
73 * @throws ClassCastException if the objects are not comparable
74 */
75
76 public int compareAtomicValues(AtomicValue a, AtomicValue b) {
77 if (a == null) {
78 if (b == null) {
79 return 0;
80 } else {
81 return -1;
82 }
83 } else if (b == null) {
84 return +1;
85 }
86
87 return collator.compareStrings(a.getStringValue(), b.getStringValue());
88 }
89
90 /**
91 * Compare two AtomicValue objects for equality according to the rules for their data type. UntypedAtomic
92 * values are compared by converting to the type of the other operand.
93 *
94 * @param a the first object to be compared. It is intended that this should be an instance
95 * of AtomicValue, though this restriction is not enforced. If it is a StringValue, the
96 * collator is used to compare the values, otherwise the value must implement the equals() method.
97 * @param b the second object to be compared. This must be comparable with the first object: for
98 * example, if one is a string, they must both be strings.
99 * @return true if the values are equal, false if not
100 * @throws ClassCastException if the objects are not comparable
101 */
102
103 public boolean comparesEqual(AtomicValue a, AtomicValue b) {
104 return compareAtomicValues(a, b) == 0;
105 }
106
107 /**
108 * Create a string representation of this AtomicComparer that can be saved in a compiled
109 * package and used to reconstitute the AtomicComparer when the package is reloaded
110 *
111 * @return a string representation of the AtomicComparer
112 */
113 public String save() {
114 return "CAC|" + getCollator().getCollationURI();
115 }
116
117 @Override
118 public int hashCode() {
119 return collator.hashCode();
120 }
121
122 @Override
123 public boolean equals(Object obj) {
124 return obj instanceof CollatingAtomicComparer && collator.equals(((CollatingAtomicComparer)obj).collator);
125 }
126 }
127