
com.sap.cloudfoundry.client.facade.adapters.ImmutableRawCloudServiceKey Maven / Gradle / Ivy
Show all versions of cloudfoundry-client-facade Show documentation
package com.sap.cloudfoundry.client.facade.adapters;
import com.sap.cloudfoundry.client.facade.Nullable;
import com.sap.cloudfoundry.client.facade.domain.CloudServiceInstance;
import com.sap.cloudfoundry.client.facade.domain.Derivable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.cloudfoundry.client.v3.servicebindings.ServiceBindingResource;
import org.immutables.value.Generated;
/**
* Immutable implementation of {@link RawCloudServiceKey}.
*
* Use the builder to create immutable instances:
* {@code ImmutableRawCloudServiceKey.builder()}.
*/
@Generated(from = "RawCloudServiceKey", generator = "Immutables")
@SuppressWarnings({"all"})
@javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor")
public final class ImmutableRawCloudServiceKey
extends RawCloudServiceKey {
private final ServiceBindingResource serviceBindingResource;
private final @Nullable Map credentials;
private final Derivable serviceInstance;
private ImmutableRawCloudServiceKey(
ServiceBindingResource serviceBindingResource,
@Nullable Map credentials,
Derivable serviceInstance) {
this.serviceBindingResource = serviceBindingResource;
this.credentials = credentials;
this.serviceInstance = serviceInstance;
}
/**
* @return The value of the {@code serviceBindingResource} attribute
*/
@Override
public ServiceBindingResource getServiceBindingResource() {
return serviceBindingResource;
}
/**
* @return The value of the {@code credentials} attribute
*/
@Override
public @Nullable Map getCredentials() {
return credentials;
}
/**
* @return The value of the {@code serviceInstance} attribute
*/
@Override
public Derivable getServiceInstance() {
return serviceInstance;
}
/**
* Copy the current immutable object by setting a value for the {@link RawCloudServiceKey#getServiceBindingResource() serviceBindingResource} 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 serviceBindingResource
* @return A modified copy of the {@code this} object
*/
public final ImmutableRawCloudServiceKey withServiceBindingResource(ServiceBindingResource value) {
if (this.serviceBindingResource == value) return this;
ServiceBindingResource newValue = Objects.requireNonNull(value, "serviceBindingResource");
return new ImmutableRawCloudServiceKey(newValue, this.credentials, this.serviceInstance);
}
/**
* Copy the current immutable object by replacing the {@link RawCloudServiceKey#getCredentials() credentials} map with the specified map.
* Nulls are not permitted as keys or values.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param entries The entries to be added to the credentials map
* @return A modified copy of {@code this} object
*/
public final ImmutableRawCloudServiceKey withCredentials(@Nullable Map entries) {
if (this.credentials == entries) return this;
@Nullable Map newValue = entries == null ? null : createUnmodifiableMap(false, false, entries);
return new ImmutableRawCloudServiceKey(this.serviceBindingResource, newValue, this.serviceInstance);
}
/**
* Copy the current immutable object by setting a value for the {@link RawCloudServiceKey#getServiceInstance() serviceInstance} 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 serviceInstance
* @return A modified copy of the {@code this} object
*/
public final ImmutableRawCloudServiceKey withServiceInstance(Derivable value) {
if (this.serviceInstance == value) return this;
Derivable newValue = Objects.requireNonNull(value, "serviceInstance");
return new ImmutableRawCloudServiceKey(this.serviceBindingResource, this.credentials, newValue);
}
/**
* This instance is equal to all instances of {@code ImmutableRawCloudServiceKey} 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 ImmutableRawCloudServiceKey
&& equalTo(0, (ImmutableRawCloudServiceKey) another);
}
private boolean equalTo(int synthetic, ImmutableRawCloudServiceKey another) {
return serviceBindingResource.equals(another.serviceBindingResource)
&& Objects.equals(credentials, another.credentials)
&& serviceInstance.equals(another.serviceInstance);
}
/**
* Computes a hash code from attributes: {@code serviceBindingResource}, {@code credentials}, {@code serviceInstance}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + serviceBindingResource.hashCode();
h += (h << 5) + Objects.hashCode(credentials);
h += (h << 5) + serviceInstance.hashCode();
return h;
}
/**
* Prints the immutable value {@code RawCloudServiceKey} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "RawCloudServiceKey{"
+ "serviceBindingResource=" + serviceBindingResource
+ ", credentials=" + credentials
+ ", serviceInstance=" + serviceInstance
+ "}";
}
/**
* Creates an immutable copy of a {@link RawCloudServiceKey} 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 RawCloudServiceKey instance
*/
public static ImmutableRawCloudServiceKey copyOf(RawCloudServiceKey instance) {
if (instance instanceof ImmutableRawCloudServiceKey) {
return (ImmutableRawCloudServiceKey) instance;
}
return ImmutableRawCloudServiceKey.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableRawCloudServiceKey ImmutableRawCloudServiceKey}.
*
* ImmutableRawCloudServiceKey.builder()
* .serviceBindingResource(org.cloudfoundry.client.v3.servicebindings.ServiceBindingResource) // required {@link RawCloudServiceKey#getServiceBindingResource() serviceBindingResource}
* .credentials(Map<String, Object> | null) // nullable {@link RawCloudServiceKey#getCredentials() credentials}
* .serviceInstance(com.sap.cloudfoundry.client.facade.domain.Derivable<com.sap.cloudfoundry.client.facade.domain.CloudServiceInstance>) // required {@link RawCloudServiceKey#getServiceInstance() serviceInstance}
* .build();
*
* @return A new ImmutableRawCloudServiceKey builder
*/
public static ImmutableRawCloudServiceKey.Builder builder() {
return new ImmutableRawCloudServiceKey.Builder();
}
/**
* Builds instances of type {@link ImmutableRawCloudServiceKey ImmutableRawCloudServiceKey}.
* 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 = "RawCloudServiceKey", generator = "Immutables")
public static final class Builder {
private static final long INIT_BIT_SERVICE_BINDING_RESOURCE = 0x1L;
private static final long INIT_BIT_SERVICE_INSTANCE = 0x2L;
private long initBits = 0x3L;
private ServiceBindingResource serviceBindingResource;
private Map credentials = null;
private Derivable serviceInstance;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code RawCloudServiceKey} 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(RawCloudServiceKey instance) {
Objects.requireNonNull(instance, "instance");
this.serviceBindingResource(instance.getServiceBindingResource());
@Nullable Map credentialsValue = instance.getCredentials();
if (credentialsValue != null) {
putAllCredentials(credentialsValue);
}
this.serviceInstance(instance.getServiceInstance());
return this;
}
/**
* Initializes the value for the {@link RawCloudServiceKey#getServiceBindingResource() serviceBindingResource} attribute.
* @param serviceBindingResource The value for serviceBindingResource
* @return {@code this} builder for use in a chained invocation
*/
public final Builder serviceBindingResource(ServiceBindingResource serviceBindingResource) {
this.serviceBindingResource = Objects.requireNonNull(serviceBindingResource, "serviceBindingResource");
initBits &= ~INIT_BIT_SERVICE_BINDING_RESOURCE;
return this;
}
/**
* Put one entry to the {@link RawCloudServiceKey#getCredentials() credentials} map.
* @param key The key in the credentials map
* @param value The associated value in the credentials map
* @return {@code this} builder for use in a chained invocation
*/
public final Builder putCredential(String key, Object value) {
if (this.credentials == null) {
this.credentials = new LinkedHashMap();
}
this.credentials.put(key, value);
return this;
}
/**
* Put one entry to the {@link RawCloudServiceKey#getCredentials() credentials} map. Nulls are not permitted
* @param entry The key and value entry
* @return {@code this} builder for use in a chained invocation
*/
public final Builder putCredential(Map.Entry entry) {
if (this.credentials == null) {
this.credentials = new LinkedHashMap();
}
String k = entry.getKey();
Object v = entry.getValue();
this.credentials.put(k, v);
return this;
}
/**
* Sets or replaces all mappings from the specified map as entries for the {@link RawCloudServiceKey#getCredentials() credentials} map. Nulls are not permitted as keys or values, but parameter itself can be null
* @param entries The entries that will be added to the credentials map
* @return {@code this} builder for use in a chained invocation
*/
public final Builder credentials(@Nullable Map entries) {
if (entries == null) {
this.credentials = null;
return this;
}
this.credentials = new LinkedHashMap();
return putAllCredentials(entries);
}
/**
* Put all mappings from the specified map as entries to {@link RawCloudServiceKey#getCredentials() credentials} map. Nulls are not permitted
* @param entries The entries that will be added to the credentials map
* @return {@code this} builder for use in a chained invocation
*/
public final Builder putAllCredentials(Map entries) {
if (this.credentials == null) {
this.credentials = new LinkedHashMap();
}
for (Map.Entry e : entries.entrySet()) {
String k = e.getKey();
Object v = e.getValue();
this.credentials.put(k, v);
}
return this;
}
/**
* Initializes the value for the {@link RawCloudServiceKey#getServiceInstance() serviceInstance} attribute.
* @param serviceInstance The value for serviceInstance
* @return {@code this} builder for use in a chained invocation
*/
public final Builder serviceInstance(Derivable serviceInstance) {
this.serviceInstance = Objects.requireNonNull(serviceInstance, "serviceInstance");
initBits &= ~INIT_BIT_SERVICE_INSTANCE;
return this;
}
/**
* Builds a new {@link ImmutableRawCloudServiceKey ImmutableRawCloudServiceKey}.
* @return An immutable instance of RawCloudServiceKey
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableRawCloudServiceKey build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableRawCloudServiceKey(
serviceBindingResource,
credentials == null ? null : createUnmodifiableMap(false, false, credentials),
serviceInstance);
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList<>();
if ((initBits & INIT_BIT_SERVICE_BINDING_RESOURCE) != 0) attributes.add("serviceBindingResource");
if ((initBits & INIT_BIT_SERVICE_INSTANCE) != 0) attributes.add("serviceInstance");
return "Cannot build RawCloudServiceKey, some of required attributes are not set " + attributes;
}
}
private static Map createUnmodifiableMap(boolean checkNulls, boolean skipNulls, Map extends K, ? extends V> map) {
switch (map.size()) {
case 0: return Collections.emptyMap();
case 1: {
Map.Entry extends K, ? extends V> e = map.entrySet().iterator().next();
K k = e.getKey();
V v = e.getValue();
if (checkNulls) {
Objects.requireNonNull(k, "key");
Objects.requireNonNull(v, v == null ? "value for key: " + k : null);
}
if (skipNulls && (k == null || v == null)) {
return Collections.emptyMap();
}
return Collections.singletonMap(k, v);
}
default: {
Map linkedMap = new LinkedHashMap<>(map.size() * 4 / 3 + 1);
if (skipNulls || checkNulls) {
for (Map.Entry extends K, ? extends V> e : map.entrySet()) {
K k = e.getKey();
V v = e.getValue();
if (skipNulls) {
if (k == null || v == null) continue;
} else if (checkNulls) {
Objects.requireNonNull(k, "key");
Objects.requireNonNull(v, v == null ? "value for key: " + k : null);
}
linkedMap.put(k, v);
}
} else {
linkedMap.putAll(map);
}
return Collections.unmodifiableMap(linkedMap);
}
}
}
}