Skip to content

Instantly share code, notes, and snippets.

@hsyed
Created March 19, 2018 12:04
Show Gist options
  • Save hsyed/e086a647c4d67d96299480ed2202dae2 to your computer and use it in GitHub Desktop.
Save hsyed/e086a647c4d67d96299480ed2202dae2 to your computer and use it in GitHub Desktop.
package com.github.hsyed.k8s.clusters.model;
import com.github.hsyed.k8s.KubernetesClusterType;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.primitives.Booleans;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.annotation.CheckReturnValue;
import javax.annotation.Generated;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
/**
* Immutable implementation of {@link KubernetesServerConfiguration.State}.
* <p>
* Use the builder to create immutable instances:
* {@code ImmutableState.builder()}.
*/
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "KubernetesServerConfiguration.State"})
@Immutable
@CheckReturnValue
public final class ImmutableState
extends KubernetesServerConfiguration.State {
private final KubernetesClusterType clusterType;
private final String kubeConfigContext;
private final String displayName;
private final ImmutableSet<String> enabledTreeFeatures;
private final boolean enabled;
private ImmutableState(ImmutableState.Builder builder) {
this.kubeConfigContext = builder.kubeConfigContext;
this.displayName = builder.displayName;
this.enabledTreeFeatures = builder.enabledTreeFeatures.build();
if (builder.clusterType != null) {
initShim.clusterType(builder.clusterType);
}
if (builder.enabledIsSet()) {
initShim.enabled(builder.enabled);
}
this.clusterType = initShim.clusterType();
this.enabled = initShim.enabled();
this.initShim = null;
}
private ImmutableState(
KubernetesClusterType clusterType,
String kubeConfigContext,
String displayName,
ImmutableSet<String> enabledTreeFeatures,
boolean enabled) {
this.clusterType = clusterType;
this.kubeConfigContext = kubeConfigContext;
this.displayName = displayName;
this.enabledTreeFeatures = enabledTreeFeatures;
this.enabled = enabled;
this.initShim = null;
}
private static final int STAGE_INITIALIZING = -1;
private static final int STAGE_UNINITIALIZED = 0;
private static final int STAGE_INITIALIZED = 1;
private transient volatile InitShim initShim = new InitShim();
private final class InitShim {
private KubernetesClusterType clusterType;
private int clusterTypeBuildStage;
KubernetesClusterType clusterType() {
if (clusterTypeBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (clusterTypeBuildStage == STAGE_UNINITIALIZED) {
clusterTypeBuildStage = STAGE_INITIALIZING;
this.clusterType = Objects.requireNonNull(ImmutableState.super.clusterType(), "clusterType");
clusterTypeBuildStage = STAGE_INITIALIZED;
}
return this.clusterType;
}
void clusterType(KubernetesClusterType clusterType) {
this.clusterType = clusterType;
clusterTypeBuildStage = STAGE_INITIALIZED;
}
private boolean enabled;
private int enabledBuildStage;
boolean enabled() {
if (enabledBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (enabledBuildStage == STAGE_UNINITIALIZED) {
enabledBuildStage = STAGE_INITIALIZING;
this.enabled = ImmutableState.super.enabled();
enabledBuildStage = STAGE_INITIALIZED;
}
return this.enabled;
}
void enabled(boolean enabled) {
this.enabled = enabled;
enabledBuildStage = STAGE_INITIALIZED;
}
private String formatInitCycleMessage() {
ArrayList<String> attributes = Lists.newArrayList();
if (clusterTypeBuildStage == STAGE_INITIALIZING) attributes.add("clusterType");
if (enabledBuildStage == STAGE_INITIALIZING) attributes.add("enabled");
return "Cannot build State, attribute initializers form cycle" + attributes;
}
}
/**
* @return The value of the {@code clusterType} attribute
*/
@Override
public KubernetesClusterType clusterType() {
InitShim shim = this.initShim;
return shim != null
? shim.clusterType()
: this.clusterType;
}
/**
* @return The value of the {@code kubeConfigContext} attribute
*/
@Override
public String getKubeConfigContext() {
return kubeConfigContext;
}
/**
* @return The value of the {@code displayName} attribute
*/
@Override
public String getDisplayName() {
return displayName;
}
/**
* @return The value of the {@code enabledTreeFeatures} attribute
*/
@Override
public ImmutableSet<String> getEnabledTreeFeatures() {
return enabledTreeFeatures;
}
/**
* @return The value of the {@code enabled} attribute
*/
@Override
public boolean enabled() {
InitShim shim = this.initShim;
return shim != null
? shim.enabled()
: this.enabled;
}
/**
* Copy the current immutable object by setting a value for the {@link KubernetesServerConfiguration.State#clusterType() clusterType} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for clusterType
* @return A modified copy of the {@code this} object
*/
public final ImmutableState withClusterType(KubernetesClusterType value) {
if (this.clusterType == value) return this;
KubernetesClusterType newValue = Objects.requireNonNull(value, "clusterType");
return new ImmutableState(newValue, this.kubeConfigContext, this.displayName, this.enabledTreeFeatures, this.enabled);
}
/**
* Copy the current immutable object by setting a value for the {@link KubernetesServerConfiguration.State#getKubeConfigContext() kubeConfigContext} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for kubeConfigContext
* @return A modified copy of the {@code this} object
*/
public final ImmutableState withKubeConfigContext(String value) {
if (this.kubeConfigContext.equals(value)) return this;
String newValue = Objects.requireNonNull(value, "kubeConfigContext");
return new ImmutableState(this.clusterType, newValue, this.displayName, this.enabledTreeFeatures, this.enabled);
}
/**
* Copy the current immutable object by setting a value for the {@link KubernetesServerConfiguration.State#getDisplayName() displayName} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for displayName
* @return A modified copy of the {@code this} object
*/
public final ImmutableState withDisplayName(String value) {
if (this.displayName.equals(value)) return this;
String newValue = Objects.requireNonNull(value, "displayName");
return new ImmutableState(this.clusterType, this.kubeConfigContext, newValue, this.enabledTreeFeatures, this.enabled);
}
/**
* Copy the current immutable object with elements that replace the content of {@link KubernetesServerConfiguration.State#getEnabledTreeFeatures() enabledTreeFeatures}.
* @param elements The elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableState withEnabledTreeFeatures(String... elements) {
ImmutableSet<String> newValue = ImmutableSet.copyOf(elements);
return new ImmutableState(this.clusterType, this.kubeConfigContext, this.displayName, newValue, this.enabled);
}
/**
* Copy the current immutable object with elements that replace the content of {@link KubernetesServerConfiguration.State#getEnabledTreeFeatures() enabledTreeFeatures}.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param elements An iterable of enabledTreeFeatures elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableState withEnabledTreeFeatures(Iterable<String> elements) {
if (this.enabledTreeFeatures == elements) return this;
ImmutableSet<String> newValue = ImmutableSet.copyOf(elements);
return new ImmutableState(this.clusterType, this.kubeConfigContext, this.displayName, newValue, this.enabled);
}
/**
* Copy the current immutable object by setting a value for the {@link KubernetesServerConfiguration.State#enabled() enabled} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for enabled
* @return A modified copy of the {@code this} object
*/
public final ImmutableState withEnabled(boolean value) {
if (this.enabled == value) return this;
return new ImmutableState(this.clusterType, this.kubeConfigContext, this.displayName, this.enabledTreeFeatures, value);
}
/**
* This instance is equal to all instances of {@code ImmutableState} 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 ImmutableState
&& equalTo((ImmutableState) another);
}
private boolean equalTo(ImmutableState another) {
return clusterType.equals(another.clusterType)
&& kubeConfigContext.equals(another.kubeConfigContext)
&& displayName.equals(another.displayName)
&& enabledTreeFeatures.equals(another.enabledTreeFeatures)
&& enabled == another.enabled;
}
/**
* Computes a hash code from attributes: {@code clusterType}, {@code kubeConfigContext}, {@code displayName}, {@code enabledTreeFeatures}, {@code enabled}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + clusterType.hashCode();
h += (h << 5) + kubeConfigContext.hashCode();
h += (h << 5) + displayName.hashCode();
h += (h << 5) + enabledTreeFeatures.hashCode();
h += (h << 5) + Booleans.hashCode(enabled);
return h;
}
/**
* Prints the immutable value {@code State} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("State")
.omitNullValues()
.add("clusterType", clusterType)
.add("kubeConfigContext", kubeConfigContext)
.add("displayName", displayName)
.add("enabledTreeFeatures", enabledTreeFeatures)
.add("enabled", enabled)
.toString();
}
/**
* Creates an immutable copy of a {@link KubernetesServerConfiguration.State} 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 State instance
*/
public static ImmutableState copyOf(KubernetesServerConfiguration.State instance) {
if (instance instanceof ImmutableState) {
return (ImmutableState) instance;
}
return ImmutableState.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableState ImmutableState}.
* @return A new ImmutableState builder
*/
public static ImmutableState.Builder builder() {
return new ImmutableState.Builder();
}
/**
* Builds instances of type {@link ImmutableState ImmutableState}.
* Initialize attributes and then invoke the {@link #build()} method to create an
* immutable instance.
* <p><em>{@code Builder} is not thread-safe and generally should not be stored in a field or collection,
* but instead used immediately to create instances.</em>
*/
@NotThreadSafe
public static final class Builder {
private static final long INIT_BIT_KUBE_CONFIG_CONTEXT = 0x1L;
private static final long INIT_BIT_DISPLAY_NAME = 0x2L;
private static final long OPT_BIT_ENABLED = 0x1L;
private long initBits = 0x3L;
private long optBits;
private @Nullable KubernetesClusterType clusterType;
private @Nullable String kubeConfigContext;
private @Nullable String displayName;
private ImmutableSet.Builder<String> enabledTreeFeatures = ImmutableSet.builder();
private boolean enabled;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code State} 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(KubernetesServerConfiguration.State instance) {
Objects.requireNonNull(instance, "instance");
clusterType(instance.clusterType());
kubeConfigContext(instance.getKubeConfigContext());
displayName(instance.getDisplayName());
addAllEnabledTreeFeatures(instance.getEnabledTreeFeatures());
enabled(instance.enabled());
return this;
}
/**
* Initializes the value for the {@link KubernetesServerConfiguration.State#clusterType() clusterType} attribute.
* <p><em>If not set, this attribute will have a default value as returned by the initializer of {@link KubernetesServerConfiguration.State#clusterType() clusterType}.</em>
* @param clusterType The value for clusterType
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder clusterType(KubernetesClusterType clusterType) {
this.clusterType = Objects.requireNonNull(clusterType, "clusterType");
return this;
}
/**
* Initializes the value for the {@link KubernetesServerConfiguration.State#getKubeConfigContext() kubeConfigContext} attribute.
* @param kubeConfigContext The value for kubeConfigContext
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder kubeConfigContext(String kubeConfigContext) {
this.kubeConfigContext = Objects.requireNonNull(kubeConfigContext, "kubeConfigContext");
initBits &= ~INIT_BIT_KUBE_CONFIG_CONTEXT;
return this;
}
/**
* Initializes the value for the {@link KubernetesServerConfiguration.State#getDisplayName() displayName} attribute.
* @param displayName The value for displayName
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder displayName(String displayName) {
this.displayName = Objects.requireNonNull(displayName, "displayName");
initBits &= ~INIT_BIT_DISPLAY_NAME;
return this;
}
/**
* Adds one element to {@link KubernetesServerConfiguration.State#getEnabledTreeFeatures() enabledTreeFeatures} set.
* @param element A enabledTreeFeatures element
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addEnabledTreeFeatures(String element) {
this.enabledTreeFeatures.add(element);
return this;
}
/**
* Adds elements to {@link KubernetesServerConfiguration.State#getEnabledTreeFeatures() enabledTreeFeatures} set.
* @param elements An array of enabledTreeFeatures elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addEnabledTreeFeatures(String... elements) {
this.enabledTreeFeatures.add(elements);
return this;
}
/**
* Sets or replaces all elements for {@link KubernetesServerConfiguration.State#getEnabledTreeFeatures() enabledTreeFeatures} set.
* @param elements An iterable of enabledTreeFeatures elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder enabledTreeFeatures(Iterable<String> elements) {
this.enabledTreeFeatures = ImmutableSet.builder();
return addAllEnabledTreeFeatures(elements);
}
/**
* Adds elements to {@link KubernetesServerConfiguration.State#getEnabledTreeFeatures() enabledTreeFeatures} set.
* @param elements An iterable of enabledTreeFeatures elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addAllEnabledTreeFeatures(Iterable<String> elements) {
this.enabledTreeFeatures.addAll(elements);
return this;
}
/**
* Initializes the value for the {@link KubernetesServerConfiguration.State#enabled() enabled} attribute.
* <p><em>If not set, this attribute will have a default value as returned by the initializer of {@link KubernetesServerConfiguration.State#enabled() enabled}.</em>
* @param enabled The value for enabled
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder enabled(boolean enabled) {
this.enabled = enabled;
optBits |= OPT_BIT_ENABLED;
return this;
}
/**
* Builds a new {@link ImmutableState ImmutableState}.
* @return An immutable instance of State
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableState build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableState(this);
}
private boolean enabledIsSet() {
return (optBits & OPT_BIT_ENABLED) != 0;
}
private String formatRequiredAttributesMessage() {
List<String> attributes = Lists.newArrayList();
if ((initBits & INIT_BIT_KUBE_CONFIG_CONTEXT) != 0) attributes.add("kubeConfigContext");
if ((initBits & INIT_BIT_DISPLAY_NAME) != 0) attributes.add("displayName");
return "Cannot build State, some of required attributes are not set " + attributes;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment