org.projectnessie.versioned.mongodb.ImmutableMongoStoreConfig Maven / Gradle / Ivy
package org.projectnessie.versioned.mongodb;
import com.google.common.base.MoreObjects;
import com.google.common.primitives.Booleans;
import com.google.common.primitives.Longs;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Var;
import java.util.ArrayList;
import java.util.List;
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 MongoStoreConfig}.
*
* Use the builder to create immutable instances:
* {@code ImmutableMongoStoreConfig.builder()}.
*/
@Generated(from = "MongoStoreConfig", generator = "Immutables")
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@javax.annotation.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
@CheckReturnValue
public final class ImmutableMongoStoreConfig extends MongoStoreConfig {
private final String connectionString;
private final String databaseName;
private final String tablePrefix;
private final long timeoutMs;
private final boolean initializeDatabase;
private ImmutableMongoStoreConfig(ImmutableMongoStoreConfig.Builder builder) {
this.connectionString = builder.connectionString;
if (builder.databaseName != null) {
initShim.databaseName(builder.databaseName);
}
if (builder.tablePrefix != null) {
initShim.tablePrefix(builder.tablePrefix);
}
if (builder.timeoutMsIsSet()) {
initShim.timeoutMs(builder.timeoutMs);
}
if (builder.initializeDatabaseIsSet()) {
initShim.initializeDatabase(builder.initializeDatabase);
}
this.databaseName = initShim.getDatabaseName();
this.tablePrefix = initShim.getTablePrefix();
this.timeoutMs = initShim.getTimeoutMs();
this.initializeDatabase = initShim.initializeDatabase();
this.initShim = null;
}
private ImmutableMongoStoreConfig(
String connectionString,
String databaseName,
String tablePrefix,
long timeoutMs,
boolean initializeDatabase) {
this.connectionString = connectionString;
this.databaseName = databaseName;
this.tablePrefix = tablePrefix;
this.timeoutMs = timeoutMs;
this.initializeDatabase = initializeDatabase;
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 = "MongoStoreConfig", generator = "Immutables")
private final class InitShim {
private byte databaseNameBuildStage = STAGE_UNINITIALIZED;
private String databaseName;
String getDatabaseName() {
if (databaseNameBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (databaseNameBuildStage == STAGE_UNINITIALIZED) {
databaseNameBuildStage = STAGE_INITIALIZING;
this.databaseName = Objects.requireNonNull(ImmutableMongoStoreConfig.super.getDatabaseName(), "databaseName");
databaseNameBuildStage = STAGE_INITIALIZED;
}
return this.databaseName;
}
void databaseName(String databaseName) {
this.databaseName = databaseName;
databaseNameBuildStage = STAGE_INITIALIZED;
}
private byte tablePrefixBuildStage = STAGE_UNINITIALIZED;
private String tablePrefix;
String getTablePrefix() {
if (tablePrefixBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (tablePrefixBuildStage == STAGE_UNINITIALIZED) {
tablePrefixBuildStage = STAGE_INITIALIZING;
this.tablePrefix = Objects.requireNonNull(ImmutableMongoStoreConfig.super.getTablePrefix(), "tablePrefix");
tablePrefixBuildStage = STAGE_INITIALIZED;
}
return this.tablePrefix;
}
void tablePrefix(String tablePrefix) {
this.tablePrefix = tablePrefix;
tablePrefixBuildStage = STAGE_INITIALIZED;
}
private byte timeoutMsBuildStage = STAGE_UNINITIALIZED;
private long timeoutMs;
long getTimeoutMs() {
if (timeoutMsBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (timeoutMsBuildStage == STAGE_UNINITIALIZED) {
timeoutMsBuildStage = STAGE_INITIALIZING;
this.timeoutMs = ImmutableMongoStoreConfig.super.getTimeoutMs();
timeoutMsBuildStage = STAGE_INITIALIZED;
}
return this.timeoutMs;
}
void timeoutMs(long timeoutMs) {
this.timeoutMs = timeoutMs;
timeoutMsBuildStage = STAGE_INITIALIZED;
}
private byte initializeDatabaseBuildStage = STAGE_UNINITIALIZED;
private boolean initializeDatabase;
boolean initializeDatabase() {
if (initializeDatabaseBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (initializeDatabaseBuildStage == STAGE_UNINITIALIZED) {
initializeDatabaseBuildStage = STAGE_INITIALIZING;
this.initializeDatabase = ImmutableMongoStoreConfig.super.initializeDatabase();
initializeDatabaseBuildStage = STAGE_INITIALIZED;
}
return this.initializeDatabase;
}
void initializeDatabase(boolean initializeDatabase) {
this.initializeDatabase = initializeDatabase;
initializeDatabaseBuildStage = STAGE_INITIALIZED;
}
private String formatInitCycleMessage() {
List attributes = new ArrayList<>();
if (databaseNameBuildStage == STAGE_INITIALIZING) attributes.add("databaseName");
if (tablePrefixBuildStage == STAGE_INITIALIZING) attributes.add("tablePrefix");
if (timeoutMsBuildStage == STAGE_INITIALIZING) attributes.add("timeoutMs");
if (initializeDatabaseBuildStage == STAGE_INITIALIZING) attributes.add("initializeDatabase");
return "Cannot build MongoStoreConfig, attribute initializers form cycle " + attributes;
}
}
/**
* @return The value of the {@code connectionString} attribute
*/
@Override
public String getConnectionString() {
return connectionString;
}
/**
* @return The value of the {@code databaseName} attribute
*/
@Override
public String getDatabaseName() {
InitShim shim = this.initShim;
return shim != null
? shim.getDatabaseName()
: this.databaseName;
}
/**
* @return The value of the {@code tablePrefix} attribute
*/
@Override
public String getTablePrefix() {
InitShim shim = this.initShim;
return shim != null
? shim.getTablePrefix()
: this.tablePrefix;
}
/**
* @return The value of the {@code timeoutMs} attribute
*/
@Override
public long getTimeoutMs() {
InitShim shim = this.initShim;
return shim != null
? shim.getTimeoutMs()
: this.timeoutMs;
}
/**
* @return The value of the {@code initializeDatabase} attribute
*/
@Override
public boolean initializeDatabase() {
InitShim shim = this.initShim;
return shim != null
? shim.initializeDatabase()
: this.initializeDatabase;
}
/**
* Copy the current immutable object by setting a value for the {@link MongoStoreConfig#getConnectionString() connectionString} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for connectionString
* @return A modified copy of the {@code this} object
*/
public final ImmutableMongoStoreConfig withConnectionString(String value) {
String newValue = Objects.requireNonNull(value, "connectionString");
if (this.connectionString.equals(newValue)) return this;
return new ImmutableMongoStoreConfig(newValue, this.databaseName, this.tablePrefix, this.timeoutMs, this.initializeDatabase);
}
/**
* Copy the current immutable object by setting a value for the {@link MongoStoreConfig#getDatabaseName() databaseName} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for databaseName
* @return A modified copy of the {@code this} object
*/
public final ImmutableMongoStoreConfig withDatabaseName(String value) {
String newValue = Objects.requireNonNull(value, "databaseName");
if (this.databaseName.equals(newValue)) return this;
return new ImmutableMongoStoreConfig(this.connectionString, newValue, this.tablePrefix, this.timeoutMs, this.initializeDatabase);
}
/**
* Copy the current immutable object by setting a value for the {@link MongoStoreConfig#getTablePrefix() tablePrefix} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for tablePrefix
* @return A modified copy of the {@code this} object
*/
public final ImmutableMongoStoreConfig withTablePrefix(String value) {
String newValue = Objects.requireNonNull(value, "tablePrefix");
if (this.tablePrefix.equals(newValue)) return this;
return new ImmutableMongoStoreConfig(this.connectionString, this.databaseName, newValue, this.timeoutMs, this.initializeDatabase);
}
/**
* Copy the current immutable object by setting a value for the {@link MongoStoreConfig#getTimeoutMs() timeoutMs} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for timeoutMs
* @return A modified copy of the {@code this} object
*/
public final ImmutableMongoStoreConfig withTimeoutMs(long value) {
if (this.timeoutMs == value) return this;
return new ImmutableMongoStoreConfig(this.connectionString, this.databaseName, this.tablePrefix, value, this.initializeDatabase);
}
/**
* Copy the current immutable object by setting a value for the {@link MongoStoreConfig#initializeDatabase() initializeDatabase} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for initializeDatabase
* @return A modified copy of the {@code this} object
*/
public final ImmutableMongoStoreConfig withInitializeDatabase(boolean value) {
if (this.initializeDatabase == value) return this;
return new ImmutableMongoStoreConfig(this.connectionString, this.databaseName, this.tablePrefix, this.timeoutMs, value);
}
/**
* This instance is equal to all instances of {@code ImmutableMongoStoreConfig} 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 ImmutableMongoStoreConfig
&& equalTo((ImmutableMongoStoreConfig) another);
}
private boolean equalTo(ImmutableMongoStoreConfig another) {
return connectionString.equals(another.connectionString)
&& databaseName.equals(another.databaseName)
&& tablePrefix.equals(another.tablePrefix)
&& timeoutMs == another.timeoutMs
&& initializeDatabase == another.initializeDatabase;
}
/**
* Computes a hash code from attributes: {@code connectionString}, {@code databaseName}, {@code tablePrefix}, {@code timeoutMs}, {@code initializeDatabase}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + connectionString.hashCode();
h += (h << 5) + databaseName.hashCode();
h += (h << 5) + tablePrefix.hashCode();
h += (h << 5) + Longs.hashCode(timeoutMs);
h += (h << 5) + Booleans.hashCode(initializeDatabase);
return h;
}
/**
* Prints the immutable value {@code MongoStoreConfig} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("MongoStoreConfig")
.omitNullValues()
.add("connectionString", connectionString)
.add("databaseName", databaseName)
.add("tablePrefix", tablePrefix)
.add("timeoutMs", timeoutMs)
.add("initializeDatabase", initializeDatabase)
.toString();
}
/**
* Creates an immutable copy of a {@link MongoStoreConfig} 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 MongoStoreConfig instance
*/
public static ImmutableMongoStoreConfig copyOf(MongoStoreConfig instance) {
if (instance instanceof ImmutableMongoStoreConfig) {
return (ImmutableMongoStoreConfig) instance;
}
return ImmutableMongoStoreConfig.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableMongoStoreConfig ImmutableMongoStoreConfig}.
*
* ImmutableMongoStoreConfig.builder()
* .connectionString(String) // required {@link MongoStoreConfig#getConnectionString() connectionString}
* .databaseName(String) // optional {@link MongoStoreConfig#getDatabaseName() databaseName}
* .tablePrefix(String) // optional {@link MongoStoreConfig#getTablePrefix() tablePrefix}
* .timeoutMs(long) // optional {@link MongoStoreConfig#getTimeoutMs() timeoutMs}
* .initializeDatabase(boolean) // optional {@link MongoStoreConfig#initializeDatabase() initializeDatabase}
* .build();
*
* @return A new ImmutableMongoStoreConfig builder
*/
public static ImmutableMongoStoreConfig.Builder builder() {
return new ImmutableMongoStoreConfig.Builder();
}
/**
* Builds instances of type {@link ImmutableMongoStoreConfig ImmutableMongoStoreConfig}.
* 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 = "MongoStoreConfig", generator = "Immutables")
@NotThreadSafe
public static final class Builder {
private static final long INIT_BIT_CONNECTION_STRING = 0x1L;
private static final long OPT_BIT_TIMEOUT_MS = 0x1L;
private static final long OPT_BIT_INITIALIZE_DATABASE = 0x2L;
private long initBits = 0x1L;
private long optBits;
private @Nullable String connectionString;
private @Nullable String databaseName;
private @Nullable String tablePrefix;
private long timeoutMs;
private boolean initializeDatabase;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code MongoStoreConfig} instance.
* Regular attribute values will be replaced with those from the given instance.
* Absent optional values will not replace present values.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder from(MongoStoreConfig instance) {
Objects.requireNonNull(instance, "instance");
connectionString(instance.getConnectionString());
databaseName(instance.getDatabaseName());
tablePrefix(instance.getTablePrefix());
timeoutMs(instance.getTimeoutMs());
initializeDatabase(instance.initializeDatabase());
return this;
}
/**
* Initializes the value for the {@link MongoStoreConfig#getConnectionString() connectionString} attribute.
* @param connectionString The value for connectionString
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder connectionString(String connectionString) {
this.connectionString = Objects.requireNonNull(connectionString, "connectionString");
initBits &= ~INIT_BIT_CONNECTION_STRING;
return this;
}
/**
* Initializes the value for the {@link MongoStoreConfig#getDatabaseName() databaseName} attribute.
*
If not set, this attribute will have a default value as returned by the initializer of {@link MongoStoreConfig#getDatabaseName() databaseName}.
* @param databaseName The value for databaseName
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder databaseName(String databaseName) {
this.databaseName = Objects.requireNonNull(databaseName, "databaseName");
return this;
}
/**
* Initializes the value for the {@link MongoStoreConfig#getTablePrefix() tablePrefix} attribute.
*
If not set, this attribute will have a default value as returned by the initializer of {@link MongoStoreConfig#getTablePrefix() tablePrefix}.
* @param tablePrefix The value for tablePrefix
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder tablePrefix(String tablePrefix) {
this.tablePrefix = Objects.requireNonNull(tablePrefix, "tablePrefix");
return this;
}
/**
* Initializes the value for the {@link MongoStoreConfig#getTimeoutMs() timeoutMs} attribute.
*
If not set, this attribute will have a default value as returned by the initializer of {@link MongoStoreConfig#getTimeoutMs() timeoutMs}.
* @param timeoutMs The value for timeoutMs
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder timeoutMs(long timeoutMs) {
this.timeoutMs = timeoutMs;
optBits |= OPT_BIT_TIMEOUT_MS;
return this;
}
/**
* Initializes the value for the {@link MongoStoreConfig#initializeDatabase() initializeDatabase} attribute.
*
If not set, this attribute will have a default value as returned by the initializer of {@link MongoStoreConfig#initializeDatabase() initializeDatabase}.
* @param initializeDatabase The value for initializeDatabase
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder initializeDatabase(boolean initializeDatabase) {
this.initializeDatabase = initializeDatabase;
optBits |= OPT_BIT_INITIALIZE_DATABASE;
return this;
}
/**
* Builds a new {@link ImmutableMongoStoreConfig ImmutableMongoStoreConfig}.
* @return An immutable instance of MongoStoreConfig
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableMongoStoreConfig build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableMongoStoreConfig(this);
}
private boolean timeoutMsIsSet() {
return (optBits & OPT_BIT_TIMEOUT_MS) != 0;
}
private boolean initializeDatabaseIsSet() {
return (optBits & OPT_BIT_INITIALIZE_DATABASE) != 0;
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_CONNECTION_STRING) != 0) attributes.add("connectionString");
return "Cannot build MongoStoreConfig, some of required attributes are not set " + attributes;
}
}
}