Debian Codegraph @

net/sf/saxon/expr/sort/GenericAtomicComparer.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.om.StandardNames;
13 import net.sf.saxon.trans.NoDynamicContextException;
14 import net.sf.saxon.trans.XPathException;
15 import net.sf.saxon.type.BuiltInAtomicType;
16 import net.sf.saxon.type.Type;
17 import net.sf.saxon.value.AtomicValue;
18 import net.sf.saxon.value.CalendarValue;
19 import net.sf.saxon.value.StringValue;
20
21 /**
22 * An AtomicComparer used for comparing atomic values of arbitrary item types. It encapsulates
23 * a Collator that is used when the values to be compared are strings. It also supports
24 * a separate method for testing equality of items, which can be used for data types that
25 * are not ordered.
26 *
27 * @author Michael H. Kay
28 */
29
30 public class GenericAtomicComparer implements AtomicComparer {
31
32 private StringCollator collator;
33 private transient XPathContext context;
34
35 /**
36 * Create an GenericAtomicComparer
37 *
38 * @param collator the collation to be used
39 * @param conversionContext a context, used when converting untyped atomic values to the target type.
40 */
41
42 public GenericAtomicComparer(/*@Nullable*/ StringCollator collator, XPathContext conversionContext) {
43 this.collator = collator;
44 if (collator == null) {
45 this.collator = CodepointCollator.getInstance();
46 }
47 context = conversionContext;
48 }
49
50 /**
51 * Factory method to make a GenericAtomicComparer for values of known types
52 *
53 * @param type0 primitive type of the first operand
54 * @param type1 primitive type of the second operand
55 * @param collator the collation to be used, if any. This is supplied as a SimpleCollation object
56 * which encapsulated both the collation URI and the collation itself.
57 * @param context the dynamic context
58 * @return a GenericAtomicComparer for values of known types
59 */
60
61 public static AtomicComparer makeAtomicComparer(
62 BuiltInAtomicType type0, BuiltInAtomicType type1, StringCollator collator, XPathContext context) {
63 int fp0 = type0.getFingerprint();
64 int fp1 = type1.getFingerprint();
65 if (fp0 == fp1) {
66 switch (fp0) {
67 case StandardNames.XS_DATE_TIME:
68 case StandardNames.XS_DATE:
69 case StandardNames.XS_TIME:
70 case StandardNames.XS_G_DAY:
71 case StandardNames.XS_G_MONTH:
72 case StandardNames.XS_G_YEAR:
73 case StandardNames.XS_G_MONTH_DAY:
74 case StandardNames.XS_G_YEAR_MONTH:
75 return new CalendarValueComparer(context);
76
77 case StandardNames.XS_BOOLEAN:
78 case StandardNames.XS_DAY_TIME_DURATION:
79 case StandardNames.XS_YEAR_MONTH_DURATION:
80 return ComparableAtomicValueComparer.getInstance();
81
82 case StandardNames.XS_BASE64_BINARY:
83 case StandardNames.XS_HEX_BINARY:
84 // These become fully comparable in XPath 3.1 but we allow it regardless of version
85 return ComparableAtomicValueComparer.getInstance();
86
87 case StandardNames.XS_QNAME:
88 case StandardNames.XS_NOTATION:
89 return EqualityComparer.getInstance();
90
91 }
92 }
93
94 if (type0.isPrimitiveNumeric() && type1.isPrimitiveNumeric()) {
95 return ComparableAtomicValueComparer.getInstance();
96 }
97
98 if ((fp0 == StandardNames.XS_STRING ||
99 fp0 == StandardNames.XS_UNTYPED_ATOMIC ||
100 fp0 == StandardNames.XS_ANY_URI) &&
101 (fp1 == StandardNames.XS_STRING ||
102 fp1 == StandardNames.XS_UNTYPED_ATOMIC ||
103 fp1 == StandardNames.XS_ANY_URI)) {
104 if (collator instanceof CodepointCollator) {
105 return CodepointCollatingComparer.getInstance();
106 } else {
107 return new CollatingAtomicComparer(collator);
108 }
109 }
110 return new GenericAtomicComparer(collator, context);
111 }
112
113 public StringCollator getCollator() {
114 return collator;
115 }
116
117 /**
118 * Supply the dynamic context in case this is needed for the comparison
119 *
120 * @param context the dynamic evaluation context
121 * @return either the original AtomicComparer, or a new AtomicComparer in which the context
122 * is known. The original AtomicComparer is not modified
123 */
124
125 public GenericAtomicComparer provideContext(XPathContext context) {
126 return new GenericAtomicComparer(collator, context);
127 }
128
129 /**
130 * Get the underlying string collator
131 *
132 * @return the string collator
133 */
134
135 public StringCollator getStringCollator() {
136 return collator;
137 }
138
139 /**
140 * Compare two AtomicValue objects according to the rules for their data type. UntypedAtomic
141 * values are compared as if they were strings; if different semantics are wanted, the conversion
142 * must be done by the caller.
143 *
144 * @param a the first object to be compared. It is intended that this should be an instance
145 * of AtomicValue, though this restriction is not enforced. If it is a StringValue, the
146 * collator is used to compare the values, otherwise the value must implement the java.util.Comparable
147 * interface.
148 * @param b the second object to be compared. This must be comparable with the first object: for
149 * example, if one is a string, they must both be strings.
150 * @return <0 if a < b, 0 if a = b, >0 if a > b
151 * @throws ClassCastException if the objects are not comparable
152 * @throws NoDynamicContextException if this comparer required access to dynamic context information,
153 * notably the implicit timezone, and this information is not available. In general this happens if a
154 * context-dependent comparison is attempted at compile-time, and it signals the compiler to generate
155 * code that tries again at run-time.
156 */
157
158 public int compareAtomicValues(AtomicValue a, AtomicValue b) throws NoDynamicContextException {
159
160 // System.err.println("Comparing " + a.getClass() + "(" + a + ") with " + b.getClass() + "(" + b + ") using " + collator);
161
162 if (a == null) {
163 return b == null ? 0 : -1;
164 } else if (b == null) {
165 return +1;
166 }
167
168 if (a instanceof StringValue && b instanceof StringValue) {
169 if (collator instanceof CodepointCollator) {
170 return CodepointCollator.compareCS(a.getStringValueCS(), b.getStringValueCS());
171 } else {
172 return collator.compareStrings(a.getStringValue(), b.getStringValue());
173 }
174 } else {
175 int implicitTimezone = context.getImplicitTimezone();
176 Comparable ac = (Comparable) a.getXPathComparable(true, collator, implicitTimezone);
177 Comparable bc = (Comparable) b.getXPathComparable(true, collator, implicitTimezone);
178 if (ac == null || bc == null) {
179 XPathException e = new XPathException("Objects are not comparable (" +
180 Type.displayTypeName(a) + ", " + Type.displayTypeName(b) + ')', "XPTY0004");
181 throw new ComparisonException(e);
182 } else {
183 return ac.compareTo(bc);
184 }
185 }
186 }
187
188 /**
189 * Compare two AtomicValue objects for equality according to the rules for their data type. UntypedAtomic
190 * values are compared as if they were strings; if different semantics are wanted, the conversion
191 * must be done by the caller.
192 *
193 * @param a the first object to be compared. If it is a StringValue, the
194 * collator is used to compare the values, otherwise the value must implement the equals() method.
195 * @param b the second object to be compared. This must be comparable with the first object: for
196 * example, if one is a string, they must both be strings.
197 * @return true if the values are equal, false if not
198 * @throws ClassCastException if the objects are not comparable
199 */
200
201 public boolean comparesEqual(AtomicValue a, AtomicValue b) throws NoDynamicContextException {
202 // System.err.println("Comparing " + a.getClass() + ": " + a + " with " + b.getClass() + ": " + b);
203 if (a instanceof StringValue && b instanceof StringValue) {
204 return collator.comparesEqual(a.getStringValue(), b.getStringValue());
205 } else if (a instanceof CalendarValue && b instanceof CalendarValue) {
206 return ((CalendarValue) a).compareTo((CalendarValue) b, context.getImplicitTimezone()) == 0;
207 } else {
208 int implicitTimezone = context.getImplicitTimezone();
209 Object ac = a.getXPathComparable(false, collator, implicitTimezone);
210 Object bc = b.getXPathComparable(false, collator, implicitTimezone);
211 return ac.equals(bc);
212 }
213 }
214
215 public XPathContext getContext() {
216 return context;
217 }
218
219 /**
220 * Create a string representation of this AtomicComparer that can be saved in a compiled
221 * package and used to reconstitute the AtomicComparer when the package is reloaded
222 *
223 * @return a string representation of the AtomicComparer
224 */
225 public String save() {
226 return "GAC|" + collator.getCollationURI();
227 }
228
229 @Override
230 public int hashCode() {
231 return collator.hashCode();
232 }
233
234 @Override
235 public boolean equals(Object obj) {
236 // In considering whether two GenericAtomicComparers are equal, we ignore the dynamic context, because this
237 // is only ever used to test the implicit timezone, and in all reasonable scenarios, the implicit timezone
238 // is global.
239 return obj instanceof GenericAtomicComparer && collator.equals(((GenericAtomicComparer)obj).collator);
240 }
241 }
242
243