Skip to content

Bytes has signed and unsigned comparators #2102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-2102.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Bytes has signed and unsigned comparators
links:
- https://github.com/palantir/conjure-java/pull/2102
1 change: 1 addition & 0 deletions conjure-lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
11 changes: 11 additions & 0 deletions conjure-lib/src/main/java/com/palantir/conjure/java/lib/Bytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -114,6 +117,14 @@ public static Bytes from(ByteBuffer buffer) {
return new Bytes(safe);
}

public static Comparator<Bytes> signed() {
return (x, y) -> SignedBytes.lexicographicalComparator().compare(x.safe, y.safe);
}

public static Comparator<Bytes> unsigned() {
return (x, y) -> UnsignedBytes.lexicographicalComparator().compare(x.safe, y.safe);
}

static final class Serializer extends JsonSerializer<Bytes> {
@Override
public void serialize(Bytes value, JsonGenerator gen, SerializerProvider _serializer) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Bytes> 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<Bytes> 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<Bytes> 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();
}
}