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

net.yudichev.jiotty.appliance.CommandMeta Maven / Gradle / Ivy

package net.yudichev.jiotty.appliance;

import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Var;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import javax.annotation.CheckReturnValue;
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 BaseCommandMeta}.
 * 

* Use the builder to create immutable instances: * {@code CommandMeta.builder()}. */ @Generated(from = "BaseCommandMeta", generator = "Immutables") @SuppressWarnings({"all"}) @ParametersAreNonnullByDefault @javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor") @Immutable @CheckReturnValue public final class CommandMeta> extends BaseCommandMeta { private final String commandName; private final ImmutableMap parameterTypes; private final Function, T> commandFactory; private CommandMeta(CommandMeta.Builder builder) { this.commandName = builder.commandName; this.commandFactory = builder.commandFactory; this.parameterTypes = builder.parameterTypesIsSet() ? builder.parameterTypes.build() : ImmutableMap.copyOf(super.parameterTypes()); } private CommandMeta( String commandName, ImmutableMap parameterTypes, Function, T> commandFactory) { this.commandName = commandName; this.parameterTypes = parameterTypes; this.commandFactory = commandFactory; } /** * @return The value of the {@code commandName} attribute */ @Override public String commandName() { return commandName; } /** * @return The value of the {@code parameterTypes} attribute */ @Override public ImmutableMap parameterTypes() { return parameterTypes; } /** * @return The value of the {@code commandFactory} attribute */ @Override public Function, T> commandFactory() { return commandFactory; } /** * Copy the current immutable object by setting a value for the {@link CommandMeta#commandName() commandName} attribute. * An equals check used to prevent copying of the same value by returning {@code this}. * @param value A new value for commandName * @return A modified copy of the {@code this} object */ public final CommandMeta withCommandName(String value) { String newValue = Objects.requireNonNull(value, "commandName"); if (this.commandName.equals(newValue)) return this; return new CommandMeta<>(newValue, this.parameterTypes, this.commandFactory); } /** * Copy the current immutable object by replacing the {@link CommandMeta#parameterTypes() parameterTypes} 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 parameterTypes map * @return A modified copy of {@code this} object */ public final CommandMeta withParameterTypes(Map entries) { if (this.parameterTypes == entries) return this; ImmutableMap newValue = ImmutableMap.copyOf(entries); return new CommandMeta<>(this.commandName, newValue, this.commandFactory); } /** * Copy the current immutable object by setting a value for the {@link CommandMeta#commandFactory() commandFactory} 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 commandFactory * @return A modified copy of the {@code this} object */ public final CommandMeta withCommandFactory(Function, T> value) { if (this.commandFactory == value) return this; Function, T> newValue = Objects.requireNonNull(value, "commandFactory"); return new CommandMeta<>(this.commandName, this.parameterTypes, newValue); } /** * This instance is equal to all instances of {@code CommandMeta} 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 CommandMeta && equalTo(0, (CommandMeta) another); } private boolean equalTo(int synthetic, CommandMeta another) { return commandName.equals(another.commandName) && parameterTypes.equals(another.parameterTypes) && commandFactory.equals(another.commandFactory); } /** * Computes a hash code from attributes: {@code commandName}, {@code parameterTypes}, {@code commandFactory}. * @return hashCode value */ @Override public int hashCode() { @Var int h = 5381; h += (h << 5) + commandName.hashCode(); h += (h << 5) + parameterTypes.hashCode(); h += (h << 5) + commandFactory.hashCode(); return h; } /** * Prints the immutable value {@code CommandMeta} with attribute values. * @return A string representation of the value */ @Override public String toString() { return MoreObjects.toStringHelper("CommandMeta") .omitNullValues() .add("commandName", commandName) .add("parameterTypes", parameterTypes) .add("commandFactory", commandFactory) .toString(); } /** * Creates an immutable copy of a {@link BaseCommandMeta} value. * Uses accessors to get values to initialize the new immutable instance. * If an instance is already immutable, it is returned as is. * @param generic parameter T * @param instance The instance to copy * @return A copied immutable CommandMeta instance */ static > CommandMeta copyOf(BaseCommandMeta instance) { if (instance instanceof CommandMeta) { return (CommandMeta) instance; } return CommandMeta.builder() .from(instance) .build(); } /** * Creates a builder for {@link CommandMeta CommandMeta}. *

   * CommandMeta.&lt;T&gt;builder()
   *    .setCommandName(String) // required {@link CommandMeta#commandName() commandName}
   *    .putParameterTypes|putAllParameterTypes(String => net.yudichev.jiotty.appliance.CommandParamType) // {@link CommandMeta#parameterTypes() parameterTypes} mappings
   *    .setCommandFactory(function.Function&lt;Map&lt;String, Object&gt;, T&gt;) // required {@link CommandMeta#commandFactory() commandFactory}
   *    .build();
   * 
* @param generic parameter T * @return A new CommandMeta builder */ public static > CommandMeta.Builder builder() { return new CommandMeta.Builder<>(); } /** * Builds instances of type {@link CommandMeta CommandMeta}. * 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 = "BaseCommandMeta", generator = "Immutables") @NotThreadSafe public static final class Builder> { private static final long INIT_BIT_COMMAND_NAME = 0x1L; private static final long INIT_BIT_COMMAND_FACTORY = 0x2L; private static final long OPT_BIT_PARAMETER_TYPES = 0x1L; private long initBits = 0x3L; private long optBits; private @Nullable String commandName; private ImmutableMap.Builder parameterTypes = ImmutableMap.builder(); private @Nullable Function, T> commandFactory; private Builder() { } /** * Fill a builder with attribute values from the provided {@code CommandMeta} 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(CommandMeta instance) { return from((BaseCommandMeta) instance); } /** * Copy abstract value type {@code BaseCommandMeta} instance into builder. * @param instance The instance from which to copy values * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue final Builder from(BaseCommandMeta instance) { Objects.requireNonNull(instance, "instance"); setCommandName(instance.commandName()); putAllParameterTypes(instance.parameterTypes()); setCommandFactory(instance.commandFactory()); return this; } /** * Initializes the value for the {@link CommandMeta#commandName() commandName} attribute. * @param commandName The value for commandName * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final Builder setCommandName(String commandName) { this.commandName = Objects.requireNonNull(commandName, "commandName"); initBits &= ~INIT_BIT_COMMAND_NAME; return this; } /** * Put one entry to the {@link CommandMeta#parameterTypes() parameterTypes} map. * @param key The key in the parameterTypes map * @param value The associated value in the parameterTypes map * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final Builder putParameterTypes(String key, CommandParamType value) { this.parameterTypes.put(key, value); optBits |= OPT_BIT_PARAMETER_TYPES; return this; } /** * Put one entry to the {@link CommandMeta#parameterTypes() parameterTypes} map. Nulls are not permitted * @param entry The key and value entry * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final Builder putParameterTypes(Map.Entry entry) { this.parameterTypes.put(entry); optBits |= OPT_BIT_PARAMETER_TYPES; return this; } /** * Sets or replaces all mappings from the specified map as entries for the {@link CommandMeta#parameterTypes() parameterTypes} map. Nulls are not permitted * @param entries The entries that will be added to the parameterTypes map * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final Builder setParameterTypes(Map entries) { this.parameterTypes = ImmutableMap.builder(); optBits |= OPT_BIT_PARAMETER_TYPES; return putAllParameterTypes(entries); } /** * Put all mappings from the specified map as entries to {@link CommandMeta#parameterTypes() parameterTypes} map. Nulls are not permitted * @param entries The entries that will be added to the parameterTypes map * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final Builder putAllParameterTypes(Map entries) { this.parameterTypes.putAll(entries); optBits |= OPT_BIT_PARAMETER_TYPES; return this; } /** * Initializes the value for the {@link CommandMeta#commandFactory() commandFactory} attribute. * @param commandFactory The value for commandFactory * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final Builder setCommandFactory(Function, T> commandFactory) { this.commandFactory = Objects.requireNonNull(commandFactory, "commandFactory"); initBits &= ~INIT_BIT_COMMAND_FACTORY; return this; } /** * Builds a new {@link CommandMeta CommandMeta}. * @return An immutable instance of CommandMeta * @throws java.lang.IllegalStateException if any required attributes are missing */ public CommandMeta build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return new CommandMeta(this); } private boolean parameterTypesIsSet() { return (optBits & OPT_BIT_PARAMETER_TYPES) != 0; } private String formatRequiredAttributesMessage() { List attributes = new ArrayList<>(); if ((initBits & INIT_BIT_COMMAND_NAME) != 0) attributes.add("commandName"); if ((initBits & INIT_BIT_COMMAND_FACTORY) != 0) attributes.add("commandFactory"); return "Cannot build CommandMeta, some of required attributes are not set " + attributes; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy