Debian Codegraph @

net/sf/saxon/expr/sort/SimpleCollation.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.Platform;
11 import net.sf.saxon.Version;
12 import net.sf.saxon.lib.StringCollator;
13 import net.sf.saxon.lib.SubstringMatcher;
14
15 import java.text.RuleBasedCollator;
16 import java.util.Comparator;
17
18 /**
19 * A simple collation that just wraps a supplied Comparator
20 */
21
22 public class SimpleCollation implements StringCollator {
23
24 private Comparator comparator;
25 private String uri;
26 /*@NotNull*/ private static Platform platform = Version.platform;
27
28 /**
29 * Create a SimpleCollation
30 *
31 * @param uri the collation URI
32 * @param comparator the Comparator that does the actual string comparison
33 */
34
35 public SimpleCollation(String uri, Comparator comparator) {
36 this.uri = uri;
37 this.comparator = comparator;
38 }
39
40 /**
41 * Get the collation URI. It must be possible to use this collation URI to reconstitute the collation
42 *
43 * @return a collation URI that can be used to reconstruct the collation when an XSLT package is reloaded.
44 */
45 public String getCollationURI() {
46 return uri;
47 }
48
49 /**
50 * Compares its two arguments for order. Returns a negative integer,
51 * zero, or a positive integer as the first argument is less than, equal
52 * to, or greater than the second.
53 *
54 * @param o1 the first object to be compared.
55 * @param o2 the second object to be compared.
56 * @return a negative integer, zero, or a positive integer as the
57 * first argument is less than, equal to, or greater than the
58 * second.
59 * @throws ClassCastException if the arguments' types prevent them from
60 * being compared by this Comparator.
61 */
62 public int compareStrings(CharSequence o1, CharSequence o2) {
63 return comparator.compare(o1, o2);
64 }
65
66 /**
67 * Compare two strings for equality. This may be more efficient than using compareStrings and
68 * testing whether the result is zero, but it must give the same result
69 *
70 * @param s1 the first string
71 * @param s2 the second string
72 * @return true if and only if the strings are considered equal,
73 */
74
75 public boolean comparesEqual(CharSequence s1, CharSequence s2) {
76 return comparator.compare(s1, s2) == 0;
77 }
78
79 /**
80 * Get the underlying comparator
81 *
82 * @return the underlying comparator
83 */
84
85 public Comparator getComparator() {
86 return comparator;
87 }
88
89 /**
90 * Set the underlying comparator
91 *
92 * @param comparator the underlying comparator
93 */
94
95 public void setComparator(Comparator comparator) {
96 this.comparator = comparator;
97 }
98
99 /**
100 * Get a collation key for a String. The essential property of collation keys
101 * is that if two values are equal under the collation, then the collation keys are
102 * compare correctly under the equals() method.
103 * @param s
104 */
105
106 public AtomicMatchKey getCollationKey(CharSequence s) {
107 return platform.getCollationKey(this, s.toString());
108 }
109
110 /**
111 * If possible, get a collator capable of substring matching (in functions such as fn:contains()).
112 *
113 * @return a collator suitable for substring matching, or null if none is available
114 */
115
116 public SubstringMatcher getSubstringMatcher() {
117 if (comparator instanceof SubstringMatcher) {
118 return (SubstringMatcher)comparator;
119 }
120 if (comparator instanceof RuleBasedCollator) {
121 return new RuleBasedSubstringMatcher(uri, (RuleBasedCollator) comparator);
122 }
123 // //#if EE==true
124 // if (comparator instanceof UCACollator) {
125 // return new ICUSubstringMatcher(uri, ((UCACollator) comparator).getRuleBasedCollator());
126 // }
127 // //#endif
128 return null;
129 }
130
131 }
132