com.sinch.xms.api.StatusImpl Maven / Gradle / Ivy
Show all versions of sdk-sms Show documentation
package com.sinch.xms.api;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
/**
* Immutable implementation of {@link Status}.
*
* Use the builder to create immutable instances:
* {@code new Status.Builder()}.
*/
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@Immutable
@CheckReturnValue
final class StatusImpl extends Status {
private final int code;
private final DeliveryStatus status;
private final int count;
private final List recipients;
private StatusImpl(
int code,
DeliveryStatus status,
int count,
List recipients) {
this.code = code;
this.status = status;
this.count = count;
this.recipients = recipients;
}
/**
* @return The value of the {@code code} attribute
*/
@JsonProperty("code")
@Override
public int code() {
return code;
}
/**
* @return The value of the {@code status} attribute
*/
@JsonProperty("status")
@Override
public DeliveryStatus status() {
return status;
}
/**
* @return The value of the {@code count} attribute
*/
@JsonProperty("count")
@Override
public int count() {
return count;
}
/**
* @return The value of the {@code recipients} attribute
*/
@JsonProperty("recipients")
@Override
public List recipients() {
return recipients;
}
/**
* Copy the current immutable object by setting a value for the {@link Status#code() code} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for code
* @return A modified copy of the {@code this} object
*/
public final StatusImpl withCode(int value) {
if (this.code == value) return this;
return new StatusImpl(value, this.status, this.count, this.recipients);
}
/**
* Copy the current immutable object by setting a value for the {@link Status#status() status} 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 status
* @return A modified copy of the {@code this} object
*/
public final StatusImpl withStatus(DeliveryStatus value) {
if (this.status == value) return this;
DeliveryStatus newValue = StatusImpl.requireNonNull(value, "status");
return new StatusImpl(this.code, newValue, this.count, this.recipients);
}
/**
* Copy the current immutable object by setting a value for the {@link Status#count() count} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for count
* @return A modified copy of the {@code this} object
*/
public final StatusImpl withCount(int value) {
if (this.count == value) return this;
return new StatusImpl(this.code, this.status, value, this.recipients);
}
/**
* Copy the current immutable object with elements that replace the content of {@link Status#recipients() recipients}.
* @param elements The elements to set
* @return A modified copy of {@code this} object
*/
public final StatusImpl withRecipients(String... elements) {
List newValue = createUnmodifiableList(false, createSafeList(Arrays.asList(elements), true, false));
return new StatusImpl(this.code, this.status, this.count, newValue);
}
/**
* Copy the current immutable object with elements that replace the content of {@link Status#recipients() recipients}.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param elements An iterable of recipients elements to set
* @return A modified copy of {@code this} object
*/
public final StatusImpl withRecipients(Iterable elements) {
if (this.recipients == elements) return this;
List newValue = createUnmodifiableList(false, createSafeList(elements, true, false));
return new StatusImpl(this.code, this.status, this.count, newValue);
}
/**
* This instance is equal to all instances of {@code StatusImpl} that have equal attribute values.
* @return {@code true} if {@code this} is equal to {@code another} instance
*/
@Override
public boolean equals(@Nullable Object another) {
if (this == another) return true;
return another instanceof StatusImpl
&& equalTo((StatusImpl) another);
}
private boolean equalTo(StatusImpl another) {
return code == another.code
&& status.equals(another.status)
&& count == another.count
&& recipients.equals(another.recipients);
}
/**
* Computes a hash code from attributes: {@code code}, {@code status}, {@code count}, {@code recipients}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 31;
h = h * 17 + code;
h = h * 17 + status.hashCode();
h = h * 17 + count;
h = h * 17 + recipients.hashCode();
return h;
}
/**
* Prints the immutable value {@code Status} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "Status{"
+ "code=" + code
+ ", status=" + status
+ ", count=" + count
+ ", recipients=" + recipients
+ "}";
}
/**
* Creates an immutable copy of a {@link Status} 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 Status instance
*/
public static Status copyOf(Status instance) {
if (instance instanceof StatusImpl) {
return (StatusImpl) instance;
}
return new Status.Builder()
.using(instance)
.build();
}
/**
* Builds instances of type {@link Status Status}.
* 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.
*/
@NotThreadSafe
static class Builder {
private static final long INIT_BIT_CODE = 0x1L;
private static final long INIT_BIT_STATUS = 0x2L;
private static final long INIT_BIT_COUNT = 0x4L;
private long initBits = 0x7L;
private int code;
private @Nullable DeliveryStatus status;
private int count;
private List recipients = new ArrayList();
/**
* Creates a builder for {@link Status Status} instances.
*/
Builder() {
if (!(this instanceof Status.Builder)) {
throw new UnsupportedOperationException("Use: new Status.Builder()");
}
}
/**
* Fill a builder with attribute values from the provided {@code Status} 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
*/
@CanIgnoreReturnValue
public final Status.Builder using(Status instance) {
StatusImpl.requireNonNull(instance, "instance");
code(instance.code());
status(instance.status());
count(instance.count());
addAllRecipients(instance.recipients());
return (Status.Builder) this;
}
/**
* Initializes the value for the {@link Status#code() code} attribute.
* @param code The value for code
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("code")
public final Status.Builder code(int code) {
this.code = code;
initBits &= ~INIT_BIT_CODE;
return (Status.Builder) this;
}
/**
* Initializes the value for the {@link Status#status() status} attribute.
* @param status The value for status
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("status")
public final Status.Builder status(DeliveryStatus status) {
this.status = StatusImpl.requireNonNull(status, "status");
initBits &= ~INIT_BIT_STATUS;
return (Status.Builder) this;
}
/**
* Initializes the value for the {@link Status#count() count} attribute.
* @param count The value for count
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("count")
public final Status.Builder count(int count) {
this.count = count;
initBits &= ~INIT_BIT_COUNT;
return (Status.Builder) this;
}
/**
* Adds one element to {@link Status#recipients() recipients} list.
* @param element A recipients element
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Status.Builder addRecipient(String element) {
this.recipients.add(StatusImpl.requireNonNull(element, "recipients element"));
return (Status.Builder) this;
}
/**
* Adds elements to {@link Status#recipients() recipients} list.
* @param elements An array of recipients elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Status.Builder addRecipient(String... elements) {
for (String element : elements) {
this.recipients.add(StatusImpl.requireNonNull(element, "recipients element"));
}
return (Status.Builder) this;
}
/**
* Sets or replaces all elements for {@link Status#recipients() recipients} list.
* @param elements An iterable of recipients elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
@JsonProperty("recipients")
public final Status.Builder recipients(Iterable elements) {
this.recipients.clear();
return addAllRecipients(elements);
}
/**
* Adds elements to {@link Status#recipients() recipients} list.
* @param elements An iterable of recipients elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Status.Builder addAllRecipients(Iterable elements) {
for (String element : elements) {
this.recipients.add(StatusImpl.requireNonNull(element, "recipients element"));
}
return (Status.Builder) this;
}
/**
* Builds a new {@link Status Status}.
* @return An immutable instance of Status
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public Status build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new StatusImpl(code, status, count, createUnmodifiableList(true, recipients));
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList();
if ((initBits & INIT_BIT_CODE) != 0) attributes.add("code");
if ((initBits & INIT_BIT_STATUS) != 0) attributes.add("status");
if ((initBits & INIT_BIT_COUNT) != 0) attributes.add("count");
return "Cannot build Status, some of required attributes are not set " + attributes;
}
}
private static T requireNonNull(T object, String message) {
if (object == null) throw new NullPointerException(message);
return object;
}
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) StatusImpl.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);
}
}
}
}