All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sinch.xms.api.PagedGroupResultImpl Maven / Gradle / Ivy

There is a newer version: 2.2.1
Show newest version
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 PagedGroupResult}.
 * 

* Use the builder to create immutable instances: * {@code new PagedGroupResult.Builder()}. */ @SuppressWarnings({"all"}) @ParametersAreNonnullByDefault @Immutable @CheckReturnValue final class PagedGroupResultImpl extends PagedGroupResult { private final List content; private final int page; private final int size; private final int totalSize; private PagedGroupResultImpl(List content, int page, int size, int totalSize) { this.content = content; this.page = page; this.size = size; this.totalSize = totalSize; } /** * @return The value of the {@code content} attribute */ @JsonProperty("groups") @Override public List content() { return content; } /** * @return The value of the {@code page} attribute */ @JsonProperty("page") @Override public int page() { return page; } /** * @return The value of the {@code size} attribute */ @JsonProperty("page_size") @Override public int size() { return size; } /** * @return The value of the {@code totalSize} attribute */ @JsonProperty("count") @Override public int totalSize() { return totalSize; } /** * Copy the current immutable object with elements that replace the content of {@link PagedGroupResult#content() content}. * @param elements The elements to set * @return A modified copy of {@code this} object */ public final PagedGroupResultImpl withContent(GroupResult... elements) { List newValue = createUnmodifiableList(false, createSafeList(Arrays.asList(elements), true, false)); return new PagedGroupResultImpl(newValue, this.page, this.size, this.totalSize); } /** * Copy the current immutable object with elements that replace the content of {@link PagedGroupResult#content() content}. * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param elements An iterable of content elements to set * @return A modified copy of {@code this} object */ public final PagedGroupResultImpl withContent(Iterable elements) { if (this.content == elements) return this; List newValue = createUnmodifiableList(false, createSafeList(elements, true, false)); return new PagedGroupResultImpl(newValue, this.page, this.size, this.totalSize); } /** * Copy the current immutable object by setting a value for the {@link PagedGroupResult#page() page} attribute. * A value equality check is used to prevent copying of the same value by returning {@code this}. * @param value A new value for page * @return A modified copy of the {@code this} object */ public final PagedGroupResultImpl withPage(int value) { if (this.page == value) return this; return new PagedGroupResultImpl(this.content, value, this.size, this.totalSize); } /** * Copy the current immutable object by setting a value for the {@link PagedGroupResult#size() size} attribute. * A value equality check is used to prevent copying of the same value by returning {@code this}. * @param value A new value for size * @return A modified copy of the {@code this} object */ public final PagedGroupResultImpl withSize(int value) { if (this.size == value) return this; return new PagedGroupResultImpl(this.content, this.page, value, this.totalSize); } /** * Copy the current immutable object by setting a value for the {@link PagedGroupResult#totalSize() totalSize} attribute. * A value equality check is used to prevent copying of the same value by returning {@code this}. * @param value A new value for totalSize * @return A modified copy of the {@code this} object */ public final PagedGroupResultImpl withTotalSize(int value) { if (this.totalSize == value) return this; return new PagedGroupResultImpl(this.content, this.page, this.size, value); } /** * This instance is equal to all instances of {@code PagedGroupResultImpl} 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 PagedGroupResultImpl && equalTo((PagedGroupResultImpl) another); } private boolean equalTo(PagedGroupResultImpl another) { return content.equals(another.content) && page == another.page && size == another.size && totalSize == another.totalSize; } /** * Computes a hash code from attributes: {@code content}, {@code page}, {@code size}, {@code totalSize}. * @return hashCode value */ @Override public int hashCode() { int h = 31; h = h * 17 + content.hashCode(); h = h * 17 + page; h = h * 17 + size; h = h * 17 + totalSize; return h; } /** * Prints the immutable value {@code PagedGroupResult} with attribute values. * @return A string representation of the value */ @Override public String toString() { return "PagedGroupResult{" + "content=" + content + ", page=" + page + ", size=" + size + ", totalSize=" + totalSize + "}"; } /** * Creates an immutable copy of a {@link PagedGroupResult} 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 PagedGroupResult instance */ public static PagedGroupResult copyOf(PagedGroupResult instance) { if (instance instanceof PagedGroupResultImpl) { return (PagedGroupResultImpl) instance; } return new PagedGroupResult.Builder() .using(instance) .build(); } /** * Builds instances of type {@link PagedGroupResult PagedGroupResult}. * 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_PAGE = 0x1L; private static final long INIT_BIT_SIZE = 0x2L; private static final long INIT_BIT_TOTAL_SIZE = 0x4L; private long initBits = 0x7L; private List content = new ArrayList(); private int page; private int size; private int totalSize; /** * Creates a builder for {@link PagedGroupResult PagedGroupResult} instances. */ Builder() { if (!(this instanceof PagedGroupResult.Builder)) { throw new UnsupportedOperationException("Use: new PagedGroupResult.Builder()"); } } /** * Fill a builder with attribute values from the provided {@code PagedGroupResult} 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 PagedGroupResult.Builder using(PagedGroupResult instance) { PagedGroupResultImpl.requireNonNull(instance, "instance"); addAllContent(instance.content()); page(instance.page()); size(instance.size()); totalSize(instance.totalSize()); return (PagedGroupResult.Builder) this; } /** * Adds one element to {@link PagedGroupResult#content() content} list. * @param element A content element * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final PagedGroupResult.Builder addContent(GroupResult element) { this.content.add(PagedGroupResultImpl.requireNonNull(element, "content element")); return (PagedGroupResult.Builder) this; } /** * Adds elements to {@link PagedGroupResult#content() content} list. * @param elements An array of content elements * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final PagedGroupResult.Builder addContent(GroupResult... elements) { for (GroupResult element : elements) { this.content.add(PagedGroupResultImpl.requireNonNull(element, "content element")); } return (PagedGroupResult.Builder) this; } /** * Sets or replaces all elements for {@link PagedGroupResult#content() content} list. * @param elements An iterable of content elements * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue @JsonProperty("groups") public final PagedGroupResult.Builder content(Iterable elements) { this.content.clear(); return addAllContent(elements); } /** * Adds elements to {@link PagedGroupResult#content() content} list. * @param elements An iterable of content elements * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final PagedGroupResult.Builder addAllContent(Iterable elements) { for (GroupResult element : elements) { this.content.add(PagedGroupResultImpl.requireNonNull(element, "content element")); } return (PagedGroupResult.Builder) this; } /** * Initializes the value for the {@link PagedGroupResult#page() page} attribute. * @param page The value for page * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue @JsonProperty("page") public final PagedGroupResult.Builder page(int page) { this.page = page; initBits &= ~INIT_BIT_PAGE; return (PagedGroupResult.Builder) this; } /** * Initializes the value for the {@link PagedGroupResult#size() size} attribute. * @param size The value for size * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue @JsonProperty("page_size") public final PagedGroupResult.Builder size(int size) { this.size = size; initBits &= ~INIT_BIT_SIZE; return (PagedGroupResult.Builder) this; } /** * Initializes the value for the {@link PagedGroupResult#totalSize() totalSize} attribute. * @param totalSize The value for totalSize * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue @JsonProperty("count") public final PagedGroupResult.Builder totalSize(int totalSize) { this.totalSize = totalSize; initBits &= ~INIT_BIT_TOTAL_SIZE; return (PagedGroupResult.Builder) this; } /** * Builds a new {@link PagedGroupResult PagedGroupResult}. * @return An immutable instance of PagedGroupResult * @throws java.lang.IllegalStateException if any required attributes are missing */ public PagedGroupResult build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return new PagedGroupResultImpl(createUnmodifiableList(true, content), page, size, totalSize); } private String formatRequiredAttributesMessage() { List attributes = new ArrayList(); if ((initBits & INIT_BIT_PAGE) != 0) attributes.add("page"); if ((initBits & INIT_BIT_SIZE) != 0) attributes.add("size"); if ((initBits & INIT_BIT_TOTAL_SIZE) != 0) attributes.add("totalSize"); return "Cannot build PagedGroupResult, 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 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) PagedGroupResultImpl.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); } } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy