org.cloudfoundry.client.lib.adapters.ImmutableRawCloudRoute Maven / Gradle / Ivy
Show all versions of cloudfoundry-client-lib Show documentation
package org.cloudfoundry.client.lib.adapters;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import org.cloudfoundry.client.lib.domain.CloudDomain;
import org.cloudfoundry.client.lib.domain.Derivable;
import org.cloudfoundry.client.v2.Resource;
import org.cloudfoundry.client.v2.routemappings.RouteMappingEntity;
import org.cloudfoundry.client.v2.routes.RouteEntity;
import org.immutables.value.Generated;
/**
* Immutable implementation of {@link RawCloudRoute}.
*
* Use the builder to create immutable instances:
* {@code ImmutableRawCloudRoute.builder()}.
*/
@Generated(from = "RawCloudRoute", generator = "Immutables")
@SuppressWarnings({"all"})
@javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor")
public final class ImmutableRawCloudRoute extends RawCloudRoute {
private final Resource resource;
private final List> routeMappingResources;
private final Derivable domain;
private ImmutableRawCloudRoute(
Resource resource,
List> routeMappingResources,
Derivable domain) {
this.resource = resource;
this.routeMappingResources = routeMappingResources;
this.domain = domain;
}
/**
* @return The value of the {@code resource} attribute
*/
@Override
public Resource getResource() {
return resource;
}
/**
* @return The value of the {@code routeMappingResources} attribute
*/
@Override
public List> getRouteMappingResources() {
return routeMappingResources;
}
/**
* @return The value of the {@code domain} attribute
*/
@Override
public Derivable getDomain() {
return domain;
}
/**
* Copy the current immutable object by setting a value for the {@link RawCloudRoute#getResource() resource} 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 resource
* @return A modified copy of the {@code this} object
*/
public final ImmutableRawCloudRoute withResource(Resource value) {
if (this.resource == value) return this;
Resource newValue = Objects.requireNonNull(value, "resource");
return new ImmutableRawCloudRoute(newValue, this.routeMappingResources, this.domain);
}
/**
* Copy the current immutable object with elements that replace the content of {@link RawCloudRoute#getRouteMappingResources() routeMappingResources}.
* @param elements The elements to set
* @return A modified copy of {@code this} object
*/
@SafeVarargs @SuppressWarnings("varargs")
public final ImmutableRawCloudRoute withRouteMappingResources(Resource... elements) {
List> newValue = createUnmodifiableList(false, createSafeList(Arrays.asList(elements), true, false));
return new ImmutableRawCloudRoute(this.resource, newValue, this.domain);
}
/**
* Copy the current immutable object with elements that replace the content of {@link RawCloudRoute#getRouteMappingResources() routeMappingResources}.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param elements An iterable of routeMappingResources elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableRawCloudRoute withRouteMappingResources(Iterable extends Resource> elements) {
if (this.routeMappingResources == elements) return this;
List> newValue = createUnmodifiableList(false, createSafeList(elements, true, false));
return new ImmutableRawCloudRoute(this.resource, newValue, this.domain);
}
/**
* Copy the current immutable object by setting a value for the {@link RawCloudRoute#getDomain() domain} 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 domain
* @return A modified copy of the {@code this} object
*/
public final ImmutableRawCloudRoute withDomain(Derivable value) {
if (this.domain == value) return this;
Derivable newValue = Objects.requireNonNull(value, "domain");
return new ImmutableRawCloudRoute(this.resource, this.routeMappingResources, newValue);
}
/**
* This instance is equal to all instances of {@code ImmutableRawCloudRoute} 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 ImmutableRawCloudRoute
&& equalTo((ImmutableRawCloudRoute) another);
}
private boolean equalTo(ImmutableRawCloudRoute another) {
return resource.equals(another.resource)
&& routeMappingResources.equals(another.routeMappingResources)
&& domain.equals(another.domain);
}
/**
* Computes a hash code from attributes: {@code resource}, {@code routeMappingResources}, {@code domain}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + resource.hashCode();
h += (h << 5) + routeMappingResources.hashCode();
h += (h << 5) + domain.hashCode();
return h;
}
/**
* Prints the immutable value {@code RawCloudRoute} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "RawCloudRoute{"
+ "resource=" + resource
+ ", routeMappingResources=" + routeMappingResources
+ ", domain=" + domain
+ "}";
}
/**
* Creates an immutable copy of a {@link RawCloudRoute} 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 RawCloudRoute instance
*/
public static ImmutableRawCloudRoute copyOf(RawCloudRoute instance) {
if (instance instanceof ImmutableRawCloudRoute) {
return (ImmutableRawCloudRoute) instance;
}
return ImmutableRawCloudRoute.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableRawCloudRoute ImmutableRawCloudRoute}.
*
* ImmutableRawCloudRoute.builder()
* .resource(org.cloudfoundry.client.v2.Resource<org.cloudfoundry.client.v2.routes.RouteEntity>) // required {@link RawCloudRoute#getResource() resource}
* .addRouteMappingResource|addAllRouteMappingResources(org.cloudfoundry.client.v2.Resource<org.cloudfoundry.client.v2.routemappings.RouteMappingEntity>) // {@link RawCloudRoute#getRouteMappingResources() routeMappingResources} elements
* .domain(org.cloudfoundry.client.lib.domain.Derivable<org.cloudfoundry.client.lib.domain.CloudDomain>) // required {@link RawCloudRoute#getDomain() domain}
* .build();
*
* @return A new ImmutableRawCloudRoute builder
*/
public static ImmutableRawCloudRoute.Builder builder() {
return new ImmutableRawCloudRoute.Builder();
}
/**
* Builds instances of type {@link ImmutableRawCloudRoute ImmutableRawCloudRoute}.
* 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 = "RawCloudRoute", generator = "Immutables")
public static final class Builder {
private static final long INIT_BIT_RESOURCE = 0x1L;
private static final long INIT_BIT_DOMAIN = 0x2L;
private long initBits = 0x3L;
private Resource resource;
private List> routeMappingResources = new ArrayList>();
private Derivable domain;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code RawCloudRoute} 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(RawCloudRoute instance) {
Objects.requireNonNull(instance, "instance");
resource(instance.getResource());
addAllRouteMappingResources(instance.getRouteMappingResources());
domain(instance.getDomain());
return this;
}
/**
* Initializes the value for the {@link RawCloudRoute#getResource() resource} attribute.
* @param resource The value for resource
* @return {@code this} builder for use in a chained invocation
*/
public final Builder resource(Resource resource) {
this.resource = Objects.requireNonNull(resource, "resource");
initBits &= ~INIT_BIT_RESOURCE;
return this;
}
/**
* Adds one element to {@link RawCloudRoute#getRouteMappingResources() routeMappingResources} list.
* @param element A routeMappingResources element
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addRouteMappingResource(Resource element) {
this.routeMappingResources.add(Objects.requireNonNull(element, "routeMappingResources element"));
return this;
}
/**
* Adds elements to {@link RawCloudRoute#getRouteMappingResources() routeMappingResources} list.
* @param elements An array of routeMappingResources elements
* @return {@code this} builder for use in a chained invocation
*/
@SafeVarargs @SuppressWarnings("varargs")
public final Builder addRouteMappingResources(Resource... elements) {
for (Resource element : elements) {
this.routeMappingResources.add(Objects.requireNonNull(element, "routeMappingResources element"));
}
return this;
}
/**
* Sets or replaces all elements for {@link RawCloudRoute#getRouteMappingResources() routeMappingResources} list.
* @param elements An iterable of routeMappingResources elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder routeMappingResources(Iterable extends Resource> elements) {
this.routeMappingResources.clear();
return addAllRouteMappingResources(elements);
}
/**
* Adds elements to {@link RawCloudRoute#getRouteMappingResources() routeMappingResources} list.
* @param elements An iterable of routeMappingResources elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addAllRouteMappingResources(Iterable extends Resource> elements) {
for (Resource element : elements) {
this.routeMappingResources.add(Objects.requireNonNull(element, "routeMappingResources element"));
}
return this;
}
/**
* Initializes the value for the {@link RawCloudRoute#getDomain() domain} attribute.
* @param domain The value for domain
* @return {@code this} builder for use in a chained invocation
*/
public final Builder domain(Derivable domain) {
this.domain = Objects.requireNonNull(domain, "domain");
initBits &= ~INIT_BIT_DOMAIN;
return this;
}
/**
* Builds a new {@link ImmutableRawCloudRoute ImmutableRawCloudRoute}.
* @return An immutable instance of RawCloudRoute
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableRawCloudRoute build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableRawCloudRoute(resource, createUnmodifiableList(true, routeMappingResources), domain);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_RESOURCE) != 0) attributes.add("resource");
if ((initBits & INIT_BIT_DOMAIN) != 0) attributes.add("domain");
return "Cannot build RawCloudRoute, some of required attributes are not set " + attributes;
}
}
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<>();
} 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);
}
}
}
}