|
| 1 | +package com.palantir.conjure.java.api.config.service; |
| 2 | + |
| 3 | +import com.google.errorprone.annotations.CanIgnoreReturnValue; |
| 4 | +import com.google.errorprone.annotations.Var; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Objects; |
| 8 | +import javax.annotation.CheckReturnValue; |
| 9 | +import javax.annotation.Generated; |
| 10 | +import javax.annotation.Nullable; |
| 11 | +import javax.annotation.ParametersAreNonnullByDefault; |
| 12 | +import javax.annotation.concurrent.Immutable; |
| 13 | +import javax.annotation.concurrent.NotThreadSafe; |
| 14 | + |
| 15 | +/** |
| 16 | + * Immutable implementation of {@link UserAgent.Agent}. |
| 17 | + * <p> |
| 18 | + * Use the builder to create immutable instances: |
| 19 | + * {@code ImmutableAgent.builder()}. |
| 20 | + */ |
| 21 | +@SuppressWarnings({"all"}) |
| 22 | +@ParametersAreNonnullByDefault |
| 23 | +@Generated("org.immutables.processor.ProxyProcessor") |
| 24 | +@org.immutables.value.Generated(from = "UserAgent.Agent", generator = "Immutables") |
| 25 | +@Immutable |
| 26 | +@CheckReturnValue |
| 27 | +final class ImmutableAgent implements UserAgent.Agent { |
| 28 | + private final String name; |
| 29 | + private final String version; |
| 30 | + |
| 31 | + private ImmutableAgent(String name, String version) { |
| 32 | + this.name = name; |
| 33 | + this.version = version; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @return The value of the {@code name} attribute |
| 38 | + */ |
| 39 | + @Override |
| 40 | + public String name() { |
| 41 | + return name; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @return The value of the {@code version} attribute |
| 46 | + */ |
| 47 | + @Override |
| 48 | + public String version() { |
| 49 | + return version; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Copy the current immutable object by setting a value for the {@link UserAgent.Agent#name() name} attribute. |
| 54 | + * An equals check used to prevent copying of the same value by returning {@code this}. |
| 55 | + * @param value A new value for name |
| 56 | + * @return A modified copy of the {@code this} object |
| 57 | + */ |
| 58 | + public final ImmutableAgent withName(String value) { |
| 59 | + if (this.name.equals(value)) return this; |
| 60 | + String newValue = Objects.requireNonNull(value, "name"); |
| 61 | + return validate(new ImmutableAgent(newValue, this.version)); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Copy the current immutable object by setting a value for the {@link UserAgent.Agent#version() version} attribute. |
| 66 | + * An equals check used to prevent copying of the same value by returning {@code this}. |
| 67 | + * @param value A new value for version |
| 68 | + * @return A modified copy of the {@code this} object |
| 69 | + */ |
| 70 | + public final ImmutableAgent withVersion(String value) { |
| 71 | + if (this.version.equals(value)) return this; |
| 72 | + String newValue = Objects.requireNonNull(value, "version"); |
| 73 | + return validate(new ImmutableAgent(this.name, newValue)); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * This instance is equal to all instances of {@code ImmutableAgent} that have equal attribute values. |
| 78 | + * @return {@code true} if {@code this} is equal to {@code another} instance |
| 79 | + */ |
| 80 | + @Override |
| 81 | + public boolean equals(@Nullable Object another) { |
| 82 | + if (this == another) return true; |
| 83 | + return another instanceof ImmutableAgent |
| 84 | + && equalTo((ImmutableAgent) another); |
| 85 | + } |
| 86 | + |
| 87 | + private boolean equalTo(ImmutableAgent another) { |
| 88 | + return name.equals(another.name) |
| 89 | + && version.equals(another.version); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Computes a hash code from attributes: {@code name}, {@code version}. |
| 94 | + * @return hashCode value |
| 95 | + */ |
| 96 | + @Override |
| 97 | + public int hashCode() { |
| 98 | + @Var int h = 5381; |
| 99 | + h += (h << 5) + name.hashCode(); |
| 100 | + h += (h << 5) + version.hashCode(); |
| 101 | + return h; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Prints the immutable value {@code Agent} with attribute values. |
| 106 | + * @return A string representation of the value |
| 107 | + */ |
| 108 | + @Override |
| 109 | + public String toString() { |
| 110 | + return "Agent{" |
| 111 | + + "name=" + name |
| 112 | + + ", version=" + version |
| 113 | + + "}"; |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + private static ImmutableAgent validate(ImmutableAgent instance) { |
| 118 | + instance.check(); |
| 119 | + return instance; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Creates an immutable copy of a {@link UserAgent.Agent} value. |
| 124 | + * Uses accessors to get values to initialize the new immutable instance. |
| 125 | + * If an instance is already immutable, it is returned as is. |
| 126 | + * @param instance The instance to copy |
| 127 | + * @return A copied immutable Agent instance |
| 128 | + */ |
| 129 | + public static UserAgent.Agent copyOf(UserAgent.Agent instance) { |
| 130 | + if (instance instanceof ImmutableAgent) { |
| 131 | + return (ImmutableAgent) instance; |
| 132 | + } |
| 133 | + return ImmutableAgent.builder() |
| 134 | + .from(instance) |
| 135 | + .build(); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Creates a builder for {@link UserAgent.Agent Agent}. |
| 140 | + * @return A new Agent builder |
| 141 | + */ |
| 142 | + public static ImmutableAgent.Builder builder() { |
| 143 | + return new ImmutableAgent.Builder(); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Builds instances of type {@link UserAgent.Agent Agent}. |
| 148 | + * Initialize attributes and then invoke the {@link #build()} method to create an |
| 149 | + * immutable instance. |
| 150 | + * <p><em>{@code Builder} is not thread-safe and generally should not be stored in a field or collection, |
| 151 | + * but instead used immediately to create instances.</em> |
| 152 | + */ |
| 153 | + @org.immutables.value.Generated(from = "UserAgent.Agent", generator = "Immutables") |
| 154 | + @NotThreadSafe |
| 155 | + public static final class Builder { |
| 156 | + private static final long INIT_BIT_NAME = 0x1L; |
| 157 | + private static final long INIT_BIT_VERSION = 0x2L; |
| 158 | + private long initBits = 0x3L; |
| 159 | + |
| 160 | + private @Nullable String name; |
| 161 | + private @Nullable String version; |
| 162 | + |
| 163 | + private Builder() { |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Fill a builder with attribute values from the provided {@code Agent} instance. |
| 168 | + * Regular attribute values will be replaced with those from the given instance. |
| 169 | + * Absent optional values will not replace present values. |
| 170 | + * @param instance The instance from which to copy values |
| 171 | + * @return {@code this} builder for use in a chained invocation |
| 172 | + */ |
| 173 | + @CanIgnoreReturnValue |
| 174 | + public final Builder from(UserAgent.Agent instance) { |
| 175 | + Objects.requireNonNull(instance, "instance"); |
| 176 | + name(instance.name()); |
| 177 | + version(instance.version()); |
| 178 | + return this; |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Initializes the value for the {@link UserAgent.Agent#name() name} attribute. |
| 183 | + * @param name The value for name |
| 184 | + * @return {@code this} builder for use in a chained invocation |
| 185 | + */ |
| 186 | + @CanIgnoreReturnValue |
| 187 | + public final Builder name(String name) { |
| 188 | + this.name = Objects.requireNonNull(name, "name"); |
| 189 | + initBits &= ~INIT_BIT_NAME; |
| 190 | + return this; |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * Initializes the value for the {@link UserAgent.Agent#version() version} attribute. |
| 195 | + * @param version The value for version |
| 196 | + * @return {@code this} builder for use in a chained invocation |
| 197 | + */ |
| 198 | + @CanIgnoreReturnValue |
| 199 | + public final Builder version(String version) { |
| 200 | + this.version = Objects.requireNonNull(version, "version"); |
| 201 | + initBits &= ~INIT_BIT_VERSION; |
| 202 | + return this; |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Builds a new {@link UserAgent.Agent Agent}. |
| 207 | + * @return An immutable instance of Agent |
| 208 | + * @throws java.lang.IllegalStateException if any required attributes are missing |
| 209 | + */ |
| 210 | + public UserAgent.Agent build() { |
| 211 | + if (initBits != 0) { |
| 212 | + throw new IllegalStateException(formatRequiredAttributesMessage()); |
| 213 | + } |
| 214 | + return ImmutableAgent.validate(new ImmutableAgent(name, version)); |
| 215 | + } |
| 216 | + |
| 217 | + private String formatRequiredAttributesMessage() { |
| 218 | + List<String> attributes = new ArrayList<>(); |
| 219 | + if ((initBits & INIT_BIT_NAME) != 0) attributes.add("name"); |
| 220 | + if ((initBits & INIT_BIT_VERSION) != 0) attributes.add("version"); |
| 221 | + return "Cannot build Agent, some of required attributes are not set " + attributes; |
| 222 | + } |
| 223 | + } |
| 224 | +} |
0 commit comments