Debian Codegraph @

net/sf/saxon/expr/sort/CalendarValueComparer.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.trans.NoDynamicContextException;
13 import net.sf.saxon.value.AtomicValue;
14 import net.sf.saxon.value.CalendarValue;
15
16 /**
17 * A comparer specifically for comparing two date, time, or dateTime values
18 */
19 public class CalendarValueComparer implements AtomicComparer {
20
21 private transient XPathContext context;
22
23 public CalendarValueComparer(XPathContext context) {
24 this.context = context;
25 }
26
27 /*@Nullable*/
28 public StringCollator getCollator() {
29 return null;
30 }
31
32 /**
33 * Supply the dynamic context in case this is needed for the comparison
34 *
35 * @param context the dynamic evaluation context
36 * @return either the original AtomicComparer, or a new AtomicComparer in which the context
37 * is known. The original AtomicComparer is not modified
38 */
39
40 public AtomicComparer provideContext(XPathContext context) {
41 return new CalendarValueComparer(context);
42 }
43
44 /**
45 * Compare two AtomicValue objects according to the rules for their data type. UntypedAtomic
46 * values are compared as if they were strings; if different semantics are wanted, the conversion
47 * must be done by the caller.
48 *
49 * @param a the first object to be compared. It is intended that this should be an instance
50 * of AtomicValue, though this restriction is not enforced. If it is a StringValue, the
51 * collator is used to compare the values, otherwise the value must implement the java.util.Comparable
52 * interface.
53 * @param b the second object to be compared. This must be comparable with the first object: for
54 * example, if one is a string, they must both be strings.
55 * @return <0 if a<b, 0 if a=b, >0 if a>b
56 * @throws ClassCastException if the objects are not comparable
57 */
58
59 public int compareAtomicValues(AtomicValue a, AtomicValue b) throws NoDynamicContextException {
60 if (a == null) {
61 return b == null ? 0 : -1;
62 } else if (b == null) {
63 return +1;
64 }
65 return ((CalendarValue) a).compareTo((CalendarValue) b, context.getImplicitTimezone());
66 }
67
68 /**
69 * Compare two AtomicValue objects for equality according to the rules for their data type. UntypedAtomic
70 * values are compared by converting to the type of the other operand.
71 *
72 * @param a the first object to be compared. It is intended that this should be an instance
73 * of AtomicValue, though this restriction is not enforced. If it is a StringValue, the
74 * collator is used to compare the values, otherwise the value must implement the equals() method.
75 * @param b the second object to be compared. This must be comparable with the first object: for
76 * example, if one is a string, they must both be strings.
77 * @return true if the values are equal, false if not
78 * @throws ClassCastException if the objects are not comparable
79 */
80
81 public boolean comparesEqual(AtomicValue a, AtomicValue b) throws NoDynamicContextException {
82 return compareAtomicValues(a, b) == 0;
83 }
84
85 /**
86 * Create a string representation of this AtomicComparer that can be saved in a compiled
87 * package and used to reconstitute the AtomicComparer when the package is reloaded
88 *
89 * @return a string representation of the AtomicComparer
90 */
91 public String save() {
92 return "CalVC";
93 }
94 }
95