summaryrefslogtreecommitdiffstats
path: root/container-search
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2023-03-22 14:07:32 +0100
committerHenning Baldersheim <balder@yahoo-inc.com>2023-03-22 14:07:32 +0100
commitadcc7343785867b991ecebb998fc96f541d21108 (patch)
treec805482b432eb9e0ec978b8f5acd1ad3549cb525 /container-search
parentd7d3271e04f210c1b084c8d586b18d777488cb23 (diff)
Make RawBase64 Comparable to avoid fallback to comparing toString().
Diffstat (limited to 'container-search')
-rw-r--r--container-search/src/main/java/com/yahoo/prelude/hitfield/RawBase64.java8
-rw-r--r--container-search/src/test/java/com/yahoo/prelude/hitfield/RawBase64TestCase.java43
2 files changed, 50 insertions, 1 deletions
diff --git a/container-search/src/main/java/com/yahoo/prelude/hitfield/RawBase64.java b/container-search/src/main/java/com/yahoo/prelude/hitfield/RawBase64.java
index 87b073b98c5..2071e43f54c 100644
--- a/container-search/src/main/java/com/yahoo/prelude/hitfield/RawBase64.java
+++ b/container-search/src/main/java/com/yahoo/prelude/hitfield/RawBase64.java
@@ -1,18 +1,24 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.hitfield;
+import java.util.Arrays;
import java.util.Base64;
/**
* @author baldersheim
*/
-public class RawBase64 {
+public class RawBase64 implements Comparable<RawBase64> {
private final byte[] content;
public RawBase64(byte[] content) {
this.content = content;
}
@Override
+ public int compareTo(RawBase64 rhs) {
+ return Arrays.compareUnsigned(content, rhs.content);
+ }
+
+ @Override
public String toString() {
return Base64.getEncoder().encodeToString(content);
}
diff --git a/container-search/src/test/java/com/yahoo/prelude/hitfield/RawBase64TestCase.java b/container-search/src/test/java/com/yahoo/prelude/hitfield/RawBase64TestCase.java
new file mode 100644
index 00000000000..0991f73914f
--- /dev/null
+++ b/container-search/src/test/java/com/yahoo/prelude/hitfield/RawBase64TestCase.java
@@ -0,0 +1,43 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+package com.yahoo.prelude.hitfield;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * @author baldersheim
+ */
+public class RawBase64TestCase {
+ private static final byte [] first = {0, 1, 2};
+ private static final byte [] second = {19, 0};
+ private static final byte [] last = {-127, 0};
+ private static final byte [] longer = {0, 1, 2, 3};
+ @Test
+ public void requireToStringToProvideBase64Encoding() {
+ assertEquals("AAEC", new RawBase64(first).toString());
+ }
+
+ private void verify(int expected, byte [] a, byte [] b) {
+ int diff = new RawBase64(a).compareTo(new RawBase64(b));
+ if (expected == 0) {
+ assertEquals(expected, diff);
+ } else {
+ assertEquals(expected, diff / Math.abs(diff));
+ }
+ }
+
+ @Test
+ public void testSortOrder() {
+ verify(0, first, first);
+ verify(-1, first, longer);
+ verify(1, longer, first);
+
+ verify(-1, first, second);
+ verify(1, second, first);
+
+ verify(-1, first, last);
+ verify(1, last, first);
+ }
+}