org.cloudfoundry.multiapps.controller.api.model.ImmutableModule 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.Date;
import java.util.List;
import java.util.Objects;
import org.cloudfoundry.multiapps.common.Nullable;
/**
* Immutable implementation of {@link Module}.
*
* Use the builder to create immutable instances:
* {@code ImmutableModule.builder()}.
*/
@SuppressWarnings({"all"})
public final class ImmutableModule implements Module {
private final @Nullable String moduleName;
private final @Nullable String appName;
private final @Nullable Date createdOn;
private final @Nullable Date updatedOn;
private final List providedDendencyNames;
private final List services;
private final List uris;
private ImmutableModule(
@Nullable String moduleName,
@Nullable String appName,
@Nullable Date createdOn,
@Nullable Date updatedOn,
List providedDendencyNames,
List services,
List uris) {
this.moduleName = moduleName;
this.appName = appName;
this.createdOn = createdOn;
this.updatedOn = updatedOn;
this.providedDendencyNames = providedDendencyNames;
this.services = services;
this.uris = uris;
}
/**
* @return The value of the {@code moduleName} attribute
*/
@JsonProperty("moduleName")
@Override
public @Nullable String getModuleName() {
return moduleName;
}
/**
* @return The value of the {@code appName} attribute
*/
@JsonProperty("appName")
@Override
public @Nullable String getAppName() {
return appName;
}
/**
* @return The value of the {@code createdOn} attribute
*/
@JsonProperty("createdOn")
@Override
public @Nullable Date getCreatedOn() {
return createdOn;
}
/**
* @return The value of the {@code updatedOn} attribute
*/
@JsonProperty("updatedOn")
@Override
public @Nullable Date getUpdatedOn() {
return updatedOn;
}
/**
* @return The value of the {@code providedDendencyNames} attribute
*/
@JsonProperty("providedDendencyNames")
@Override
public List getProvidedDendencyNames() {
return providedDendencyNames;
}
/**
* @return The value of the {@code services} attribute
*/
@JsonProperty("services")
@Override
public List getServices() {
return services;
}
/**
* @return The value of the {@code uris} attribute
*/
@JsonProperty("uris")
@Override
public List getUris() {
return uris;
}
/**
* Copy the current immutable object by setting a value for the {@link Module#getModuleName() moduleName} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for moduleName (can be {@code null})
* @return A modified copy of the {@code this} object
*/
public final ImmutableModule withModuleName(@Nullable String value) {
if (Objects.equals(this.moduleName, value)) return this;
return new ImmutableModule(
value,
this.appName,
this.createdOn,
this.updatedOn,
this.providedDendencyNames,
this.services,
this.uris);
}
/**
* Copy the current immutable object by setting a value for the {@link Module#getAppName() appName} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for appName (can be {@code null})
* @return A modified copy of the {@code this} object
*/
public final ImmutableModule withAppName(@Nullable String value) {
if (Objects.equals(this.appName, value)) return this;
return new ImmutableModule(
this.moduleName,
value,
this.createdOn,
this.updatedOn,
this.providedDendencyNames,
this.services,
this.uris);
}
/**
* Copy the current immutable object by setting a value for the {@link Module#getCreatedOn() createdOn} 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 createdOn (can be {@code null})
* @return A modified copy of the {@code this} object
*/
public final ImmutableModule withCreatedOn(@Nullable Date value) {
if (this.createdOn == value) return this;
return new ImmutableModule(
this.moduleName,
this.appName,
value,
this.updatedOn,
this.providedDendencyNames,
this.services,
this.uris);
}
/**
* Copy the current immutable object by setting a value for the {@link Module#getUpdatedOn() updatedOn} 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 updatedOn (can be {@code null})
* @return A modified copy of the {@code this} object
*/
public final ImmutableModule withUpdatedOn(@Nullable Date value) {
if (this.updatedOn == value) return this;
return new ImmutableModule(
this.moduleName,
this.appName,
this.createdOn,
value,
this.providedDendencyNames,
this.services,
this.uris);
}
/**
* Copy the current immutable object with elements that replace the content of {@link Module#getProvidedDendencyNames() providedDendencyNames}.
* @param elements The elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableModule withProvidedDendencyNames(String... elements) {
List newValue = createUnmodifiableList(false, createSafeList(Arrays.asList(elements), true, false));
return new ImmutableModule(
this.moduleName,
this.appName,
this.createdOn,
this.updatedOn,
newValue,
this.services,
this.uris);
}
/**
* Copy the current immutable object with elements that replace the content of {@link Module#getProvidedDendencyNames() providedDendencyNames}.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param elements An iterable of providedDendencyNames elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableModule withProvidedDendencyNames(Iterable elements) {
if (this.providedDendencyNames == elements) return this;
List newValue = createUnmodifiableList(false, createSafeList(elements, true, false));
return new ImmutableModule(
this.moduleName,
this.appName,
this.createdOn,
this.updatedOn,
newValue,
this.services,
this.uris);
}
/**
* Copy the current immutable object with elements that replace the content of {@link Module#getServices() services}.
* @param elements The elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableModule withServices(String... elements) {
List newValue = createUnmodifiableList(false, createSafeList(Arrays.asList(elements), true, false));
return new ImmutableModule(
this.moduleName,
this.appName,
this.createdOn,
this.updatedOn,
this.providedDendencyNames,
newValue,
this.uris);
}
/**
* Copy the current immutable object with elements that replace the content of {@link Module#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 ImmutableModule withServices(Iterable elements) {
if (this.services == elements) return this;
List newValue = createUnmodifiableList(false, createSafeList(elements, true, false));
return new ImmutableModule(
this.moduleName,
this.appName,
this.createdOn,
this.updatedOn,
this.providedDendencyNames,
newValue,
this.uris);
}
/**
* Copy the current immutable object with elements that replace the content of {@link Module#getUris() uris}.
* @param elements The elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableModule withUris(String... elements) {
List newValue = createUnmodifiableList(false, createSafeList(Arrays.asList(elements), true, false));
return new ImmutableModule(
this.moduleName,
this.appName,
this.createdOn,
this.updatedOn,
this.providedDendencyNames,
this.services,
newValue);
}
/**
* Copy the current immutable object with elements that replace the content of {@link Module#getUris() uris}.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param elements An iterable of uris elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableModule withUris(Iterable elements) {
if (this.uris == elements) return this;
List newValue = createUnmodifiableList(false, createSafeList(elements, true, false));
return new ImmutableModule(
this.moduleName,
this.appName,
this.createdOn,
this.updatedOn,
this.providedDendencyNames,
this.services,
newValue);
}
/**
* This instance is equal to all instances of {@code ImmutableModule} 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 ImmutableModule
&& equalTo(0, (ImmutableModule) another);
}
private boolean equalTo(int synthetic, ImmutableModule another) {
return Objects.equals(moduleName, another.moduleName)
&& Objects.equals(appName, another.appName)
&& Objects.equals(createdOn, another.createdOn)
&& Objects.equals(updatedOn, another.updatedOn)
&& providedDendencyNames.equals(another.providedDendencyNames)
&& services.equals(another.services)
&& uris.equals(another.uris);
}
/**
* Computes a hash code from attributes: {@code moduleName}, {@code appName}, {@code createdOn}, {@code updatedOn}, {@code providedDendencyNames}, {@code services}, {@code uris}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + Objects.hashCode(moduleName);
h += (h << 5) + Objects.hashCode(appName);
h += (h << 5) + Objects.hashCode(createdOn);
h += (h << 5) + Objects.hashCode(updatedOn);
h += (h << 5) + providedDendencyNames.hashCode();
h += (h << 5) + services.hashCode();
h += (h << 5) + uris.hashCode();
return h;
}
/**
* Prints the immutable value {@code Module} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "Module{"
+ "moduleName=" + moduleName
+ ", appName=" + appName
+ ", createdOn=" + createdOn
+ ", updatedOn=" + updatedOn
+ ", providedDendencyNames=" + providedDendencyNames
+ ", services=" + services
+ ", uris=" + uris
+ "}";
}
/**
* 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 Module {
String moduleName;
String appName;
Date createdOn;
Date updatedOn;
List providedDendencyNames = Collections.emptyList();
List services = Collections.emptyList();
List uris = Collections.emptyList();
@JsonProperty("moduleName")
public void setModuleName(@Nullable String moduleName) {
this.moduleName = moduleName;
}
@JsonProperty("appName")
public void setAppName(@Nullable String appName) {
this.appName = appName;
}
@JsonProperty("createdOn")
public void setCreatedOn(@Nullable Date createdOn) {
this.createdOn = createdOn;
}
@JsonProperty("updatedOn")
public void setUpdatedOn(@Nullable Date updatedOn) {
this.updatedOn = updatedOn;
}
@JsonProperty("providedDendencyNames")
public void setProvidedDendencyNames(List providedDendencyNames) {
this.providedDendencyNames = providedDendencyNames;
}
@JsonProperty("services")
public void setServices(List services) {
this.services = services;
}
@JsonProperty("uris")
public void setUris(List uris) {
this.uris = uris;
}
@Override
public String getModuleName() { throw new UnsupportedOperationException(); }
@Override
public String getAppName() { throw new UnsupportedOperationException(); }
@Override
public Date getCreatedOn() { throw new UnsupportedOperationException(); }
@Override
public Date getUpdatedOn() { throw new UnsupportedOperationException(); }
@Override
public List getProvidedDendencyNames() { throw new UnsupportedOperationException(); }
@Override
public List getServices() { throw new UnsupportedOperationException(); }
@Override
public List getUris() { 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 ImmutableModule fromJson(Json json) {
ImmutableModule.Builder builder = ImmutableModule.builder();
if (json.moduleName != null) {
builder.moduleName(json.moduleName);
}
if (json.appName != null) {
builder.appName(json.appName);
}
if (json.createdOn != null) {
builder.createdOn(json.createdOn);
}
if (json.updatedOn != null) {
builder.updatedOn(json.updatedOn);
}
if (json.providedDendencyNames != null) {
builder.addAllProvidedDendencyNames(json.providedDendencyNames);
}
if (json.services != null) {
builder.addAllServices(json.services);
}
if (json.uris != null) {
builder.addAllUris(json.uris);
}
return builder.build();
}
/**
* Creates an immutable copy of a {@link Module} 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 Module instance
*/
public static ImmutableModule copyOf(Module instance) {
if (instance instanceof ImmutableModule) {
return (ImmutableModule) instance;
}
return ImmutableModule.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableModule ImmutableModule}.
*
* ImmutableModule.builder()
* .moduleName(String | null) // nullable {@link Module#getModuleName() moduleName}
* .appName(String | null) // nullable {@link Module#getAppName() appName}
* .createdOn(Date | null) // nullable {@link Module#getCreatedOn() createdOn}
* .updatedOn(Date | null) // nullable {@link Module#getUpdatedOn() updatedOn}
* .addProvidedDendencyName|addAllProvidedDendencyNames(String) // {@link Module#getProvidedDendencyNames() providedDendencyNames} elements
* .addService|addAllServices(String) // {@link Module#getServices() services} elements
* .addUri|addAllUris(String) // {@link Module#getUris() uris} elements
* .build();
*
* @return A new ImmutableModule builder
*/
public static ImmutableModule.Builder builder() {
return new ImmutableModule.Builder();
}
/**
* Builds instances of type {@link ImmutableModule ImmutableModule}.
* 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 String moduleName;
private String appName;
private Date createdOn;
private Date updatedOn;
private List providedDendencyNames = new ArrayList();
private List services = new ArrayList();
private List uris = new ArrayList();
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code Module} 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(Module instance) {
Objects.requireNonNull(instance, "instance");
@Nullable String moduleNameValue = instance.getModuleName();
if (moduleNameValue != null) {
moduleName(moduleNameValue);
}
@Nullable String appNameValue = instance.getAppName();
if (appNameValue != null) {
appName(appNameValue);
}
@Nullable Date createdOnValue = instance.getCreatedOn();
if (createdOnValue != null) {
createdOn(createdOnValue);
}
@Nullable Date updatedOnValue = instance.getUpdatedOn();
if (updatedOnValue != null) {
updatedOn(updatedOnValue);
}
addAllProvidedDendencyNames(instance.getProvidedDendencyNames());
addAllServices(instance.getServices());
addAllUris(instance.getUris());
return this;
}
/**
* Initializes the value for the {@link Module#getModuleName() moduleName} attribute.
* @param moduleName The value for moduleName (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("moduleName")
public final Builder moduleName(@Nullable String moduleName) {
this.moduleName = moduleName;
return this;
}
/**
* Initializes the value for the {@link Module#getAppName() appName} attribute.
* @param appName The value for appName (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("appName")
public final Builder appName(@Nullable String appName) {
this.appName = appName;
return this;
}
/**
* Initializes the value for the {@link Module#getCreatedOn() createdOn} attribute.
* @param createdOn The value for createdOn (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("createdOn")
public final Builder createdOn(@Nullable Date createdOn) {
this.createdOn = createdOn;
return this;
}
/**
* Initializes the value for the {@link Module#getUpdatedOn() updatedOn} attribute.
* @param updatedOn The value for updatedOn (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("updatedOn")
public final Builder updatedOn(@Nullable Date updatedOn) {
this.updatedOn = updatedOn;
return this;
}
/**
* Adds one element to {@link Module#getProvidedDendencyNames() providedDendencyNames} list.
* @param element A providedDendencyNames element
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addProvidedDendencyName(String element) {
this.providedDendencyNames.add(Objects.requireNonNull(element, "providedDendencyNames element"));
return this;
}
/**
* Adds elements to {@link Module#getProvidedDendencyNames() providedDendencyNames} list.
* @param elements An array of providedDendencyNames elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addProvidedDendencyNames(String... elements) {
for (String element : elements) {
this.providedDendencyNames.add(Objects.requireNonNull(element, "providedDendencyNames element"));
}
return this;
}
/**
* Sets or replaces all elements for {@link Module#getProvidedDendencyNames() providedDendencyNames} list.
* @param elements An iterable of providedDendencyNames elements
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("providedDendencyNames")
public final Builder providedDendencyNames(Iterable elements) {
this.providedDendencyNames.clear();
return addAllProvidedDendencyNames(elements);
}
/**
* Adds elements to {@link Module#getProvidedDendencyNames() providedDendencyNames} list.
* @param elements An iterable of providedDendencyNames elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addAllProvidedDendencyNames(Iterable elements) {
for (String element : elements) {
this.providedDendencyNames.add(Objects.requireNonNull(element, "providedDendencyNames element"));
}
return this;
}
/**
* Adds one element to {@link Module#getServices() services} list.
* @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 Module#getServices() services} list.
* @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 Module#getServices() services} list.
* @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 Module#getServices() services} list.
* @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;
}
/**
* Adds one element to {@link Module#getUris() uris} list.
* @param element A uris element
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addUri(String element) {
this.uris.add(Objects.requireNonNull(element, "uris element"));
return this;
}
/**
* Adds elements to {@link Module#getUris() uris} list.
* @param elements An array of uris elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addUris(String... elements) {
for (String element : elements) {
this.uris.add(Objects.requireNonNull(element, "uris element"));
}
return this;
}
/**
* Sets or replaces all elements for {@link Module#getUris() uris} list.
* @param elements An iterable of uris elements
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("uris")
public final Builder uris(Iterable elements) {
this.uris.clear();
return addAllUris(elements);
}
/**
* Adds elements to {@link Module#getUris() uris} list.
* @param elements An iterable of uris elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addAllUris(Iterable elements) {
for (String element : elements) {
this.uris.add(Objects.requireNonNull(element, "uris element"));
}
return this;
}
/**
* Builds a new {@link ImmutableModule ImmutableModule}.
* @return An immutable instance of Module
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableModule build() {
return new ImmutableModule(
moduleName,
appName,
createdOn,
updatedOn,
createUnmodifiableList(true, providedDendencyNames),
createUnmodifiableList(true, services),
createUnmodifiableList(true, uris));
}
}
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);
}
}
}
}