refactor(bcmethod): move duplicate checking out of factory

This commit is contained in:
tueem 2025-05-02 12:57:02 +02:00
parent 4dd7b70f40
commit ad2c4c00f6
Signed by: tueem
GPG Key ID: 65C8667EC17A88FB
2 changed files with 4 additions and 26 deletions

View File

@ -29,6 +29,10 @@ public class LoneMethodContainer<I extends Object, C extends Object> implements
@Override @Override
public MethodContainer<I, C> addMethod(ReflectedMethod<I, C> method) { public MethodContainer<I, C> addMethod(ReflectedMethod<I, C> method) {
if (this.methodStore.keySet().contains(method.identifier())) {
logger.warn("Could not add {} to container because the same identifier was already present.", method);
return this;
}
this.methodStore.put(method.identifier(), method); this.methodStore.put(method.identifier(), method);
logger.debug("Added {} to container", method); logger.debug("Added {} to container", method);
return this; return this;

View File

@ -5,14 +5,10 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import net.tomatentum.cutin.container.MethodContainer;
import net.tomatentum.cutin.factory.ReflectedMethodFactory;
import net.tomatentum.cutin.factory.ReflectedMethodFactory.ParserResults;
import net.tomatentum.cutin.util.ReflectionUtil; import net.tomatentum.cutin.util.ReflectionUtil;
public abstract class BestCandidateMethod<I extends Object, C extends Object> extends ReflectedMethod<I, C> { public abstract class BestCandidateMethod<I extends Object, C extends Object> extends ReflectedMethod<I, C> {
@ -71,26 +67,4 @@ public abstract class BestCandidateMethod<I extends Object, C extends Object> ex
return parameters; return parameters;
} }
public abstract static class Factory<I extends Object, C extends Object> implements ReflectedMethodFactory.Factory<I, C> {
private MethodContainer<I, C> methodContainer;
private String methodName;
protected Factory(MethodContainer<I, C> methodContainer, String methodName) {
this.methodContainer = methodContainer;
this.methodName = methodName;
}
@Override
public Optional<ReflectedMethod<I, C>> produce(Method method, Object containingObject, ParserResults results) {
Optional<BestCandidateMethod<I, C>> bcMethod = bcProduce(methodName, containingObject, results);
if (bcMethod.isEmpty() || methodContainer.methods().contains(bcMethod.get()))
return Optional.empty();
return Optional.of(bcMethod.get());
}
protected abstract Optional<BestCandidateMethod<I, C>> bcProduce(String methodName, Object containingObject, ParserResults results);
}
} }