edu.isi.nlp.ImmutableStringWithNonBmp Maven / Gradle / Ivy
package edu.isi.nlp;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.List;
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 StringWithNonBmp}.
*
* Use the builder to create immutable instances:
* {@code new StringWithNonBmp.Builder()}.
*/
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "StringWithNonBmp"})
@Immutable
final class ImmutableStringWithNonBmp extends StringWithNonBmp {
private final String utf16CodeUnits;
private final int lengthInUtf16CodeUnits;
private final int lengthInCodePoints;
private ImmutableStringWithNonBmp(String utf16CodeUnits) {
this.utf16CodeUnits = utf16CodeUnits;
this.lengthInUtf16CodeUnits = initShim.lengthInUtf16CodeUnits();
this.lengthInCodePoints = initShim.lengthInCodePoints();
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 int lengthInUtf16CodeUnits;
private int lengthInUtf16CodeUnitsStage;
int lengthInUtf16CodeUnits() {
if (lengthInUtf16CodeUnitsStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (lengthInUtf16CodeUnitsStage == STAGE_UNINITIALIZED) {
lengthInUtf16CodeUnitsStage = STAGE_INITIALIZING;
this.lengthInUtf16CodeUnits = ImmutableStringWithNonBmp.super.lengthInUtf16CodeUnits();
lengthInUtf16CodeUnitsStage = STAGE_INITIALIZED;
}
return this.lengthInUtf16CodeUnits;
}
private int lengthInCodePoints;
private int lengthInCodePointsStage;
int lengthInCodePoints() {
if (lengthInCodePointsStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (lengthInCodePointsStage == STAGE_UNINITIALIZED) {
lengthInCodePointsStage = STAGE_INITIALIZING;
this.lengthInCodePoints = ImmutableStringWithNonBmp.super.lengthInCodePoints();
lengthInCodePointsStage = STAGE_INITIALIZED;
}
return this.lengthInCodePoints;
}
private String formatInitCycleMessage() {
ArrayList attributes = Lists.newArrayList();
if (lengthInUtf16CodeUnitsStage == STAGE_INITIALIZING) attributes.add("lengthInUtf16CodeUnits");
if (lengthInCodePointsStage == STAGE_INITIALIZING) attributes.add("lengthInCodePoints");
return "Cannot build StringWithNonBmp, attribute initializers form cycle" + attributes;
}
}
/**
* @return The value of the {@code utf16CodeUnits} attribute
*/
@Override
public String utf16CodeUnits() {
return utf16CodeUnits;
}
/**
* @return The computed-at-construction value of the {@code lengthInUtf16CodeUnits} attribute
*/
@Override
public int lengthInUtf16CodeUnits() {
InitShim shim = this.initShim;
return shim != null
? shim.lengthInUtf16CodeUnits()
: this.lengthInUtf16CodeUnits;
}
/**
* @return The computed-at-construction value of the {@code lengthInCodePoints} attribute
*/
@Override
public int lengthInCodePoints() {
InitShim shim = this.initShim;
return shim != null
? shim.lengthInCodePoints()
: this.lengthInCodePoints;
}
/**
* Copy the current immutable object by setting a value for the {@link StringWithNonBmp#utf16CodeUnits() utf16CodeUnits} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for utf16CodeUnits
* @return A modified copy of the {@code this} object
*/
public final ImmutableStringWithNonBmp withUtf16CodeUnits(String value) {
if (this.utf16CodeUnits.equals(value)) return this;
String newValue = Preconditions.checkNotNull(value, "utf16CodeUnits");
return new ImmutableStringWithNonBmp(newValue);
}
/**
* Creates an immutable copy of a {@link StringWithNonBmp} 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 StringWithNonBmp instance
*/
public static StringWithNonBmp copyOf(StringWithNonBmp instance) {
if (instance instanceof ImmutableStringWithNonBmp) {
return (ImmutableStringWithNonBmp) instance;
}
return new StringWithNonBmp.Builder()
.from(instance)
.build();
}
/**
* Builds instances of type {@link StringWithNonBmp StringWithNonBmp}.
* 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.
*/
@NotThreadSafe
static class Builder {
private static final long INIT_BIT_UTF16_CODE_UNITS = 0x1L;
private long initBits = 0x1L;
private @Nullable String utf16CodeUnits;
/**
* Creates a builder for {@link StringWithNonBmp StringWithNonBmp} instances.
*/
Builder() {
if (!(this instanceof StringWithNonBmp.Builder)) {
throw new UnsupportedOperationException("Use: new StringWithNonBmp.Builder()");
}
}
/**
* Fill a builder with attribute values from the provided {@code edu.isi.nlp.StringWithNonBmp} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
public final StringWithNonBmp.Builder from(StringWithNonBmp instance) {
Preconditions.checkNotNull(instance, "instance");
from((Object) instance);
return (StringWithNonBmp.Builder) this;
}
/**
* Fill a builder with attribute values from the provided {@code edu.isi.nlp.UnicodeFriendlyString} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
public final StringWithNonBmp.Builder from(UnicodeFriendlyString instance) {
Preconditions.checkNotNull(instance, "instance");
from((Object) instance);
return (StringWithNonBmp.Builder) this;
}
private void from(Object object) {
long bits = 0;
if (object instanceof StringWithNonBmp) {
StringWithNonBmp instance = (StringWithNonBmp) object;
if ((bits & 0x1L) == 0) {
utf16CodeUnits(instance.utf16CodeUnits());
bits |= 0x1L;
}
}
if (object instanceof UnicodeFriendlyString) {
UnicodeFriendlyString instance = (UnicodeFriendlyString) object;
if ((bits & 0x1L) == 0) {
utf16CodeUnits(instance.utf16CodeUnits());
bits |= 0x1L;
}
}
}
/**
* Initializes the value for the {@link StringWithNonBmp#utf16CodeUnits() utf16CodeUnits} attribute.
* @param utf16CodeUnits The value for utf16CodeUnits
* @return {@code this} builder for use in a chained invocation
*/
public final StringWithNonBmp.Builder utf16CodeUnits(String utf16CodeUnits) {
this.utf16CodeUnits = Preconditions.checkNotNull(utf16CodeUnits, "utf16CodeUnits");
initBits &= ~INIT_BIT_UTF16_CODE_UNITS;
return (StringWithNonBmp.Builder) this;
}
/**
* Builds a new {@link StringWithNonBmp StringWithNonBmp}.
* @return An immutable instance of StringWithNonBmp
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public StringWithNonBmp build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableStringWithNonBmp(utf16CodeUnits);
}
private String formatRequiredAttributesMessage() {
List attributes = Lists.newArrayList();
if ((initBits & INIT_BIT_UTF16_CODE_UNITS) != 0) attributes.add("utf16CodeUnits");
return "Cannot build StringWithNonBmp, some of required attributes are not set " + attributes;
}
}
}