Skip to content

Commit b885c0b

Browse files
[refactor] Add try-with-resources statement to the collation-key function, and add BinaryValue.hashCode() method.
1 parent 590b5d5 commit b885c0b

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

exist-core/src/main/java/org/exist/xquery/functions/fn/FunCollationKey.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@
2929

3030
import com.ibm.icu.text.Collator;
3131

32+
import java.io.IOException;
3233
import java.nio.charset.StandardCharsets;
3334

3435
import static org.exist.xquery.FunctionDSL.*;
3536
import static org.exist.xquery.functions.fn.FnModule.functionSignatures;
3637

3738
public class FunCollationKey extends BasicFunction {
3839

39-
private static final String FN_NAME = "collation-key"; // , Function.BUILTIN_FUNCTION_NS, FnModule.PREFIX);
40+
private static final String FN_NAME = "collation-key";
4041
private static final String FN_DESCRIPTION =
4142
"Given a $value-string value and a $collation-string " +
4243
"collation, generates an internal value called a collation key, with the " +
@@ -63,8 +64,17 @@ public FunCollationKey(final XQueryContext context, final FunctionSignature sign
6364
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
6465
final String source = (args.length >= 1) ? args[0].itemAt(0).toString() : "";
6566
final Collator collator = (args.length >= 2) ? Collations.getCollationFromURI(args[1].itemAt(0).toString()) : null;
66-
67-
return new BinaryValueFromBinaryString(new Base64BinaryValueType(), Base64.encodeBase64String(
68-
(collator == null) ? source.getBytes(StandardCharsets.UTF_8) : new String(collator.getCollationKey(source).toByteArray()).getBytes(StandardCharsets.UTF_8))).convertTo(new Base64BinaryValueType());
67+
final Sequence sequence;
68+
try (BinaryValueFromBinaryString binaryValue = new BinaryValueFromBinaryString(
69+
new Base64BinaryValueType(),
70+
Base64.encodeBase64String(
71+
(collator == null) ?
72+
source.getBytes(StandardCharsets.UTF_8) :
73+
new String(collator.getCollationKey(source).toByteArray()).getBytes(StandardCharsets.UTF_8)))) {
74+
sequence = binaryValue.convertTo(new Base64BinaryValueType());
75+
} catch (IOException e) {
76+
throw new XPathException(e);
77+
}
78+
return sequence;
6979
}
7080
}

exist-core/src/main/java/org/exist/xquery/value/BinaryValue.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.exist.xquery.XPathException;
3232

3333
import java.io.*;
34-
import java.util.Objects;
3534

3635
import static java.nio.charset.StandardCharsets.UTF_8;
3736

@@ -292,4 +291,25 @@ public boolean equals(Object o) {
292291
BinaryValue that = (BinaryValue) o;
293292
return compareTo(that) == 0;
294293
}
294+
295+
@Override
296+
public int hashCode() {
297+
final InputStream is = getInputStream();
298+
int hash = 7;
299+
300+
if (is != null) {
301+
int read;
302+
do {
303+
try {
304+
read = is.read();
305+
if (read != -1) {
306+
hash = 31 * hash + read;
307+
}
308+
} catch (final IOException ioe) {
309+
read = -1;
310+
}
311+
} while (read != -1);
312+
}
313+
return hash;
314+
}
295315
}

0 commit comments

Comments
 (0)