From ca53a39f344a7a91726a39e7a78161aa428a154b Mon Sep 17 00:00:00 2001 From: David Schlosnagle Date: Mon, 4 Sep 2023 19:32:46 -0400 Subject: [PATCH 1/2] Bytes has signed and unsigned comparators --- conjure-lib/build.gradle | 1 + .../com/palantir/conjure/java/lib/Bytes.java | 11 ++++++++ .../palantir/conjure/java/lib/BytesTests.java | 26 +++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/conjure-lib/build.gradle b/conjure-lib/build.gradle index a489777d9..99fd6f0b4 100644 --- a/conjure-lib/build.gradle +++ b/conjure-lib/build.gradle @@ -26,6 +26,7 @@ dependencies { implementation 'com.fasterxml.jackson.core:jackson-annotations' implementation 'com.fasterxml.jackson.core:jackson-core' + implementation 'com.google.guava:guava' implementation 'com.palantir.safe-logging:safe-logging' testImplementation 'com.palantir.safe-logging:preconditions-assertj' diff --git a/conjure-lib/src/main/java/com/palantir/conjure/java/lib/Bytes.java b/conjure-lib/src/main/java/com/palantir/conjure/java/lib/Bytes.java index c63890d71..12206765a 100644 --- a/conjure-lib/src/main/java/com/palantir/conjure/java/lib/Bytes.java +++ b/conjure-lib/src/main/java/com/palantir/conjure/java/lib/Bytes.java @@ -24,11 +24,14 @@ import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.google.common.primitives.SignedBytes; +import com.google.common.primitives.UnsignedBytes; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.util.Arrays; +import java.util.Comparator; /** An immutable {@code byte[]} wrapper. */ @JsonSerialize(using = Bytes.Serializer.class) @@ -114,6 +117,14 @@ public static Bytes from(ByteBuffer buffer) { return new Bytes(safe); } + public static Comparator signed() { + return (x, y) -> SignedBytes.lexicographicalComparator().compare(x.safe, y.safe); + } + + public static Comparator unsigned() { + return (x, y) -> UnsignedBytes.lexicographicalComparator().compare(x.safe, y.safe); + } + static final class Serializer extends JsonSerializer { @Override public void serialize(Bytes value, JsonGenerator gen, SerializerProvider _serializer) throws IOException { diff --git a/conjure-lib/src/test/java/com/palantir/conjure/java/lib/BytesTests.java b/conjure-lib/src/test/java/com/palantir/conjure/java/lib/BytesTests.java index 797056f64..9a1e8c442 100644 --- a/conjure-lib/src/test/java/com/palantir/conjure/java/lib/BytesTests.java +++ b/conjure-lib/src/test/java/com/palantir/conjure/java/lib/BytesTests.java @@ -24,6 +24,7 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; +import java.util.Comparator; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.parallel.Execution; import org.junit.jupiter.api.parallel.ExecutionMode; @@ -181,4 +182,29 @@ public void testSerDe_cannotMapEmptyArray() { ObjectMapper mapper = new ObjectMapper(); assertThatThrownBy(() -> mapper.readValue("[]", Bytes.class)).isInstanceOf(JsonParseException.class); } + + @Test + public void testUnsignedComparator() { + Comparator comparator = Bytes.unsigned(); + assertThat(Bytes.from(new byte[] {1})).usingComparator(comparator).isEqualTo(Bytes.from(new byte[] {1})); + assertThat(Bytes.from(new byte[] {-1})).usingComparator(comparator).isEqualTo(Bytes.from(new byte[] {-1})); + checkComparator(comparator, Bytes.from(new byte[] {-1, 0, 1}), Bytes.from(new byte[] {-1, 0, 2})); + checkComparator(comparator, Bytes.from(new byte[] {1, 0, 1}), Bytes.from(new byte[] {-1, 0, 1})); + } + + @Test + public void testSignedComparator() { + Comparator comparator = Bytes.signed(); + assertThat(Bytes.from(new byte[] {1})).usingComparator(comparator).isEqualTo(Bytes.from(new byte[] {1})); + assertThat(Bytes.from(new byte[] {-1})).usingComparator(comparator).isEqualTo(Bytes.from(new byte[] {-1})); + checkComparator(comparator, Bytes.from(new byte[] {-1, 0, 1}), Bytes.from(new byte[] {-1, 0, 2})); + checkComparator(comparator, Bytes.from(new byte[] {-1, 0, 1}), Bytes.from(new byte[] {1, 0, 1})); + } + + private static void checkComparator(Comparator comparator, Bytes lesser, Bytes greater) { + assertThat(comparator.compare(lesser, lesser)).isZero(); + assertThat(comparator.compare(lesser, greater)).isLessThan(0); + assertThat(comparator.compare(greater, lesser)).isGreaterThan(0); + assertThat(comparator.compare(greater, greater)).isZero(); + } } From 0b346fcc08f10627d0c645b4d6b290e3b49ed933 Mon Sep 17 00:00:00 2001 From: svc-changelog Date: Mon, 4 Sep 2023 23:34:16 +0000 Subject: [PATCH 2/2] Add generated changelog entries --- changelog/@unreleased/pr-2102.v2.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changelog/@unreleased/pr-2102.v2.yml diff --git a/changelog/@unreleased/pr-2102.v2.yml b/changelog/@unreleased/pr-2102.v2.yml new file mode 100644 index 000000000..8d2509823 --- /dev/null +++ b/changelog/@unreleased/pr-2102.v2.yml @@ -0,0 +1,5 @@ +type: improvement +improvement: + description: Bytes has signed and unsigned comparators + links: + - https://github.com/palantir/conjure-java/pull/2102