Debian Codegraph @

net/sf/saxon/expr/sort/DoubleSortComparer.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 import net.sf.saxon.value.NumericValue;
14
15 /**
16 * An AtomicComparer used for sorting values that are known to be numeric.
17 * It also supports a separate method for getting a collation key to test equality of items.
18 * This comparator treats NaN values as equal to each other, and less than any other value.
19 *
20 * @author Michael H. Kay
21 */
22
23 public class DoubleSortComparer implements AtomicComparer {
24
25 private static DoubleSortComparer THE_INSTANCE = new DoubleSortComparer();
26
27 /**
28 * Get the singular instance of this class
29 *
30 * @return the singular instance
31 */
32
33 public static DoubleSortComparer getInstance() {
34 return THE_INSTANCE;
35 }
36
37 private DoubleSortComparer() {
38
39 }
40
41 /*@Nullable*/
42 public StringCollator getCollator() {
43 return null;
44 }
45
46 /**
47 * Supply the dynamic context in case this is needed for the comparison
48 *
49 * @param context the dynamic evaluation context
50 * @return either the original AtomicComparer, or a new AtomicComparer in which the context
51 * is known. The original AtomicComparer is not modified
52 */
53
54 public AtomicComparer provideContext(XPathContext context) {
55 return this;
56 }
57
58 /**
59 * Compare two AtomicValue objects according to the rules for their data type.
60 *
61 * @param a the first object to be compared. It is intended that this should normally be an instance
62 * of AtomicValue, though this restriction is not enforced. If it is a StringValue, the
63 * collator is used to compare the values, otherwise the value must implement the java.util.Comparable
64 * interface.
65 * @param b the second object to be compared. This must be comparable with the first object: for
66 * example, if one is a string, they must both be strings.
67 * @return <0 if a<b, 0 if a=b, >0 if a>b
68 * @throws ClassCastException if the objects are not comparable
69 */
70
71 public int compareAtomicValues(AtomicValue a, AtomicValue b) {
72 if (a == null) {
73 if (b == null) {
74 return 0;
75 } else {
76 return -1;
77 }
78 } else if (b == null) {
79 return +1;
80 }
81
82 NumericValue an = (NumericValue) a;
83 NumericValue bn = (NumericValue) b;
84
85 if (an.isNaN()) {
86 return bn.isNaN() ? 0 : -1;
87 } else if (bn.isNaN()) {
88 return +1;
89 }
90
91 return an.compareTo(bn);
92 }
93
94 /**
95 * Test whether two values compare equal. Note that for this comparer, NaN is considered equal to itself
96 */
97
98 public boolean comparesEqual(AtomicValue a, AtomicValue b) {
99 return compareAtomicValues(a, b) == 0;
100 }
101
102 /**
103 * Create a string representation of this AtomicComparer that can be saved in a compiled
104 * package and used to reconstitute the AtomicComparer when the package is reloaded
105 *
106 * @return a string representation of the AtomicComparer
107 */
108 public String save() {
109 return "DblSC";
110 }
111 }
112