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

io.resys.hdes.ast.api.nodes.ImmutableHeaders Maven / Gradle / Ivy

The newest version!
package io.resys.hdes.ast.api.nodes;

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 javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import org.immutables.value.Generated;

/**
 * Immutable implementation of {@link BodyNode.Headers}.
 * 

* Use the builder to create immutable instances: * {@code ImmutableHeaders.builder()}. */ @Generated(from = "BodyNode.Headers", generator = "Immutables") @SuppressWarnings({"all"}) @ParametersAreNonnullByDefault @javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor") @Immutable public final class ImmutableHeaders implements BodyNode.Headers { private final HdesNode.Token token; private final List acceptDefs; private final List returnDefs; private ImmutableHeaders( HdesNode.Token token, List acceptDefs, List returnDefs) { this.token = token; this.acceptDefs = acceptDefs; this.returnDefs = returnDefs; } /** * @return The value of the {@code token} attribute */ @Override public HdesNode.Token getToken() { return token; } /** * @return The value of the {@code acceptDefs} attribute */ @Override public List getAcceptDefs() { return acceptDefs; } /** * @return The value of the {@code returnDefs} attribute */ @Override public List getReturnDefs() { return returnDefs; } /** * Copy the current immutable object by setting a value for the {@link BodyNode.Headers#getToken() token} 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 token * @return A modified copy of the {@code this} object */ public final ImmutableHeaders withToken(HdesNode.Token value) { if (this.token == value) return this; HdesNode.Token newValue = Objects.requireNonNull(value, "token"); return new ImmutableHeaders(newValue, this.acceptDefs, this.returnDefs); } /** * Copy the current immutable object with elements that replace the content of {@link BodyNode.Headers#getAcceptDefs() acceptDefs}. * @param elements The elements to set * @return A modified copy of {@code this} object */ public final ImmutableHeaders withAcceptDefs(BodyNode.TypeDef... elements) { List newValue = createUnmodifiableList(false, createSafeList(Arrays.asList(elements), true, false)); return new ImmutableHeaders(this.token, newValue, this.returnDefs); } /** * Copy the current immutable object with elements that replace the content of {@link BodyNode.Headers#getAcceptDefs() acceptDefs}. * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param elements An iterable of acceptDefs elements to set * @return A modified copy of {@code this} object */ public final ImmutableHeaders withAcceptDefs(Iterable elements) { if (this.acceptDefs == elements) return this; List newValue = createUnmodifiableList(false, createSafeList(elements, true, false)); return new ImmutableHeaders(this.token, newValue, this.returnDefs); } /** * Copy the current immutable object with elements that replace the content of {@link BodyNode.Headers#getReturnDefs() returnDefs}. * @param elements The elements to set * @return A modified copy of {@code this} object */ public final ImmutableHeaders withReturnDefs(BodyNode.TypeDef... elements) { List newValue = createUnmodifiableList(false, createSafeList(Arrays.asList(elements), true, false)); return new ImmutableHeaders(this.token, this.acceptDefs, newValue); } /** * Copy the current immutable object with elements that replace the content of {@link BodyNode.Headers#getReturnDefs() returnDefs}. * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param elements An iterable of returnDefs elements to set * @return A modified copy of {@code this} object */ public final ImmutableHeaders withReturnDefs(Iterable elements) { if (this.returnDefs == elements) return this; List newValue = createUnmodifiableList(false, createSafeList(elements, true, false)); return new ImmutableHeaders(this.token, this.acceptDefs, newValue); } /** * This instance is equal to all instances of {@code ImmutableHeaders} 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 ImmutableHeaders && equalTo((ImmutableHeaders) another); } private boolean equalTo(ImmutableHeaders another) { return token.equals(another.token) && acceptDefs.equals(another.acceptDefs) && returnDefs.equals(another.returnDefs); } /** * Computes a hash code from attributes: {@code token}, {@code acceptDefs}, {@code returnDefs}. * @return hashCode value */ @Override public int hashCode() { int h = 5381; h += (h << 5) + token.hashCode(); h += (h << 5) + acceptDefs.hashCode(); h += (h << 5) + returnDefs.hashCode(); return h; } /** * Prints the immutable value {@code Headers} with attribute values. * @return A string representation of the value */ @Override public String toString() { return "Headers{" + "token=" + token + ", acceptDefs=" + acceptDefs + ", returnDefs=" + returnDefs + "}"; } /** * Creates an immutable copy of a {@link BodyNode.Headers} 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 Headers instance */ public static ImmutableHeaders copyOf(BodyNode.Headers instance) { if (instance instanceof ImmutableHeaders) { return (ImmutableHeaders) instance; } return ImmutableHeaders.builder() .from(instance) .build(); } /** * Creates a builder for {@link ImmutableHeaders ImmutableHeaders}. *

   * ImmutableHeaders.builder()
   *    .token(io.resys.hdes.ast.api.nodes.HdesNode.Token) // required {@link BodyNode.Headers#getToken() token}
   *    .addAcceptDefs|addAllAcceptDefs(io.resys.hdes.ast.api.nodes.BodyNode.TypeDef) // {@link BodyNode.Headers#getAcceptDefs() acceptDefs} elements
   *    .addReturnDefs|addAllReturnDefs(io.resys.hdes.ast.api.nodes.BodyNode.TypeDef) // {@link BodyNode.Headers#getReturnDefs() returnDefs} elements
   *    .build();
   * 
* @return A new ImmutableHeaders builder */ public static ImmutableHeaders.Builder builder() { return new ImmutableHeaders.Builder(); } /** * Builds instances of type {@link ImmutableHeaders ImmutableHeaders}. * 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 = "BodyNode.Headers", generator = "Immutables") @NotThreadSafe public static final class Builder { private static final long INIT_BIT_TOKEN = 0x1L; private long initBits = 0x1L; private @Nullable HdesNode.Token token; private List acceptDefs = new ArrayList(); private List returnDefs = new ArrayList(); private Builder() { } /** * Fill a builder with attribute values from the provided {@code io.resys.hdes.ast.api.nodes.HdesNode} instance. * @param instance The instance from which to copy values * @return {@code this} builder for use in a chained invocation */ public final Builder from(HdesNode instance) { Objects.requireNonNull(instance, "instance"); from((Object) instance); return this; } /** * Fill a builder with attribute values from the provided {@code io.resys.hdes.ast.api.nodes.BodyNode.Headers} instance. * @param instance The instance from which to copy values * @return {@code this} builder for use in a chained invocation */ public final Builder from(BodyNode.Headers instance) { Objects.requireNonNull(instance, "instance"); from((Object) instance); return this; } private void from(Object object) { if (object instanceof HdesNode) { HdesNode instance = (HdesNode) object; token(instance.getToken()); } if (object instanceof BodyNode.Headers) { BodyNode.Headers instance = (BodyNode.Headers) object; addAllAcceptDefs(instance.getAcceptDefs()); addAllReturnDefs(instance.getReturnDefs()); } } /** * Initializes the value for the {@link BodyNode.Headers#getToken() token} attribute. * @param token The value for token * @return {@code this} builder for use in a chained invocation */ public final Builder token(HdesNode.Token token) { this.token = Objects.requireNonNull(token, "token"); initBits &= ~INIT_BIT_TOKEN; return this; } /** * Adds one element to {@link BodyNode.Headers#getAcceptDefs() acceptDefs} list. * @param element A acceptDefs element * @return {@code this} builder for use in a chained invocation */ public final Builder addAcceptDefs(BodyNode.TypeDef element) { this.acceptDefs.add(Objects.requireNonNull(element, "acceptDefs element")); return this; } /** * Adds elements to {@link BodyNode.Headers#getAcceptDefs() acceptDefs} list. * @param elements An array of acceptDefs elements * @return {@code this} builder for use in a chained invocation */ public final Builder addAcceptDefs(BodyNode.TypeDef... elements) { for (BodyNode.TypeDef element : elements) { this.acceptDefs.add(Objects.requireNonNull(element, "acceptDefs element")); } return this; } /** * Sets or replaces all elements for {@link BodyNode.Headers#getAcceptDefs() acceptDefs} list. * @param elements An iterable of acceptDefs elements * @return {@code this} builder for use in a chained invocation */ public final Builder acceptDefs(Iterable elements) { this.acceptDefs.clear(); return addAllAcceptDefs(elements); } /** * Adds elements to {@link BodyNode.Headers#getAcceptDefs() acceptDefs} list. * @param elements An iterable of acceptDefs elements * @return {@code this} builder for use in a chained invocation */ public final Builder addAllAcceptDefs(Iterable elements) { for (BodyNode.TypeDef element : elements) { this.acceptDefs.add(Objects.requireNonNull(element, "acceptDefs element")); } return this; } /** * Adds one element to {@link BodyNode.Headers#getReturnDefs() returnDefs} list. * @param element A returnDefs element * @return {@code this} builder for use in a chained invocation */ public final Builder addReturnDefs(BodyNode.TypeDef element) { this.returnDefs.add(Objects.requireNonNull(element, "returnDefs element")); return this; } /** * Adds elements to {@link BodyNode.Headers#getReturnDefs() returnDefs} list. * @param elements An array of returnDefs elements * @return {@code this} builder for use in a chained invocation */ public final Builder addReturnDefs(BodyNode.TypeDef... elements) { for (BodyNode.TypeDef element : elements) { this.returnDefs.add(Objects.requireNonNull(element, "returnDefs element")); } return this; } /** * Sets or replaces all elements for {@link BodyNode.Headers#getReturnDefs() returnDefs} list. * @param elements An iterable of returnDefs elements * @return {@code this} builder for use in a chained invocation */ public final Builder returnDefs(Iterable elements) { this.returnDefs.clear(); return addAllReturnDefs(elements); } /** * Adds elements to {@link BodyNode.Headers#getReturnDefs() returnDefs} list. * @param elements An iterable of returnDefs elements * @return {@code this} builder for use in a chained invocation */ public final Builder addAllReturnDefs(Iterable elements) { for (BodyNode.TypeDef element : elements) { this.returnDefs.add(Objects.requireNonNull(element, "returnDefs element")); } return this; } /** * Builds a new {@link ImmutableHeaders ImmutableHeaders}. * @return An immutable instance of Headers * @throws java.lang.IllegalStateException if any required attributes are missing */ public ImmutableHeaders build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return new ImmutableHeaders(token, createUnmodifiableList(true, acceptDefs), createUnmodifiableList(true, returnDefs)); } private String formatRequiredAttributesMessage() { List attributes = new ArrayList<>(); if ((initBits & INIT_BIT_TOKEN) != 0) attributes.add("token"); return "Cannot build Headers, some of required attributes are not set " + attributes; } } 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) 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); } } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy