diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/Holder.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/Holder.java index d597ba9ca..b056fa2ed 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/Holder.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/Holder.java @@ -35,7 +35,7 @@ public interface Holder { * @throws IllegalArgumentException if the id is negative */ static Holder of(final int id) { - return new HolderImpl<>(id); + return new IdHolder<>(id); } /** @@ -46,7 +46,7 @@ public interface Holder { * @return a new direct holder */ static Holder of(final T value) { - return new HolderImpl<>(value); + return new ValueHolder<>(value); } /** diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/HolderImpl.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/IdHolder.java similarity index 73% rename from api/src/main/java/com/viaversion/viaversion/api/minecraft/HolderImpl.java rename to api/src/main/java/com/viaversion/viaversion/api/minecraft/IdHolder.java index bcbaaf390..1386d50b3 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/HolderImpl.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/IdHolder.java @@ -25,49 +25,29 @@ package com.viaversion.viaversion.api.minecraft; import com.google.common.base.Preconditions; import it.unimi.dsi.fastutil.ints.Int2IntFunction; -final class HolderImpl implements Holder { +record IdHolder(int id) implements Holder { - private final T value; - private final int id; - - HolderImpl(final int id) { + IdHolder { Preconditions.checkArgument(id >= 0, "id cannot be negative"); - this.value = null; - this.id = id; - } - - HolderImpl(final T value) { - this.value = value; - this.id = -1; } @Override public boolean isDirect() { - return id == -1; + return false; } @Override public boolean hasId() { - return id != -1; + return true; } @Override public T value() { - Preconditions.checkArgument(isDirect(), "Holder is not direct"); - return value; - } - - @Override - public int id() { - return id; + throw new IllegalArgumentException("Holder is not direct"); } @Override public Holder updateId(final Int2IntFunction rewriteFunction) { - if (isDirect()) { - return this; - } - final int rewrittenId = rewriteFunction.applyAsInt(id); if (rewrittenId == id) { return this; @@ -77,12 +57,4 @@ final class HolderImpl implements Holder { } return Holder.of(rewrittenId); } - - @Override - public String toString() { - return "HolderImpl{" + - "value=" + value + - ", id=" + id + - '}'; - } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/ValueHolder.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/ValueHolder.java new file mode 100644 index 000000000..c34b07cd0 --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/ValueHolder.java @@ -0,0 +1,48 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * 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 com.viaversion.viaversion.api.minecraft; + +import it.unimi.dsi.fastutil.ints.Int2IntFunction; + +record ValueHolder(T value) implements Holder { + + @Override + public boolean isDirect() { + return true; + } + + @Override + public boolean hasId() { + return false; + } + + @Override + public int id() { + return -1; + } + + @Override + public Holder updateId(final Int2IntFunction rewriteFunction) { + return this; + } +}