Skip to content

Commit

Permalink
[improvement] 'UseImmutableBytes' feature flag allows opting-in to By…
Browse files Browse the repository at this point in the history
…tes instead of ByteBuffer (#226)
  • Loading branch information
Carter Kozak authored and bulldozer-bot[bot] committed Feb 8, 2019
1 parent a792407 commit 10f4965
Show file tree
Hide file tree
Showing 28 changed files with 535 additions and 78 deletions.
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);
}
}
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);
}
}
}
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import java.nio.ByteBuffer;
import com.palantir.conjure.java.lib.Bytes;
import java.util.Objects;
import javax.annotation.Generated;

@Generated("com.palantir.conjure.java.types.AliasGenerator")
public final class BinaryAliasExample {
private final ByteBuffer value;
private final Bytes value;

private BinaryAliasExample(ByteBuffer value) {
private BinaryAliasExample(Bytes value) {
Objects.requireNonNull(value, "value cannot be null");
this.value = value;
}

@JsonValue
public ByteBuffer get() {
public Bytes get() {
return value;
}

Expand All @@ -38,7 +38,7 @@ public int hashCode() {
}

@JsonCreator
public static BinaryAliasExample of(ByteBuffer value) {
public static BinaryAliasExample of(Bytes value) {
return new BinaryAliasExample(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.palantir.conjure.java.lib.Bytes;
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;
Expand All @@ -16,18 +16,18 @@
@JsonDeserialize(builder = BinaryExample.Builder.class)
@Generated("com.palantir.conjure.java.types.BeanGenerator")
public final class BinaryExample {
private final ByteBuffer binary;
private final Bytes binary;

private volatile int memoizedHashCode;

private BinaryExample(ByteBuffer binary) {
private BinaryExample(Bytes binary) {
validateFields(binary);
this.binary = binary;
}

@JsonProperty("binary")
public ByteBuffer getBinary() {
return this.binary.asReadOnlyBuffer();
public Bytes getBinary() {
return this.binary;
}

@Override
Expand Down Expand Up @@ -58,11 +58,11 @@ public String toString() {
.toString();
}

public static BinaryExample of(ByteBuffer binary) {
public static BinaryExample of(Bytes binary) {
return builder().binary(binary).build();
}

private static void validateFields(ByteBuffer binary) {
private static void validateFields(Bytes binary) {
List<String> missingFields = null;
missingFields = addFieldIfMissing(missingFields, binary, "binary");
if (missingFields != null) {
Expand Down Expand Up @@ -91,7 +91,7 @@ public static Builder builder() {
@Generated("com.palantir.conjure.java.types.BeanBuilderGenerator")
@JsonIgnoreProperties(ignoreUnknown = true)
public static final class Builder {
private ByteBuffer binary;
private Bytes binary;

private Builder() {}

Expand All @@ -101,10 +101,8 @@ public Builder from(BinaryExample other) {
}

@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();
public Builder binary(Bytes binary) {
this.binary = Preconditions.checkNotNull(binary, "binary cannot be null");
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ public enum FeatureFlags {
* Undertow generated service interfaces are generated with an "Undertow" prefix.
*/
UndertowServicePrefix,

/**
* Use the conjure immutable "Bytes" class over ByteBuffer.
*/
UseImmutableBytes,
}
Loading

0 comments on commit 10f4965

Please sign in to comment.