feat(method): add Factory to BestCandidateMethod
This commit is contained in:
parent
8842e9d8e1
commit
c44b874b41
@ -3,8 +3,12 @@ package net.tomatentum.cutin.method;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import net.tomatentum.cutin.ReflectedMethodFactory;
|
||||
import net.tomatentum.cutin.container.MethodContainer;
|
||||
import net.tomatentum.cutin.util.ReflectionUtil;
|
||||
|
||||
public class BestCandidateMethod<I> extends ReflectedMethod<I> {
|
||||
@ -20,6 +24,13 @@ public class BestCandidateMethod<I> extends ReflectedMethod<I> {
|
||||
this.additionalParameters = Arrays.asList(additionalParameters);
|
||||
}
|
||||
|
||||
public BestCandidateMethod(String methodName, Object containingObject, Object... additionalParameters) {
|
||||
super(getMethod(containingObject, methodName), containingObject);
|
||||
this.methodName = methodName;
|
||||
this.identifier = null;
|
||||
this.additionalParameters = Arrays.asList(additionalParameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getParameter(Object context, int index) {
|
||||
return additionalParameters.get(index);
|
||||
@ -50,4 +61,41 @@ public class BestCandidateMethod<I> extends ReflectedMethod<I> {
|
||||
.findFirst().orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof BestCandidateMethod<?> bcMethod) {
|
||||
return this.containingObject().getClass().equals(bcMethod.containingObject().getClass()) &&
|
||||
this.methodName.equals(bcMethod.methodName);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(containingObject.getClass(), methodName);
|
||||
}
|
||||
|
||||
public abstract static class Factory<I extends Object> implements ReflectedMethodFactory.Factory<I> {
|
||||
|
||||
private MethodContainer<I> methodContainer;
|
||||
private String methodName;
|
||||
private Object[] additionalParameters;
|
||||
|
||||
protected Factory(MethodContainer<I> methodContainer, String methodName, Object... additionalParameters) {
|
||||
this.methodContainer = methodContainer;
|
||||
this.methodName = methodName;
|
||||
this.additionalParameters = additionalParameters;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ReflectedMethod<I>> produce(Method method, Object containingObject) {
|
||||
BestCandidateMethod<I> bcMethod = new BestCandidateMethod<>(
|
||||
methodName, containingObject, additionalParameters);
|
||||
if (methodContainer.methods().contains(bcMethod))
|
||||
return Optional.empty();
|
||||
return Optional.of(bcMethod);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user