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