org.cloudfoundry.multiapps.controller.process.util.ImmutableFileEntryProperties Maven / Gradle / Ivy
package org.cloudfoundry.multiapps.controller.process.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* Immutable implementation of {@link FileEntryProperties}.
*
* Use the builder to create immutable instances:
* {@code ImmutableFileEntryProperties.builder()}.
*/
@SuppressWarnings({"all"})
public final class ImmutableFileEntryProperties
implements FileEntryProperties {
private final String guid;
private final String name;
private final String spaceGuid;
private final long maxFileSizeInBytes;
private ImmutableFileEntryProperties(String guid, String name, String spaceGuid, long maxFileSizeInBytes) {
this.guid = guid;
this.name = name;
this.spaceGuid = spaceGuid;
this.maxFileSizeInBytes = maxFileSizeInBytes;
}
/**
* @return The value of the {@code guid} attribute
*/
@Override
public String getGuid() {
return guid;
}
/**
* @return The value of the {@code name} attribute
*/
@Override
public String getName() {
return name;
}
/**
* @return The value of the {@code spaceGuid} attribute
*/
@Override
public String getSpaceGuid() {
return spaceGuid;
}
/**
* @return The value of the {@code maxFileSizeInBytes} attribute
*/
@Override
public long getMaxFileSizeInBytes() {
return maxFileSizeInBytes;
}
/**
* Copy the current immutable object by setting a value for the {@link FileEntryProperties#getGuid() guid} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for guid
* @return A modified copy of the {@code this} object
*/
public final ImmutableFileEntryProperties withGuid(String value) {
String newValue = Objects.requireNonNull(value, "guid");
if (this.guid.equals(newValue)) return this;
return new ImmutableFileEntryProperties(newValue, this.name, this.spaceGuid, this.maxFileSizeInBytes);
}
/**
* Copy the current immutable object by setting a value for the {@link FileEntryProperties#getName() name} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for name
* @return A modified copy of the {@code this} object
*/
public final ImmutableFileEntryProperties withName(String value) {
String newValue = Objects.requireNonNull(value, "name");
if (this.name.equals(newValue)) return this;
return new ImmutableFileEntryProperties(this.guid, newValue, this.spaceGuid, this.maxFileSizeInBytes);
}
/**
* Copy the current immutable object by setting a value for the {@link FileEntryProperties#getSpaceGuid() spaceGuid} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for spaceGuid
* @return A modified copy of the {@code this} object
*/
public final ImmutableFileEntryProperties withSpaceGuid(String value) {
String newValue = Objects.requireNonNull(value, "spaceGuid");
if (this.spaceGuid.equals(newValue)) return this;
return new ImmutableFileEntryProperties(this.guid, this.name, newValue, this.maxFileSizeInBytes);
}
/**
* Copy the current immutable object by setting a value for the {@link FileEntryProperties#getMaxFileSizeInBytes() maxFileSizeInBytes} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for maxFileSizeInBytes
* @return A modified copy of the {@code this} object
*/
public final ImmutableFileEntryProperties withMaxFileSizeInBytes(long value) {
if (this.maxFileSizeInBytes == value) return this;
return new ImmutableFileEntryProperties(this.guid, this.name, this.spaceGuid, value);
}
/**
* This instance is equal to all instances of {@code ImmutableFileEntryProperties} that have equal attribute values.
* @return {@code true} if {@code this} is equal to {@code another} instance
*/
@Override
public boolean equals(Object another) {
if (this == another) return true;
return another instanceof ImmutableFileEntryProperties
&& equalTo(0, (ImmutableFileEntryProperties) another);
}
private boolean equalTo(int synthetic, ImmutableFileEntryProperties another) {
return guid.equals(another.guid)
&& name.equals(another.name)
&& spaceGuid.equals(another.spaceGuid)
&& maxFileSizeInBytes == another.maxFileSizeInBytes;
}
/**
* Computes a hash code from attributes: {@code guid}, {@code name}, {@code spaceGuid}, {@code maxFileSizeInBytes}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + guid.hashCode();
h += (h << 5) + name.hashCode();
h += (h << 5) + spaceGuid.hashCode();
h += (h << 5) + Long.hashCode(maxFileSizeInBytes);
return h;
}
/**
* Prints the immutable value {@code FileEntryProperties} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "FileEntryProperties{"
+ "guid=" + guid
+ ", name=" + name
+ ", spaceGuid=" + spaceGuid
+ ", maxFileSizeInBytes=" + maxFileSizeInBytes
+ "}";
}
/**
* Creates an immutable copy of a {@link FileEntryProperties} 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 FileEntryProperties instance
*/
public static ImmutableFileEntryProperties copyOf(FileEntryProperties instance) {
if (instance instanceof ImmutableFileEntryProperties) {
return (ImmutableFileEntryProperties) instance;
}
return ImmutableFileEntryProperties.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableFileEntryProperties ImmutableFileEntryProperties}.
*
* ImmutableFileEntryProperties.builder()
* .guid(String) // required {@link FileEntryProperties#getGuid() guid}
* .name(String) // required {@link FileEntryProperties#getName() name}
* .spaceGuid(String) // required {@link FileEntryProperties#getSpaceGuid() spaceGuid}
* .maxFileSizeInBytes(long) // required {@link FileEntryProperties#getMaxFileSizeInBytes() maxFileSizeInBytes}
* .build();
*
* @return A new ImmutableFileEntryProperties builder
*/
public static ImmutableFileEntryProperties.Builder builder() {
return new ImmutableFileEntryProperties.Builder();
}
/**
* Builds instances of type {@link ImmutableFileEntryProperties ImmutableFileEntryProperties}.
* 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.
*/
public static final class Builder {
private static final long INIT_BIT_GUID = 0x1L;
private static final long INIT_BIT_NAME = 0x2L;
private static final long INIT_BIT_SPACE_GUID = 0x4L;
private static final long INIT_BIT_MAX_FILE_SIZE_IN_BYTES = 0x8L;
private long initBits = 0xfL;
private String guid;
private String name;
private String spaceGuid;
private long maxFileSizeInBytes;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code FileEntryProperties} 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
*/
public final Builder from(FileEntryProperties instance) {
Objects.requireNonNull(instance, "instance");
this.guid(instance.getGuid());
this.name(instance.getName());
this.spaceGuid(instance.getSpaceGuid());
this.maxFileSizeInBytes(instance.getMaxFileSizeInBytes());
return this;
}
/**
* Initializes the value for the {@link FileEntryProperties#getGuid() guid} attribute.
* @param guid The value for guid
* @return {@code this} builder for use in a chained invocation
*/
public final Builder guid(String guid) {
this.guid = Objects.requireNonNull(guid, "guid");
initBits &= ~INIT_BIT_GUID;
return this;
}
/**
* Initializes the value for the {@link FileEntryProperties#getName() name} attribute.
* @param name The value for name
* @return {@code this} builder for use in a chained invocation
*/
public final Builder name(String name) {
this.name = Objects.requireNonNull(name, "name");
initBits &= ~INIT_BIT_NAME;
return this;
}
/**
* Initializes the value for the {@link FileEntryProperties#getSpaceGuid() spaceGuid} attribute.
* @param spaceGuid The value for spaceGuid
* @return {@code this} builder for use in a chained invocation
*/
public final Builder spaceGuid(String spaceGuid) {
this.spaceGuid = Objects.requireNonNull(spaceGuid, "spaceGuid");
initBits &= ~INIT_BIT_SPACE_GUID;
return this;
}
/**
* Initializes the value for the {@link FileEntryProperties#getMaxFileSizeInBytes() maxFileSizeInBytes} attribute.
* @param maxFileSizeInBytes The value for maxFileSizeInBytes
* @return {@code this} builder for use in a chained invocation
*/
public final Builder maxFileSizeInBytes(long maxFileSizeInBytes) {
this.maxFileSizeInBytes = maxFileSizeInBytes;
initBits &= ~INIT_BIT_MAX_FILE_SIZE_IN_BYTES;
return this;
}
/**
* Builds a new {@link ImmutableFileEntryProperties ImmutableFileEntryProperties}.
* @return An immutable instance of FileEntryProperties
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableFileEntryProperties build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableFileEntryProperties(guid, name, spaceGuid, maxFileSizeInBytes);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_GUID) != 0) attributes.add("guid");
if ((initBits & INIT_BIT_NAME) != 0) attributes.add("name");
if ((initBits & INIT_BIT_SPACE_GUID) != 0) attributes.add("spaceGuid");
if ((initBits & INIT_BIT_MAX_FILE_SIZE_IN_BYTES) != 0) attributes.add("maxFileSizeInBytes");
return "Cannot build FileEntryProperties, some of required attributes are not set " + attributes;
}
}
}