mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-26 17:06:54 +00:00
Convert NamedMethod into a record
This commit is contained in:
parent
50d460624f
commit
4a5e03c11a
@ -19,8 +19,8 @@ final class SaturatedMethod {
|
||||
|
||||
SaturatedMethod(Object target, NamedMethod<PeripheralMethod> method) {
|
||||
this.target = target;
|
||||
name = method.getName();
|
||||
this.method = method.getMethod();
|
||||
name = method.name();
|
||||
this.method = method.method();
|
||||
}
|
||||
|
||||
MethodResult apply(ILuaContext context, IComputerAccess computer, IArguments args) throws LuaException {
|
||||
|
@ -328,7 +328,7 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
|
||||
methodMap.put(dynamicMethods[i], PeripheralMethod.DYNAMIC.get(i));
|
||||
}
|
||||
for (var method : methods) {
|
||||
methodMap.put(method.getName(), method.getMethod());
|
||||
methodMap.put(method.name(), method.method());
|
||||
}
|
||||
return methodMap;
|
||||
}
|
||||
|
@ -4,38 +4,23 @@
|
||||
|
||||
package dan200.computercraft.core.asm;
|
||||
|
||||
import dan200.computercraft.api.lua.LuaFunction;
|
||||
import dan200.computercraft.api.lua.MethodResult;
|
||||
import dan200.computercraft.api.peripheral.GenericPeripheral;
|
||||
import dan200.computercraft.api.peripheral.PeripheralType;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public final class NamedMethod<T> {
|
||||
private final String name;
|
||||
private final T method;
|
||||
private final boolean nonYielding;
|
||||
|
||||
private final @Nullable PeripheralType genericType;
|
||||
|
||||
NamedMethod(String name, T method, boolean nonYielding, @Nullable PeripheralType genericType) {
|
||||
this.name = name;
|
||||
this.method = method;
|
||||
this.nonYielding = nonYielding;
|
||||
this.genericType = genericType;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public T getMethod() {
|
||||
return method;
|
||||
}
|
||||
|
||||
public boolean nonYielding() {
|
||||
return nonYielding;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public PeripheralType getGenericType() {
|
||||
return genericType;
|
||||
}
|
||||
/**
|
||||
* A method generated from a {@link LuaFunction}.
|
||||
*
|
||||
* @param name The name of this method.
|
||||
* @param method The underlying method implementation.
|
||||
* @param nonYielding If this method is guaranteed to never yield, and will always return a
|
||||
* {@linkplain MethodResult#of(Object...) basic result}.
|
||||
* @param genericType The peripheral type of this method. This is only set if this is a method on a
|
||||
* {@link GenericPeripheral}.
|
||||
* @param <T> The type of method, either a {@link LuaMethod} or {@link PeripheralMethod}.
|
||||
*/
|
||||
public record NamedMethod<T>(String name, T method, boolean nonYielding, @Nullable PeripheralType genericType) {
|
||||
}
|
||||
|
@ -172,9 +172,9 @@ public class CobaltLuaMachine implements ILuaMachine {
|
||||
}
|
||||
|
||||
ObjectSource.allMethods(LuaMethod.GENERATOR, object, (instance, method) ->
|
||||
table.rawset(method.getName(), method.nonYielding()
|
||||
? new BasicFunction(this, method.getMethod(), instance, context, method.getName())
|
||||
: new ResultInterpreterFunction(this, method.getMethod(), instance, context, method.getName())));
|
||||
table.rawset(method.name(), method.nonYielding()
|
||||
? new BasicFunction(this, method.method(), instance, context, method.name())
|
||||
: new ResultInterpreterFunction(this, method.method(), instance, context, method.name())));
|
||||
|
||||
try {
|
||||
if (table.next(Constants.NIL).first().isNil()) return null;
|
||||
|
@ -28,7 +28,7 @@ public class ObjectWrapper implements ILuaContext {
|
||||
methodMap.put(dynamicMethods[i], LuaMethod.DYNAMIC.get(i));
|
||||
}
|
||||
for (var method : methods) {
|
||||
methodMap.put(method.getName(), method.getMethod());
|
||||
methodMap.put(method.name(), method.method());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -44,7 +44,7 @@ public class GeneratorTest {
|
||||
var methods = LuaMethod.GENERATOR.getMethods(Basic.class);
|
||||
var methods2 = LuaMethod.GENERATOR.getMethods(Basic2.class);
|
||||
assertThat(methods, contains(named("go")));
|
||||
assertThat(methods.get(0).getMethod(), sameInstance(methods2.get(0).getMethod()));
|
||||
assertThat(methods.get(0).method(), sameInstance(methods2.get(0).method()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -217,8 +217,8 @@ public class GeneratorTest {
|
||||
|
||||
private static <T> T find(Collection<NamedMethod<T>> methods, String name) {
|
||||
return methods.stream()
|
||||
.filter(x -> x.getName().equals(name))
|
||||
.map(NamedMethod::getMethod)
|
||||
.filter(x -> x.name().equals(name))
|
||||
.map(NamedMethod::method)
|
||||
.findAny()
|
||||
.orElseThrow(NullPointerException::new);
|
||||
}
|
||||
@ -235,7 +235,7 @@ public class GeneratorTest {
|
||||
}
|
||||
|
||||
public static <T> Matcher<NamedMethod<T>> named(String method) {
|
||||
return contramap(is(method), "name", NamedMethod::getName);
|
||||
return contramap(is(method), "name", NamedMethod::name);
|
||||
}
|
||||
|
||||
private static final ILuaContext CONTEXT = new ILuaContext() {
|
||||
|
@ -71,7 +71,7 @@ public class GenericPeripheralProvider {
|
||||
|
||||
// If we have a peripheral type, use it. Always pick the smallest one, so it's consistent (assuming mods
|
||||
// don't change).
|
||||
var type = method.getGenericType();
|
||||
var type = method.genericType();
|
||||
if (type != null && type.getPrimaryType() != null) {
|
||||
var name = type.getPrimaryType();
|
||||
if (this.name == null || this.name.compareTo(name) > 0) this.name = name;
|
||||
|
@ -71,7 +71,7 @@ public class GenericPeripheralProvider {
|
||||
|
||||
// If we have a peripheral type, use it. Always pick the smallest one, so it's consistent (assuming mods
|
||||
// don't change).
|
||||
var type = method.getGenericType();
|
||||
var type = method.genericType();
|
||||
if (type != null && type.getPrimaryType() != null) {
|
||||
var name = type.getPrimaryType();
|
||||
if (this.name == null || this.name.compareTo(name) > 0) this.name = name;
|
||||
|
Loading…
Reference in New Issue
Block a user