Split holder implementation

This commit is contained in:
Nassim Jahnke 2024-09-04 11:51:43 +02:00
parent 22d396d909
commit f9ec6b9d03
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
3 changed files with 55 additions and 35 deletions

View File

@ -35,7 +35,7 @@ public interface Holder<T> {
* @throws IllegalArgumentException if the id is negative * @throws IllegalArgumentException if the id is negative
*/ */
static <T> Holder<T> of(final int id) { static <T> Holder<T> of(final int id) {
return new HolderImpl<>(id); return new IdHolder<>(id);
} }
/** /**
@ -46,7 +46,7 @@ public interface Holder<T> {
* @return a new direct holder * @return a new direct holder
*/ */
static <T> Holder<T> of(final T value) { static <T> Holder<T> of(final T value) {
return new HolderImpl<>(value); return new ValueHolder<>(value);
} }
/** /**

View File

@ -25,49 +25,29 @@ package com.viaversion.viaversion.api.minecraft;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import it.unimi.dsi.fastutil.ints.Int2IntFunction; import it.unimi.dsi.fastutil.ints.Int2IntFunction;
final class HolderImpl<T> implements Holder<T> { record IdHolder<T>(int id) implements Holder<T> {
private final T value; IdHolder {
private final int id;
HolderImpl(final int id) {
Preconditions.checkArgument(id >= 0, "id cannot be negative"); 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 @Override
public boolean isDirect() { public boolean isDirect() {
return id == -1; return false;
} }
@Override @Override
public boolean hasId() { public boolean hasId() {
return id != -1; return true;
} }
@Override @Override
public T value() { public T value() {
Preconditions.checkArgument(isDirect(), "Holder is not direct"); throw new IllegalArgumentException("Holder is not direct");
return value;
}
@Override
public int id() {
return id;
} }
@Override @Override
public Holder<T> updateId(final Int2IntFunction rewriteFunction) { public Holder<T> updateId(final Int2IntFunction rewriteFunction) {
if (isDirect()) {
return this;
}
final int rewrittenId = rewriteFunction.applyAsInt(id); final int rewrittenId = rewriteFunction.applyAsInt(id);
if (rewrittenId == id) { if (rewrittenId == id) {
return this; return this;
@ -77,12 +57,4 @@ final class HolderImpl<T> implements Holder<T> {
} }
return Holder.of(rewrittenId); return Holder.of(rewrittenId);
} }
@Override
public String toString() {
return "HolderImpl{" +
"value=" + value +
", id=" + id +
'}';
}
} }

View File

@ -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>(T value) implements Holder<T> {
@Override
public boolean isDirect() {
return true;
}
@Override
public boolean hasId() {
return false;
}
@Override
public int id() {
return -1;
}
@Override
public Holder<T> updateId(final Int2IntFunction rewriteFunction) {
return this;
}
}