All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.spotify.github.http.ImmutableHttpRequest Maven / Gradle / Ivy

The newest version!
package com.spotify.github.http;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Var;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import org.immutables.value.Generated;

/**
 * Immutable implementation of {@link HttpRequest}.
 * 

* Use the builder to create immutable instances: * {@code ImmutableHttpRequest.builder()}. */ @Generated(from = "HttpRequest", generator = "Immutables") @SuppressWarnings({"all"}) @ParametersAreNonnullByDefault @javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor") @Immutable @CheckReturnValue public final class ImmutableHttpRequest implements HttpRequest { private final String method; private final String url; private final @Nullable String body; private final Map> headers; private ImmutableHttpRequest(ImmutableHttpRequest.Builder builder) { this.url = builder.url; this.body = builder.body; if (builder.method != null) { initShim.method(builder.method); } if (builder.headersIsSet()) { initShim.headers(createUnmodifiableMap(false, false, builder.headers)); } this.method = initShim.method(); this.headers = initShim.headers(); this.initShim = null; } private ImmutableHttpRequest( String method, String url, @Nullable String body, Map> headers) { this.method = method; this.url = url; this.body = body; this.headers = headers; this.initShim = null; } private static final byte STAGE_INITIALIZING = -1; private static final byte STAGE_UNINITIALIZED = 0; private static final byte STAGE_INITIALIZED = 1; @SuppressWarnings("Immutable") private transient volatile InitShim initShim = new InitShim(); @Generated(from = "HttpRequest", generator = "Immutables") private final class InitShim { private byte methodBuildStage = STAGE_UNINITIALIZED; private String method; String method() { if (methodBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage()); if (methodBuildStage == STAGE_UNINITIALIZED) { methodBuildStage = STAGE_INITIALIZING; this.method = Objects.requireNonNull(methodInitialize(), "method"); methodBuildStage = STAGE_INITIALIZED; } return this.method; } void method(String method) { this.method = method; methodBuildStage = STAGE_INITIALIZED; } private byte headersBuildStage = STAGE_UNINITIALIZED; private Map> headers; Map> headers() { if (headersBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage()); if (headersBuildStage == STAGE_UNINITIALIZED) { headersBuildStage = STAGE_INITIALIZING; this.headers = createUnmodifiableMap(true, false, headersInitialize()); headersBuildStage = STAGE_INITIALIZED; } return this.headers; } void headers(Map> headers) { this.headers = headers; headersBuildStage = STAGE_INITIALIZED; } private String formatInitCycleMessage() { List attributes = new ArrayList<>(); if (methodBuildStage == STAGE_INITIALIZING) attributes.add("method"); if (headersBuildStage == STAGE_INITIALIZING) attributes.add("headers"); return "Cannot build HttpRequest, attribute initializers form cycle " + attributes; } } private String methodInitialize() { return HttpRequest.super.method(); } private Map> headersInitialize() { return HttpRequest.super.headers(); } /** * @return The value of the {@code method} attribute */ @JsonProperty @Override public String method() { InitShim shim = this.initShim; return shim != null ? shim.method() : this.method; } /** * @return The value of the {@code url} attribute */ @JsonProperty @Override public String url() { return url; } /** * @return The value of the {@code body} attribute */ @JsonProperty @Override public @Nullable String body() { return body; } /** * @return The value of the {@code headers} attribute */ @JsonProperty @Override public Map> headers() { InitShim shim = this.initShim; return shim != null ? shim.headers() : this.headers; } /** * Copy the current immutable object by setting a value for the {@link HttpRequest#method() method} attribute. * An equals check used to prevent copying of the same value by returning {@code this}. * @param value A new value for method * @return A modified copy of the {@code this} object */ public final ImmutableHttpRequest withMethod(String value) { String newValue = Objects.requireNonNull(value, "method"); if (this.method.equals(newValue)) return this; return new ImmutableHttpRequest(newValue, this.url, this.body, this.headers); } /** * Copy the current immutable object by setting a value for the {@link HttpRequest#url() url} attribute. * An equals check used to prevent copying of the same value by returning {@code this}. * @param value A new value for url * @return A modified copy of the {@code this} object */ public final ImmutableHttpRequest withUrl(String value) { String newValue = Objects.requireNonNull(value, "url"); if (this.url.equals(newValue)) return this; return new ImmutableHttpRequest(this.method, newValue, this.body, this.headers); } /** * Copy the current immutable object by setting a value for the {@link HttpRequest#body() body} attribute. * An equals check used to prevent copying of the same value by returning {@code this}. * @param value A new value for body (can be {@code null}) * @return A modified copy of the {@code this} object */ public final ImmutableHttpRequest withBody(@Nullable String value) { if (Objects.equals(this.body, value)) return this; return new ImmutableHttpRequest(this.method, this.url, value, this.headers); } /** * Copy the current immutable object by replacing the {@link HttpRequest#headers() headers} map with the specified map. * Nulls are not permitted as keys or values. * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param entries The entries to be added to the headers map * @return A modified copy of {@code this} object */ public final ImmutableHttpRequest withHeaders(Map> entries) { if (this.headers == entries) return this; Map> newValue = createUnmodifiableMap(true, false, entries); return new ImmutableHttpRequest(this.method, this.url, this.body, newValue); } /** * This instance is equal to all instances of {@code ImmutableHttpRequest} that have equal attribute values. * @return {@code true} if {@code this} is equal to {@code another} instance */ @Override public boolean equals(@Nullable Object another) { if (this == another) return true; return another instanceof ImmutableHttpRequest && equalTo(0, (ImmutableHttpRequest) another); } private boolean equalTo(int synthetic, ImmutableHttpRequest another) { return method.equals(another.method) && url.equals(another.url) && Objects.equals(body, another.body) && headers.equals(another.headers); } /** * Computes a hash code from attributes: {@code method}, {@code url}, {@code body}, {@code headers}. * @return hashCode value */ @Override public int hashCode() { @Var int h = 5381; h += (h << 5) + method.hashCode(); h += (h << 5) + url.hashCode(); h += (h << 5) + Objects.hashCode(body); h += (h << 5) + headers.hashCode(); return h; } /** * Prints the immutable value {@code HttpRequest} with attribute values. * @return A string representation of the value */ @Override public String toString() { return "HttpRequest{" + "method=" + method + ", url=" + url + ", body=" + body + ", headers=" + headers + "}"; } /** * Utility type used to correctly read immutable object from JSON representation. * @deprecated Do not use this type directly, it exists only for the Jackson-binding infrastructure */ @Generated(from = "HttpRequest", generator = "Immutables") @Deprecated @SuppressWarnings("Immutable") @JsonDeserialize @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE) static final class Json implements HttpRequest { @Nullable String method; @Nullable String url; @Nullable String body; @Nullable Map> headers = Collections.emptyMap(); boolean headersIsSet; @JsonProperty public void setMethod(String method) { this.method = method; } @JsonProperty public void setUrl(String url) { this.url = url; } @JsonProperty public void setBody(@Nullable String body) { this.body = body; } @JsonProperty public void setHeaders(Map> headers) { this.headers = headers; this.headersIsSet = null != headers; } @Override public String method() { throw new UnsupportedOperationException(); } @Override public String url() { throw new UnsupportedOperationException(); } @Override public String body() { throw new UnsupportedOperationException(); } @Override public Map> headers() { throw new UnsupportedOperationException(); } } /** * @param json A JSON-bindable data structure * @return An immutable value type * @deprecated Do not use this method directly, it exists only for the Jackson-binding infrastructure */ @Deprecated @JsonCreator(mode = JsonCreator.Mode.DELEGATING) static ImmutableHttpRequest fromJson(Json json) { ImmutableHttpRequest.Builder builder = ImmutableHttpRequest.builder(); if (json.method != null) { builder.method(json.method); } if (json.url != null) { builder.url(json.url); } if (json.body != null) { builder.body(json.body); } if (json.headersIsSet) { builder.putAllHeaders(json.headers); } return builder.build(); } /** * Creates an immutable copy of a {@link HttpRequest} value. * Uses accessors to get values to initialize the new immutable instance. * If an instance is already immutable, it is returned as is. * @param instance The instance to copy * @return A copied immutable HttpRequest instance */ public static ImmutableHttpRequest copyOf(HttpRequest instance) { if (instance instanceof ImmutableHttpRequest) { return (ImmutableHttpRequest) instance; } return ImmutableHttpRequest.builder() .from(instance) .build(); } /** * Creates a builder for {@link ImmutableHttpRequest ImmutableHttpRequest}. *

   * ImmutableHttpRequest.builder()
   *    .method(String) // optional {@link HttpRequest#method() method}
   *    .url(String) // required {@link HttpRequest#url() url}
   *    .body(String | null) // nullable {@link HttpRequest#body() body}
   *    .putHeaders|putAllHeaders(String => List&lt;String&gt;) // {@link HttpRequest#headers() headers} mappings
   *    .build();
   * 
* @return A new ImmutableHttpRequest builder */ public static ImmutableHttpRequest.Builder builder() { return new ImmutableHttpRequest.Builder(); } /** * Builds instances of type {@link ImmutableHttpRequest ImmutableHttpRequest}. * Initialize attributes and then invoke the {@link #build()} method to create an * immutable instance. *

{@code Builder} is not thread-safe and generally should not be stored in a field or collection, * but instead used immediately to create instances. */ @Generated(from = "HttpRequest", generator = "Immutables") @NotThreadSafe public static final class Builder { private static final long INIT_BIT_URL = 0x1L; private static final long OPT_BIT_HEADERS = 0x1L; private long initBits = 0x1L; private long optBits; private @Nullable String method; private @Nullable String url; private @Nullable String body; private Map> headers = new LinkedHashMap>(); private Builder() { } /** * Fill a builder with attribute values from the provided {@code HttpRequest} instance. * Regular attribute values will be replaced with those from the given instance. * Absent optional values will not replace present values. * Collection elements and entries will be added, not replaced. * @param instance The instance from which to copy values * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final Builder from(HttpRequest instance) { Objects.requireNonNull(instance, "instance"); method(instance.method()); url(instance.url()); @Nullable String bodyValue = instance.body(); if (bodyValue != null) { body(bodyValue); } putAllHeaders(instance.headers()); return this; } /** * Initializes the value for the {@link HttpRequest#method() method} attribute. *

If not set, this attribute will have a default value as returned by the initializer of {@link HttpRequest#method() method}. * @param method The value for method * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue @JsonProperty public final Builder method(String method) { this.method = Objects.requireNonNull(method, "method"); return this; } /** * Initializes the value for the {@link HttpRequest#url() url} attribute. * @param url The value for url * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue @JsonProperty public final Builder url(String url) { this.url = Objects.requireNonNull(url, "url"); initBits &= ~INIT_BIT_URL; return this; } /** * Initializes the value for the {@link HttpRequest#body() body} attribute. * @param body The value for body (can be {@code null}) * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue @JsonProperty public final Builder body(@Nullable String body) { this.body = body; return this; } /** * Put one entry to the {@link HttpRequest#headers() headers} map. * @param key The key in the headers map * @param value The associated value in the headers map * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final Builder putHeaders(String key, List value) { this.headers.put( Objects.requireNonNull(key, "headers key"), value == null ? Objects.requireNonNull(value, "headers value for key: " + key) : value); optBits |= OPT_BIT_HEADERS; return this; } /** * Put one entry to the {@link HttpRequest#headers() headers} map. Nulls are not permitted * @param entry The key and value entry * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final Builder putHeaders(Map.Entry> entry) { String k = entry.getKey(); List v = entry.getValue(); this.headers.put( Objects.requireNonNull(k, "headers key"), v == null ? Objects.requireNonNull(v, "headers value for key: " + k) : v); optBits |= OPT_BIT_HEADERS; return this; } /** * Sets or replaces all mappings from the specified map as entries for the {@link HttpRequest#headers() headers} map. Nulls are not permitted * @param entries The entries that will be added to the headers map * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue @JsonProperty public final Builder headers(Map> entries) { this.headers.clear(); optBits |= OPT_BIT_HEADERS; return putAllHeaders(entries); } /** * Put all mappings from the specified map as entries to {@link HttpRequest#headers() headers} map. Nulls are not permitted * @param entries The entries that will be added to the headers map * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final Builder putAllHeaders(Map> entries) { for (Map.Entry> e : entries.entrySet()) { String k = e.getKey(); List v = e.getValue(); this.headers.put( Objects.requireNonNull(k, "headers key"), v == null ? Objects.requireNonNull(v, "headers value for key: " + k) : v); } optBits |= OPT_BIT_HEADERS; return this; } /** * Builds a new {@link ImmutableHttpRequest ImmutableHttpRequest}. * @return An immutable instance of HttpRequest * @throws java.lang.IllegalStateException if any required attributes are missing */ public ImmutableHttpRequest build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return new ImmutableHttpRequest(this); } private boolean headersIsSet() { return (optBits & OPT_BIT_HEADERS) != 0; } private String formatRequiredAttributesMessage() { List attributes = new ArrayList<>(); if ((initBits & INIT_BIT_URL) != 0) attributes.add("url"); return "Cannot build HttpRequest, some of required attributes are not set " + attributes; } } private static Map createUnmodifiableMap(boolean checkNulls, boolean skipNulls, Map map) { switch (map.size()) { case 0: return Collections.emptyMap(); case 1: { Map.Entry e = map.entrySet().iterator().next(); K k = e.getKey(); V v = e.getValue(); if (checkNulls) { Objects.requireNonNull(k, "key"); if (v == null) Objects.requireNonNull(v, "value for key: " + k); } if (skipNulls && (k == null || v == null)) { return Collections.emptyMap(); } return Collections.singletonMap(k, v); } default: { Map linkedMap = new LinkedHashMap<>(map.size()); if (skipNulls || checkNulls) { for (Map.Entry e : map.entrySet()) { K k = e.getKey(); V v = e.getValue(); if (skipNulls) { if (k == null || v == null) continue; } else if (checkNulls) { Objects.requireNonNull(k, "key"); if (v == null) Objects.requireNonNull(v, "value for key: " + k); } linkedMap.put(k, v); } } else { linkedMap.putAll(map); } return Collections.unmodifiableMap(linkedMap); } } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy