org.incendo.cloud.minecraft.extras.caption.RichVariableImpl Maven / Gradle / Ivy
//
// MIT License
//
// Copyright (c) 2024 Incendo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package org.incendo.cloud.minecraft.extras.caption;
import java.util.Objects;
import org.apiguardian.api.API;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.immutables.value.Generated;
/**
* Immutable implementation of {@link RichVariable}.
*
* Use the static factory method to create immutable instances:
* {@code RichVariableImpl.of()}.
*/
@Generated(from = "RichVariable", generator = "Immutables")
@SuppressWarnings({"all"})
@javax.annotation.Generated("org.immutables.processor.ProxyProcessor")
@API(status = API.Status.INTERNAL, consumers = "org.incendo.cloud.*")
final class RichVariableImpl implements RichVariable {
private final java.lang.@NonNull String key;
private final net.kyori.adventure.text.@NonNull Component component;
private RichVariableImpl(
java.lang.@NonNull String key,
net.kyori.adventure.text.@NonNull Component component) {
this.key = Objects.requireNonNull(key, "key");
this.component = Objects.requireNonNull(component, "component");
}
private RichVariableImpl(
RichVariableImpl original,
java.lang.@NonNull String key,
net.kyori.adventure.text.@NonNull Component component) {
this.key = key;
this.component = component;
}
/**
* @return The value of the {@code key} attribute
*/
@Override
public java.lang.@NonNull String key() {
return key;
}
/**
* @return The value of the {@code component} attribute
*/
@Override
public net.kyori.adventure.text.@NonNull Component component() {
return component;
}
/**
* Copy the current immutable object by setting a value for the {@link RichVariable#key() key} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for key
* @return A modified copy of the {@code this} object
*/
public final RichVariableImpl withKey(java.lang.@NonNull String value) {
java.lang.@NonNull String newValue = Objects.requireNonNull(value, "key");
if (this.key.equals(newValue)) return this;
return new RichVariableImpl(this, newValue, this.component);
}
/**
* Copy the current immutable object by setting a value for the {@link RichVariable#component() component} 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 component
* @return A modified copy of the {@code this} object
*/
public final RichVariableImpl withComponent(net.kyori.adventure.text.@NonNull Component value) {
if (this.component == value) return this;
net.kyori.adventure.text.@NonNull Component newValue = Objects.requireNonNull(value, "component");
return new RichVariableImpl(this, this.key, newValue);
}
/**
* This instance is equal to all instances of {@code RichVariableImpl} 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 RichVariableImpl
&& equalTo(0, (RichVariableImpl) another);
}
private boolean equalTo(int synthetic, RichVariableImpl another) {
return key.equals(another.key)
&& component.equals(another.component);
}
/**
* Computes a hash code from attributes: {@code key}, {@code component}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + key.hashCode();
h += (h << 5) + component.hashCode();
return h;
}
/**
* Prints the immutable value {@code RichVariable} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "RichVariable{"
+ "key=" + key
+ ", component=" + component
+ "}";
}
/**
* Construct a new immutable {@code RichVariable} instance.
* @param key The value for the {@code key} attribute
* @param component The value for the {@code component} attribute
* @return An immutable RichVariable instance
*/
public static RichVariableImpl of(java.lang.@NonNull String key, net.kyori.adventure.text.@NonNull Component component) {
return new RichVariableImpl(key, component);
}
/**
* Creates an immutable copy of a {@link RichVariable} 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 RichVariable instance
*/
public static RichVariableImpl copyOf(RichVariable instance) {
if (instance instanceof RichVariableImpl) {
return (RichVariableImpl) instance;
}
return RichVariableImpl.of(instance.key(), instance.component());
}
}