-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[improvement] 'UseImmutableBytes' feature flag allows opting-in to By…
…tes instead of ByteBuffer (#226)
- Loading branch information
1 parent
a792407
commit 10f4965
Showing
28 changed files
with
535 additions
and
78 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
conjure-java-core/src/integrationInput/java/com/palantir/binary/BinaryAliasExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.palantir.binary; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonValue; | ||
import java.nio.ByteBuffer; | ||
import java.util.Objects; | ||
import javax.annotation.Generated; | ||
|
||
@Generated("com.palantir.conjure.java.types.AliasGenerator") | ||
public final class BinaryAliasExample { | ||
private final ByteBuffer value; | ||
|
||
private BinaryAliasExample(ByteBuffer value) { | ||
Objects.requireNonNull(value, "value cannot be null"); | ||
this.value = value; | ||
} | ||
|
||
@JsonValue | ||
public ByteBuffer get() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return value.toString(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return this == other | ||
|| (other instanceof BinaryAliasExample | ||
&& this.value.equals(((BinaryAliasExample) other).value)); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return value.hashCode(); | ||
} | ||
|
||
@JsonCreator | ||
public static BinaryAliasExample of(ByteBuffer value) { | ||
return new BinaryAliasExample(value); | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
conjure-java-core/src/integrationInput/java/com/palantir/binary/BinaryExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package com.palantir.binary; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonSetter; | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import com.palantir.logsafe.Preconditions; | ||
import com.palantir.logsafe.SafeArg; | ||
import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; | ||
import java.nio.ByteBuffer; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import javax.annotation.Generated; | ||
|
||
@JsonDeserialize(builder = BinaryExample.Builder.class) | ||
@Generated("com.palantir.conjure.java.types.BeanGenerator") | ||
public final class BinaryExample { | ||
private final ByteBuffer binary; | ||
|
||
private volatile int memoizedHashCode; | ||
|
||
private BinaryExample(ByteBuffer binary) { | ||
validateFields(binary); | ||
this.binary = binary; | ||
} | ||
|
||
@JsonProperty("binary") | ||
public ByteBuffer getBinary() { | ||
return this.binary.asReadOnlyBuffer(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return this == other || (other instanceof BinaryExample && equalTo((BinaryExample) other)); | ||
} | ||
|
||
private boolean equalTo(BinaryExample other) { | ||
return this.binary.equals(other.binary); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
if (memoizedHashCode == 0) { | ||
memoizedHashCode = Objects.hash(binary); | ||
} | ||
return memoizedHashCode; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new StringBuilder("BinaryExample") | ||
.append('{') | ||
.append("binary") | ||
.append(": ") | ||
.append(binary) | ||
.append('}') | ||
.toString(); | ||
} | ||
|
||
public static BinaryExample of(ByteBuffer binary) { | ||
return builder().binary(binary).build(); | ||
} | ||
|
||
private static void validateFields(ByteBuffer binary) { | ||
List<String> missingFields = null; | ||
missingFields = addFieldIfMissing(missingFields, binary, "binary"); | ||
if (missingFields != null) { | ||
throw new SafeIllegalArgumentException( | ||
"Some required fields have not been set", | ||
SafeArg.of("missingFields", missingFields)); | ||
} | ||
} | ||
|
||
private static List<String> addFieldIfMissing( | ||
List<String> prev, Object fieldValue, String fieldName) { | ||
List<String> missingFields = prev; | ||
if (fieldValue == null) { | ||
if (missingFields == null) { | ||
missingFields = new ArrayList<>(1); | ||
} | ||
missingFields.add(fieldName); | ||
} | ||
return missingFields; | ||
} | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
@Generated("com.palantir.conjure.java.types.BeanBuilderGenerator") | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public static final class Builder { | ||
private ByteBuffer binary; | ||
|
||
private Builder() {} | ||
|
||
public Builder from(BinaryExample other) { | ||
binary(other.getBinary()); | ||
return this; | ||
} | ||
|
||
@JsonSetter("binary") | ||
public Builder binary(ByteBuffer binary) { | ||
Preconditions.checkNotNull(binary, "binary cannot be null"); | ||
this.binary = ByteBuffer.allocate(binary.remaining()).put(binary.duplicate()); | ||
this.binary.rewind(); | ||
return this; | ||
} | ||
|
||
public BinaryExample build() { | ||
return new BinaryExample(binary); | ||
} | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
conjure-java-core/src/integrationInput/java/com/palantir/binary/OptionalExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package com.palantir.binary; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonSetter; | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import com.palantir.logsafe.Preconditions; | ||
import com.palantir.logsafe.SafeArg; | ||
import com.palantir.logsafe.exceptions.SafeIllegalArgumentException; | ||
import java.nio.ByteBuffer; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import javax.annotation.Generated; | ||
|
||
@JsonDeserialize(builder = OptionalExample.Builder.class) | ||
@Generated("com.palantir.conjure.java.types.BeanGenerator") | ||
public final class OptionalExample { | ||
private final Optional<ByteBuffer> item; | ||
|
||
private volatile int memoizedHashCode; | ||
|
||
private OptionalExample(Optional<ByteBuffer> item) { | ||
validateFields(item); | ||
this.item = item; | ||
} | ||
|
||
@JsonProperty("item") | ||
public Optional<ByteBuffer> getItem() { | ||
return this.item; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return this == other | ||
|| (other instanceof OptionalExample && equalTo((OptionalExample) other)); | ||
} | ||
|
||
private boolean equalTo(OptionalExample other) { | ||
return this.item.equals(other.item); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
if (memoizedHashCode == 0) { | ||
memoizedHashCode = Objects.hash(item); | ||
} | ||
return memoizedHashCode; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new StringBuilder("OptionalExample") | ||
.append('{') | ||
.append("item") | ||
.append(": ") | ||
.append(item) | ||
.append('}') | ||
.toString(); | ||
} | ||
|
||
public static OptionalExample of(ByteBuffer item) { | ||
return builder().item(Optional.of(item)).build(); | ||
} | ||
|
||
private static void validateFields(Optional<ByteBuffer> item) { | ||
List<String> missingFields = null; | ||
missingFields = addFieldIfMissing(missingFields, item, "item"); | ||
if (missingFields != null) { | ||
throw new SafeIllegalArgumentException( | ||
"Some required fields have not been set", | ||
SafeArg.of("missingFields", missingFields)); | ||
} | ||
} | ||
|
||
private static List<String> addFieldIfMissing( | ||
List<String> prev, Object fieldValue, String fieldName) { | ||
List<String> missingFields = prev; | ||
if (fieldValue == null) { | ||
if (missingFields == null) { | ||
missingFields = new ArrayList<>(1); | ||
} | ||
missingFields.add(fieldName); | ||
} | ||
return missingFields; | ||
} | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
@Generated("com.palantir.conjure.java.types.BeanBuilderGenerator") | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public static final class Builder { | ||
private Optional<ByteBuffer> item = Optional.empty(); | ||
|
||
private Builder() {} | ||
|
||
public Builder from(OptionalExample other) { | ||
item(other.getItem()); | ||
return this; | ||
} | ||
|
||
@JsonSetter("item") | ||
public Builder item(Optional<ByteBuffer> item) { | ||
this.item = Preconditions.checkNotNull(item, "item cannot be null"); | ||
return this; | ||
} | ||
|
||
public Builder item(ByteBuffer item) { | ||
this.item = Optional.of(Preconditions.checkNotNull(item, "item cannot be null")); | ||
return this; | ||
} | ||
|
||
public OptionalExample build() { | ||
return new OptionalExample(item); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.