org.cloudfoundry.multiapps.controller.api.model.ImmutableMta Maven / Gradle / Ivy
package org.cloudfoundry.multiapps.controller.api.model;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import org.cloudfoundry.multiapps.common.Nullable;
/**
* Immutable implementation of {@link Mta}.
*
* Use the builder to create immutable instances:
* {@code ImmutableMta.builder()}.
*/
@SuppressWarnings({"all"})
public final class ImmutableMta implements Mta {
private final @Nullable Metadata metadata;
private final List modules;
private final Set services;
private ImmutableMta(
@Nullable Metadata metadata,
List modules,
Set services) {
this.metadata = metadata;
this.modules = modules;
this.services = services;
}
/**
* @return The value of the {@code metadata} attribute
*/
@JsonProperty("metadata")
@Override
public @Nullable Metadata getMetadata() {
return metadata;
}
/**
* @return The value of the {@code modules} attribute
*/
@JsonProperty("modules")
@Override
public List getModules() {
return modules;
}
/**
* @return The value of the {@code services} attribute
*/
@JsonProperty("services")
@Override
public Set getServices() {
return services;
}
/**
* Copy the current immutable object by setting a value for the {@link Mta#getMetadata() metadata} attribute.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for metadata (can be {@code null})
* @return A modified copy of the {@code this} object
*/
public final ImmutableMta withMetadata(@Nullable Metadata value) {
if (this.metadata == value) return this;
return new ImmutableMta(value, this.modules, this.services);
}
/**
* Copy the current immutable object with elements that replace the content of {@link Mta#getModules() modules}.
* @param elements The elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableMta withModules(Module... elements) {
List newValue = createUnmodifiableList(false, createSafeList(Arrays.asList(elements), true, false));
return new ImmutableMta(this.metadata, newValue, this.services);
}
/**
* Copy the current immutable object with elements that replace the content of {@link Mta#getModules() modules}.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param elements An iterable of modules elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableMta withModules(Iterable extends Module> elements) {
if (this.modules == elements) return this;
List newValue = createUnmodifiableList(false, createSafeList(elements, true, false));
return new ImmutableMta(this.metadata, newValue, this.services);
}
/**
* Copy the current immutable object with elements that replace the content of {@link Mta#getServices() services}.
* @param elements The elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableMta withServices(String... elements) {
Set newValue = createUnmodifiableSet(createSafeList(Arrays.asList(elements), true, false));
return new ImmutableMta(this.metadata, this.modules, newValue);
}
/**
* Copy the current immutable object with elements that replace the content of {@link Mta#getServices() services}.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param elements An iterable of services elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableMta withServices(Iterable elements) {
if (this.services == elements) return this;
Set newValue = createUnmodifiableSet(createSafeList(elements, true, false));
return new ImmutableMta(this.metadata, this.modules, newValue);
}
/**
* This instance is equal to all instances of {@code ImmutableMta} 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 ImmutableMta
&& equalTo(0, (ImmutableMta) another);
}
private boolean equalTo(int synthetic, ImmutableMta another) {
return Objects.equals(metadata, another.metadata)
&& modules.equals(another.modules)
&& services.equals(another.services);
}
/**
* Computes a hash code from attributes: {@code metadata}, {@code modules}, {@code services}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + Objects.hashCode(metadata);
h += (h << 5) + modules.hashCode();
h += (h << 5) + services.hashCode();
return h;
}
/**
* Prints the immutable value {@code Mta} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "Mta{"
+ "metadata=" + metadata
+ ", modules=" + modules
+ ", services=" + services
+ "}";
}
/**
* Utility type used to correctly read immutable object from JSON representation.
* @deprecated Do not use this type directly, it exists only for the Jackson-binding infrastructure
*/
@Deprecated
@JsonDeserialize
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE)
static final class Json implements Mta {
Metadata metadata;
List modules = Collections.emptyList();
Set services = Collections.emptySet();
@JsonProperty("metadata")
public void setMetadata(@Nullable Metadata metadata) {
this.metadata = metadata;
}
@JsonProperty("modules")
public void setModules(List modules) {
this.modules = modules;
}
@JsonProperty("services")
public void setServices(Set services) {
this.services = services;
}
@Override
public Metadata getMetadata() { throw new UnsupportedOperationException(); }
@Override
public List getModules() { throw new UnsupportedOperationException(); }
@Override
public Set getServices() { throw new UnsupportedOperationException(); }
}
/**
* @param json A JSON-bindable data structure
* @return An immutable value type
* @deprecated Do not use this method directly, it exists only for the Jackson-binding infrastructure
*/
@Deprecated
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
static ImmutableMta fromJson(Json json) {
ImmutableMta.Builder builder = ImmutableMta.builder();
if (json.metadata != null) {
builder.metadata(json.metadata);
}
if (json.modules != null) {
builder.addAllModules(json.modules);
}
if (json.services != null) {
builder.addAllServices(json.services);
}
return builder.build();
}
/**
* Creates an immutable copy of a {@link Mta} 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 Mta instance
*/
public static ImmutableMta copyOf(Mta instance) {
if (instance instanceof ImmutableMta) {
return (ImmutableMta) instance;
}
return ImmutableMta.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableMta ImmutableMta}.
*
* ImmutableMta.builder()
* .metadata(org.cloudfoundry.multiapps.controller.api.model.Metadata | null) // nullable {@link Mta#getMetadata() metadata}
* .addModule|addAllModules(org.cloudfoundry.multiapps.controller.api.model.Module) // {@link Mta#getModules() modules} elements
* .addService|addAllServices(String) // {@link Mta#getServices() services} elements
* .build();
*
* @return A new ImmutableMta builder
*/
public static ImmutableMta.Builder builder() {
return new ImmutableMta.Builder();
}
/**
* Builds instances of type {@link ImmutableMta ImmutableMta}.
* 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 Metadata metadata;
private List modules = new ArrayList();
private List services = new ArrayList();
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code Mta} 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
*/
public final Builder from(Mta instance) {
Objects.requireNonNull(instance, "instance");
@Nullable Metadata metadataValue = instance.getMetadata();
if (metadataValue != null) {
metadata(metadataValue);
}
addAllModules(instance.getModules());
addAllServices(instance.getServices());
return this;
}
/**
* Initializes the value for the {@link Mta#getMetadata() metadata} attribute.
* @param metadata The value for metadata (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("metadata")
public final Builder metadata(@Nullable Metadata metadata) {
this.metadata = metadata;
return this;
}
/**
* Adds one element to {@link Mta#getModules() modules} list.
* @param element A modules element
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addModule(Module element) {
this.modules.add(Objects.requireNonNull(element, "modules element"));
return this;
}
/**
* Adds elements to {@link Mta#getModules() modules} list.
* @param elements An array of modules elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addModules(Module... elements) {
for (Module element : elements) {
this.modules.add(Objects.requireNonNull(element, "modules element"));
}
return this;
}
/**
* Sets or replaces all elements for {@link Mta#getModules() modules} list.
* @param elements An iterable of modules elements
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("modules")
public final Builder modules(Iterable extends Module> elements) {
this.modules.clear();
return addAllModules(elements);
}
/**
* Adds elements to {@link Mta#getModules() modules} list.
* @param elements An iterable of modules elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addAllModules(Iterable extends Module> elements) {
for (Module element : elements) {
this.modules.add(Objects.requireNonNull(element, "modules element"));
}
return this;
}
/**
* Adds one element to {@link Mta#getServices() services} set.
* @param element A services element
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addService(String element) {
this.services.add(Objects.requireNonNull(element, "services element"));
return this;
}
/**
* Adds elements to {@link Mta#getServices() services} set.
* @param elements An array of services elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addServices(String... elements) {
for (String element : elements) {
this.services.add(Objects.requireNonNull(element, "services element"));
}
return this;
}
/**
* Sets or replaces all elements for {@link Mta#getServices() services} set.
* @param elements An iterable of services elements
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("services")
public final Builder services(Iterable elements) {
this.services.clear();
return addAllServices(elements);
}
/**
* Adds elements to {@link Mta#getServices() services} set.
* @param elements An iterable of services elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addAllServices(Iterable elements) {
for (String element : elements) {
this.services.add(Objects.requireNonNull(element, "services element"));
}
return this;
}
/**
* Builds a new {@link ImmutableMta ImmutableMta}.
* @return An immutable instance of Mta
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableMta build() {
return new ImmutableMta(metadata, createUnmodifiableList(true, modules), createUnmodifiableSet(services));
}
}
private static List createSafeList(Iterable extends T> iterable, boolean checkNulls, boolean skipNulls) {
ArrayList list;
if (iterable instanceof Collection>) {
int size = ((Collection>) iterable).size();
if (size == 0) return Collections.emptyList();
list = new ArrayList<>(size);
} else {
list = new ArrayList<>();
}
for (T element : iterable) {
if (skipNulls && element == null) continue;
if (checkNulls) Objects.requireNonNull(element, "element");
list.add(element);
}
return list;
}
private static List createUnmodifiableList(boolean clone, List list) {
switch(list.size()) {
case 0: return Collections.emptyList();
case 1: return Collections.singletonList(list.get(0));
default:
if (clone) {
return Collections.unmodifiableList(new ArrayList<>(list));
} else {
if (list instanceof ArrayList>) {
((ArrayList>) list).trimToSize();
}
return Collections.unmodifiableList(list);
}
}
}
/** Unmodifiable set constructed from list to avoid rehashing. */
private static Set createUnmodifiableSet(List list) {
switch(list.size()) {
case 0: return Collections.emptySet();
case 1: return Collections.singleton(list.get(0));
default:
Set set = new LinkedHashSet<>(list.size() * 4 / 3 + 1);
set.addAll(list);
return Collections.unmodifiableSet(set);
}
}
}