io.syndesis.common.model.ImmutableDataShape Maven / Gradle / Ivy
/*
* Copyright (C) 2016 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.syndesis.common.model;
import com.fasterxml.jackson.annotation.JsonProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.ObjectStreamException;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Generated;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validation;
import javax.validation.Validator;
/**
* Immutable implementation of {@link DataShape}.
*
* Use the builder to create immutable instances:
* {@code new DataShape.Builder()}.
* Use the static factory method to create immutable instances:
* {@code ImmutableDataShape.of()}.
*/
@SuppressWarnings({"ArrayEquals", "ArrayHashCode", "ArrayToString", "all"})
@SuppressFBWarnings
@Generated({"Immutables.generator", "DataShape"})
final class ImmutableDataShape implements DataShape {
private final String name;
private final String description;
private final DataShapeKinds kind;
private final String type;
private final String specification;
private final byte[] exemplar;
private final Map metadata;
private ImmutableDataShape(
String name,
String description,
DataShapeKinds kind,
String type,
String specification,
Optional exemplar,
Map metadata) {
this.name = name;
this.description = description;
this.kind = kind;
this.type = type;
this.specification = specification;
this.exemplar = exemplar.orElse(null);
this.metadata = createUnmodifiableMap(true, false, metadata);
}
private ImmutableDataShape(
ImmutableDataShape original,
String name,
String description,
DataShapeKinds kind,
String type,
String specification,
byte[] exemplar,
Map metadata) {
this.name = name;
this.description = description;
this.kind = kind;
this.type = type;
this.specification = specification;
this.exemplar = exemplar;
this.metadata = metadata;
}
/**
* @return The value of the {@code name} attribute
*/
@JsonProperty("name")
@Override
public String getName() {
return name;
}
/**
* @return The value of the {@code description} attribute
*/
@JsonProperty("description")
@Override
public String getDescription() {
return description;
}
/**
* @return The value of the {@code kind} attribute
*/
@JsonProperty("kind")
@Override
public DataShapeKinds getKind() {
return kind;
}
/**
* @return The value of the {@code type} attribute
*/
@JsonProperty("type")
@Override
public String getType() {
return type;
}
/**
* @return The value of the {@code specification} attribute
*/
@JsonProperty("specification")
@Override
public String getSpecification() {
return specification;
}
/**
* @return The value of the {@code exemplar} attribute
*/
@JsonProperty("exemplar")
@Override
public Optional getExemplar() {
return Optional.ofNullable(exemplar);
}
/**
* @return The value of the {@code metadata} attribute
*/
@JsonProperty("metadata")
@Override
public Map getMetadata() {
return metadata;
}
/**
* Copy the current immutable object by setting a value for the {@link DataShape#getName() name} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for name (can be {@code null})
* @return A modified copy of the {@code this} object
*/
public final ImmutableDataShape withName(String value) {
if (Objects.equals(this.name, value)) return this;
return validate(new ImmutableDataShape(
this,
value,
this.description,
this.kind,
this.type,
this.specification,
this.exemplar,
this.metadata));
}
/**
* Copy the current immutable object by setting a value for the {@link DataShape#getDescription() description} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for description (can be {@code null})
* @return A modified copy of the {@code this} object
*/
public final ImmutableDataShape withDescription(String value) {
if (Objects.equals(this.description, value)) return this;
return validate(new ImmutableDataShape(this, this.name, value, this.kind, this.type, this.specification, this.exemplar, this.metadata));
}
/**
* Copy the current immutable object by setting a value for the {@link DataShape#getKind() kind} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for kind (can be {@code null})
* @return A modified copy of the {@code this} object
*/
public final ImmutableDataShape withKind(DataShapeKinds value) {
if (this.kind == value) return this;
return validate(new ImmutableDataShape(
this,
this.name,
this.description,
value,
this.type,
this.specification,
this.exemplar,
this.metadata));
}
/**
* Copy the current immutable object by setting a value for the {@link DataShape#getType() type} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for type (can be {@code null})
* @return A modified copy of the {@code this} object
*/
public final ImmutableDataShape withType(String value) {
if (Objects.equals(this.type, value)) return this;
return validate(new ImmutableDataShape(
this,
this.name,
this.description,
this.kind,
value,
this.specification,
this.exemplar,
this.metadata));
}
/**
* Copy the current immutable object by setting a value for the {@link DataShape#getSpecification() specification} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for specification (can be {@code null})
* @return A modified copy of the {@code this} object
*/
public final ImmutableDataShape withSpecification(String value) {
if (Objects.equals(this.specification, value)) return this;
return validate(new ImmutableDataShape(this, this.name, this.description, this.kind, this.type, value, this.exemplar, this.metadata));
}
/**
* Copy the current immutable object by setting a present value for the optional {@link DataShape#getExemplar() exemplar} attribute.
* @param value The value for exemplar
* @return A modified copy of {@code this} object
*/
public final ImmutableDataShape withExemplar(byte[] value) {
byte[] newValue = Objects.requireNonNull(value, "exemplar");
if (this.exemplar == newValue) return this;
return validate(new ImmutableDataShape(
this,
this.name,
this.description,
this.kind,
this.type,
this.specification,
newValue,
this.metadata));
}
/**
* Copy the current immutable object by setting an optional value for the {@link DataShape#getExemplar() exemplar} attribute.
* A shallow reference equality check is used on unboxed optional value to prevent copying of the same value by returning {@code this}.
* @param optional A value for exemplar
* @return A modified copy of {@code this} object
*/
public final ImmutableDataShape withExemplar(Optional optional) {
byte[] value = optional.orElse(null);
if (this.exemplar == value) return this;
return validate(new ImmutableDataShape(
this,
this.name,
this.description,
this.kind,
this.type,
this.specification,
value,
this.metadata));
}
/**
* Copy the current immutable object by replacing the {@link DataShape#getMetadata() metadata} 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 metadata map
* @return A modified copy of {@code this} object
*/
public final ImmutableDataShape withMetadata(Map entries) {
if (this.metadata == entries) return this;
Map newValue = createUnmodifiableMap(true, false, entries);
return validate(new ImmutableDataShape(
this,
this.name,
this.description,
this.kind,
this.type,
this.specification,
this.exemplar,
newValue));
}
/**
* This instance is equal to all instances of {@code ImmutableDataShape} 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 ImmutableDataShape
&& equalTo((ImmutableDataShape) another);
}
private boolean equalTo(ImmutableDataShape another) {
return Objects.equals(name, another.name)
&& Objects.equals(description, another.description)
&& Objects.equals(kind, another.kind)
&& Objects.equals(type, another.type)
&& Objects.equals(specification, another.specification)
&& Objects.equals(exemplar, another.exemplar)
&& metadata.equals(another.metadata);
}
/**
* Computes a hash code from attributes: {@code name}, {@code description}, {@code kind}, {@code type}, {@code specification}, {@code exemplar}, {@code metadata}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + Objects.hashCode(name);
h += (h << 5) + Objects.hashCode(description);
h += (h << 5) + Objects.hashCode(kind);
h += (h << 5) + Objects.hashCode(type);
h += (h << 5) + Objects.hashCode(specification);
h += (h << 5) + Objects.hashCode(exemplar);
h += (h << 5) + metadata.hashCode();
return h;
}
/**
* Prints the immutable value {@code DataShape} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder("DataShape{");
if (name != null) {
builder.append("name=").append(name);
}
if (description != null) {
if (builder.length() > 10) builder.append(", ");
builder.append("description=").append(description);
}
if (kind != null) {
if (builder.length() > 10) builder.append(", ");
builder.append("kind=").append(kind);
}
if (type != null) {
if (builder.length() > 10) builder.append(", ");
builder.append("type=").append(type);
}
if (specification != null) {
if (builder.length() > 10) builder.append(", ");
builder.append("specification=").append(specification);
}
if (exemplar != null) {
if (builder.length() > 10) builder.append(", ");
builder.append("exemplar=").append(exemplar);
}
if (builder.length() > 10) builder.append(", ");
builder.append("metadata=").append(metadata);
return builder.append("}").toString();
}
/**
* Construct a new immutable {@code DataShape} instance.
* @param name The value for the {@code name} attribute
* @param description The value for the {@code description} attribute
* @param kind The value for the {@code kind} attribute
* @param type The value for the {@code type} attribute
* @param specification The value for the {@code specification} attribute
* @param exemplar The value for the {@code exemplar} attribute
* @param metadata The value for the {@code metadata} attribute
* @return An immutable DataShape instance
*/
public static DataShape of(String name, String description, DataShapeKinds kind, String type, String specification, Optional exemplar, Map metadata) {
return validate(new ImmutableDataShape(name, description, kind, type, specification, exemplar, metadata));
}
private static final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
private static ImmutableDataShape validate(ImmutableDataShape instance) {
Set> constraintViolations = validator.validate(instance);
if (!constraintViolations.isEmpty()) {
throw new ConstraintViolationException(constraintViolations);
}
return instance;
}
/**
* Creates an immutable copy of a {@link DataShape} 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 DataShape instance
*/
public static DataShape copyOf(DataShape instance) {
if (instance instanceof ImmutableDataShape) {
return (ImmutableDataShape) instance;
}
return new DataShape.Builder()
.createFrom(instance)
.build();
}
private Object readResolve() throws ObjectStreamException {
return validate(this);
}
/**
* Builds instances of type {@link DataShape DataShape}.
* 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 class Builder {
private String name;
private String description;
private DataShapeKinds kind;
private String type;
private String specification;
private byte[] exemplar;
private Map metadata = new LinkedHashMap();
/**
* Creates a builder for {@link DataShape DataShape} instances.
*/
public Builder() {
if (!(this instanceof DataShape.Builder)) {
throw new UnsupportedOperationException("Use: new DataShape.Builder()");
}
}
/**
* Fill a builder with attribute values from the provided {@code io.syndesis.common.model.WithName} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
public final DataShape.Builder createFrom(WithName instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return (DataShape.Builder) this;
}
/**
* Fill a builder with attribute values from the provided {@code io.syndesis.common.model.DataShape} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
public final DataShape.Builder createFrom(DataShape instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return (DataShape.Builder) this;
}
/**
* Fill a builder with attribute values from the provided {@code io.syndesis.common.model.WithMetadata} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
public final DataShape.Builder createFrom(WithMetadata instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return (DataShape.Builder) this;
}
private void from(Object object) {
long bits = 0;
if (object instanceof WithName) {
WithName instance = (WithName) object;
if ((bits & 0x2L) == 0) {
String nameValue = instance.getName();
if (nameValue != null) {
name(nameValue);
}
bits |= 0x2L;
}
}
if (object instanceof DataShape) {
DataShape instance = (DataShape) object;
if ((bits & 0x1L) == 0) {
putAllMetadata(instance.getMetadata());
bits |= 0x1L;
}
DataShapeKinds kindValue = instance.getKind();
if (kindValue != null) {
kind(kindValue);
}
if ((bits & 0x2L) == 0) {
String nameValue = instance.getName();
if (nameValue != null) {
name(nameValue);
}
bits |= 0x2L;
}
Optional exemplarOptional = instance.getExemplar();
if (exemplarOptional.isPresent()) {
exemplar(exemplarOptional);
}
String descriptionValue = instance.getDescription();
if (descriptionValue != null) {
description(descriptionValue);
}
String specificationValue = instance.getSpecification();
if (specificationValue != null) {
specification(specificationValue);
}
String typeValue = instance.getType();
if (typeValue != null) {
type(typeValue);
}
}
if (object instanceof WithMetadata) {
WithMetadata instance = (WithMetadata) object;
if ((bits & 0x1L) == 0) {
putAllMetadata(instance.getMetadata());
bits |= 0x1L;
}
}
}
/**
* Initializes the value for the {@link DataShape#getName() name} attribute.
* @param name The value for name (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("name")
public final DataShape.Builder name(String name) {
this.name = name;
return (DataShape.Builder) this;
}
/**
* Initializes the value for the {@link DataShape#getDescription() description} attribute.
* @param description The value for description (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("description")
public final DataShape.Builder description(String description) {
this.description = description;
return (DataShape.Builder) this;
}
/**
* Initializes the value for the {@link DataShape#getKind() kind} attribute.
* @param kind The value for kind (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("kind")
public final DataShape.Builder kind(DataShapeKinds kind) {
this.kind = kind;
return (DataShape.Builder) this;
}
/**
* Initializes the value for the {@link DataShape#getType() type} attribute.
* @param type The value for type (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("type")
public final DataShape.Builder type(String type) {
this.type = type;
return (DataShape.Builder) this;
}
/**
* Initializes the value for the {@link DataShape#getSpecification() specification} attribute.
* @param specification The value for specification (can be {@code null})
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("specification")
public final DataShape.Builder specification(String specification) {
this.specification = specification;
return (DataShape.Builder) this;
}
/**
* Initializes the optional value {@link DataShape#getExemplar() exemplar} to exemplar.
* @param exemplar The value for exemplar
* @return {@code this} builder for chained invocation
*/
public final DataShape.Builder exemplar(byte[] exemplar) {
this.exemplar = Objects.requireNonNull(exemplar, "exemplar");
return (DataShape.Builder) this;
}
/**
* Initializes the optional value {@link DataShape#getExemplar() exemplar} to exemplar.
* @param exemplar The value for exemplar
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("exemplar")
public final DataShape.Builder exemplar(Optional exemplar) {
this.exemplar = exemplar.orElse(null);
return (DataShape.Builder) this;
}
/**
* Put one entry to the {@link DataShape#getMetadata() metadata} map.
* @param key The key in the metadata map
* @param value The associated value in the metadata map
* @return {@code this} builder for use in a chained invocation
*/
public final DataShape.Builder putMetadata(String key, String value) {
this.metadata.put(
Objects.requireNonNull(key, "metadata key"),
Objects.requireNonNull(value, "metadata value"));
return (DataShape.Builder) this;
}
/**
* Put one entry to the {@link DataShape#getMetadata() metadata} map. Nulls are not permitted
* @param entry The key and value entry
* @return {@code this} builder for use in a chained invocation
*/
public final DataShape.Builder putMetadata(Map.Entry entry) {
String k = entry.getKey();
String v = entry.getValue();
this.metadata.put(
Objects.requireNonNull(k, "metadata key"),
Objects.requireNonNull(v, "metadata value"));
return (DataShape.Builder) this;
}
/**
* Sets or replaces all mappings from the specified map as entries for the {@link DataShape#getMetadata() metadata} map. Nulls are not permitted
* @param metadata The entries that will be added to the metadata map
* @return {@code this} builder for use in a chained invocation
*/
@JsonProperty("metadata")
public final DataShape.Builder metadata(Map metadata) {
this.metadata.clear();
return putAllMetadata(metadata);
}
/**
* Put all mappings from the specified map as entries to {@link DataShape#getMetadata() metadata} map. Nulls are not permitted
* @param metadata The entries that will be added to the metadata map
* @return {@code this} builder for use in a chained invocation
*/
public final DataShape.Builder putAllMetadata(Map metadata) {
for (Map.Entry entry : metadata.entrySet()) {
String k = entry.getKey();
String v = entry.getValue();
this.metadata.put(
Objects.requireNonNull(k, "metadata key"),
Objects.requireNonNull(v, "metadata value"));
}
return (DataShape.Builder) this;
}
/**
* Builds a new {@link DataShape DataShape}.
* @return An immutable instance of DataShape
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public DataShape build() {
return ImmutableDataShape.validate(new ImmutableDataShape(
null,
name,
description,
kind,
type,
specification,
exemplar,
createUnmodifiableMap(false, false, metadata)));
}
}
private static Map createUnmodifiableMap(boolean checkNulls, boolean skipNulls, Map map) {
switch (map.size()) {
case 0: return Collections.emptyMap();
case 1: {
Map.Entry e = map.entrySet().iterator().next();
K k = e.getKey();
V v = e.getValue();
if (checkNulls) {
Objects.requireNonNull(k, "key");
Objects.requireNonNull(v, "value");
}
if (skipNulls && (k == null || v == null)) {
return Collections.emptyMap();
}
return Collections.singletonMap(k, v);
}
default: {
Map linkedMap = new LinkedHashMap(map.size());
if (skipNulls || checkNulls) {
for (Map.Entry 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, "value");
}
linkedMap.put(k, v);
}
} else {
linkedMap.putAll(map);
}
return Collections.unmodifiableMap(linkedMap);
}
}
}
}