Debian Codegraph @

net/sf/saxon/expr/EquivalenceComparer.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;
9
10 import net.sf.saxon.expr.sort.GenericAtomicComparer;
11 import net.sf.saxon.lib.StringCollator;
12 import net.sf.saxon.trans.NoDynamicContextException;
13 import net.sf.saxon.value.AtomicValue;
14 import net.sf.saxon.value.CalendarValue;
15 import net.sf.saxon.value.StringValue;
16
17 /**
18 * A comparer that compares atomic values for equality, with the properties:
19 * - non-comparable types compare false
20 * - NaN compares equal to NaN
21 */
22 public class EquivalenceComparer extends GenericAtomicComparer {
23
24 protected EquivalenceComparer(StringCollator collator, XPathContext context) {
25 super(collator, context);
26 }
27
28 @Override
29 public EquivalenceComparer provideContext(XPathContext context) {
30 return new EquivalenceComparer(getStringCollator(), context);
31 }
32
33 /**
34 * Compare two AtomicValue objects for equality according to the rules for their data type. UntypedAtomic
35 * values are compared as if they were strings; if different semantics are wanted, the conversion
36 * must be done by the caller.
37 *
38 * @param a the first object to be compared. If it is a StringValue, the
39 * collator is used to compare the values, otherwise the value must implement the equals() method.
40 * @param b the second object to be compared. This must be comparable with the first object: for
41 * example, if one is a string, they must both be strings.
42 * @return true if the values are equal, false if not
43 * @throws ClassCastException if the objects are not comparable
44 */
45
46 public boolean comparesEqual (AtomicValue a, AtomicValue b) throws NoDynamicContextException {
47 // System.err.println("Comparing " + a.getClass() + ": " + a + " with " + b.getClass() + ": " + b);
48 if (a instanceof StringValue && b instanceof StringValue) {
49 return getStringCollator().comparesEqual(a.getStringValue(), b.getStringValue());
50 } else if (a instanceof CalendarValue && b instanceof CalendarValue) {
51 return ((CalendarValue) a).compareTo((CalendarValue) b, getContext().getImplicitTimezone()) == 0;
52 } else if (a.isNaN() && b.isNaN()) {
53 return true;
54 } else {
55 int implicitTimezone = getContext().getImplicitTimezone();
56 Object ac = a.getXPathComparable(false, getStringCollator(), implicitTimezone);
57 Object bc = b.getXPathComparable(false, getStringCollator(), implicitTimezone);
58 return ac.equals(bc);
59 }
60 }
61
62
63
64 // /**
65 // * Compare two values that are known to be non-comparable. In the base class this method
66 // * throws a ClassCastException. In this subclass it is overridden to return
67 // * {@link net.sf.saxon.om.SequenceTool#INDETERMINATE_ORDERING}
68 // */
69 //
70 // @Override
71 // protected int compareNonComparables(AtomicValue a, AtomicValue b) {
72 // return SequenceTool.INDETERMINATE_ORDERING;
73 // }
74
75 /**
76 * Create a string representation of this AtomicComparer that can be saved in a compiled
77 * package and used to reconstitute the AtomicComparer when the package is reloaded
78 *
79 * @return a string representation of the AtomicComparer
80 */
81 @Override
82 public String save() {
83 return "EQUIV|" + super.save();
84 }
85 }
86
87 // Copyright (c) 2010 Saxonica Limited.
88
89
90