feat(core): add Context generic
All checks were successful
Build / Gradle-Build (push) Successful in 15s
Publish / Gradle-Publish (push) Successful in 13s
Test / Gradle-Test (push) Successful in 13s

This commit is contained in:
Tueem 2025-04-11 20:44:03 +02:00
parent c44b874b41
commit 2a6eba871d
Signed by: tueem
GPG Key ID: F2CE0513D231AD7A
13 changed files with 77 additions and 77 deletions

View File

@ -1,7 +1,7 @@
package net.tomatentum.cutin;
public interface MethodExecutor {
public interface MethodExecutor<C extends Object> {
void handle(Object context);
void handle(C context);
}

View File

@ -2,8 +2,8 @@ package net.tomatentum.cutin;
import net.tomatentum.cutin.container.MethodContainer;
public interface MethodProcessor<I extends Object> {
public interface MethodProcessor<I extends Object, C extends Object> {
void process(Object context, MethodContainer<I> methodContainer);
void process(Object context, MethodContainer<I, C> methodContainer);
}

View File

@ -2,10 +2,10 @@ package net.tomatentum.cutin;
import java.util.Collection;
public interface ProcessorContainer<I> {
public interface ProcessorContainer<I extends Object, C extends Object> {
ProcessorContainer<I> addProcessor(MethodProcessor<I> processor);
ProcessorContainer<I, C> addProcessor(MethodProcessor<I, C> processor);
Collection<MethodProcessor<I>> processor();
Collection<MethodProcessor<I, C>> processor();
}

View File

@ -9,26 +9,26 @@ import org.slf4j.LoggerFactory;
import net.tomatentum.cutin.container.MethodContainer;
public class ProcessorMethodExecutor<I extends Object> implements MethodExecutor, ProcessorContainer<I> {
public class ProcessorMethodExecutor<I extends Object, C extends Object> implements MethodExecutor<C>, ProcessorContainer<I, C> {
private Logger logger = LoggerFactory.getLogger(getClass());
private MethodContainer<I> methodContainer;
private Set<MethodProcessor<I>> processors;
private MethodContainer<I, C> methodContainer;
private Set<MethodProcessor<I, C>> processors;
public ProcessorMethodExecutor(MethodContainer<I> methodContainer) {
public ProcessorMethodExecutor(MethodContainer<I, C> methodContainer) {
this.methodContainer = methodContainer;
this.processors = new HashSet<>();
}
@Override
public ProcessorContainer<I> addProcessor(MethodProcessor<I> processor) {
public ProcessorContainer<I, C> addProcessor(MethodProcessor<I, C> processor) {
processors.add(processor);
return this;
}
@Override
public Collection<MethodProcessor<I>> processor() {
public Collection<MethodProcessor<I, C>> processor() {
return this.processors;
}

View File

@ -7,14 +7,14 @@ import java.util.Optional;
import net.tomatentum.cutin.method.ReflectedMethod;
public interface ReflectedMethodFactory<I extends Object> {
Optional<ReflectedMethod<I>> produce(Method method, Object containingClass);
ReflectedMethodFactory<I> addFactory(Factory<I> factory);
public interface ReflectedMethodFactory<I extends Object, C extends Object> {
Optional<ReflectedMethod<I, C>> produce(Method method, Object containingClass);
ReflectedMethodFactory<I, C> addFactory(Factory<I, C> factory);
public interface Factory<I extends Object> {
public interface Factory<I extends Object, C extends Object> {
Optional<ReflectedMethod<I>> produce(Method method, Object containingObject);
void addParser(ReflectedMethod<I> method, List<MethodParser> parser);
Optional<ReflectedMethod<I, C>> produce(Method method, Object containingObject);
void addParser(ReflectedMethod<I, C> method, List<MethodParser> parser);
}
}

View File

@ -12,23 +12,23 @@ import net.tomatentum.cutin.method.ReflectedMethod;
import net.tomatentum.cutin.util.ReflectionUtil;
public class ReflectedMethodFactoryImpl<I extends Object> implements ReflectedMethodFactory<I> {
public class ReflectedMethodFactoryImpl<I extends Object, C extends Object> implements ReflectedMethodFactory<I, C> {
private Logger logger = LoggerFactory.getLogger(getClass());
private List<Factory<I>> factories;
private List<Factory<I, C>> factories;
public ReflectedMethodFactoryImpl() {
this(new ArrayList<>());
}
public ReflectedMethodFactoryImpl(List<Factory<I>> factories) {
public ReflectedMethodFactoryImpl(List<Factory<I, C>> factories) {
this.factories = factories;
}
@Override
public Optional<ReflectedMethod<I>> produce(Method method, Object containingClass) {
Optional<ReflectedMethod<I>> rmethod = this.factories.stream()
public Optional<ReflectedMethod<I, C>> produce(Method method, Object containingClass) {
Optional<ReflectedMethod<I, C>> rmethod = this.factories.stream()
.map(f -> factoryProduce(f, method, containingClass))
.filter(Optional::isPresent)
.map(Optional::get)
@ -42,14 +42,14 @@ public class ReflectedMethodFactoryImpl<I extends Object> implements ReflectedMe
}
@Override
public ReflectedMethodFactory<I> addFactory(Factory<I> factory) {
public ReflectedMethodFactory<I, C> addFactory(Factory<I, C> factory) {
this.factories.add(factory);
return this;
}
private Optional<ReflectedMethod<I>> factoryProduce(Factory<I> factory, Method method, Object containingClass) {
private Optional<ReflectedMethod<I, C>> factoryProduce(Factory<I, C> factory, Method method, Object containingClass) {
List<MethodParser> parser = new ArrayList<>();
Optional<ReflectedMethod<I>> m = factory.produce(method, containingClass);
Optional<ReflectedMethod<I, C>> m = factory.produce(method, containingClass);
m.ifPresent(x -> {
factory.addParser(x, parser);
parser.forEach(MethodParser::parse);

View File

@ -11,24 +11,24 @@ import java.util.Set;
import net.tomatentum.cutin.ReflectedMethodFactory;
import net.tomatentum.cutin.method.ReflectedMethod;
public class LoneMethodContainer<I extends Object> implements MethodContainer<I> {
public class LoneMethodContainer<I extends Object, C extends Object> implements MethodContainer<I, C> {
private Map<I, ReflectedMethod<I>> methodStore;
private ReflectedMethodFactory<I> factory;
private Map<I, ReflectedMethod<I, C>> methodStore;
private ReflectedMethodFactory<I, C> factory;
public LoneMethodContainer(ReflectedMethodFactory<I> factory) {
public LoneMethodContainer(ReflectedMethodFactory<I, C> factory) {
this.methodStore = new HashMap<>();
this.factory = factory;
}
@Override
public MethodContainer<I> addMethod(ReflectedMethod<I> method) {
public MethodContainer<I, C> addMethod(ReflectedMethod<I, C> method) {
this.methodStore.put(method.identifier(), method);
return this;
}
@Override
public MethodContainer<I> addMethods(Object containingObject, Method... methods) {
public MethodContainer<I, C> addMethods(Object containingObject, Method... methods) {
for (Method method : methods)
this.factory.produce(method, containingObject)
.ifPresent(this::addMethod);
@ -41,17 +41,17 @@ public class LoneMethodContainer<I extends Object> implements MethodContainer<I>
}
@Override
public Collection<ReflectedMethod<I>> methods() {
public Collection<ReflectedMethod<I, C>> methods() {
return this.methodStore.values();
}
@Override
public Collection<ReflectedMethod<I>> findFor(I identifier) {
public Collection<ReflectedMethod<I, C>> findFor(I identifier) {
return Arrays.asList(this.methodStore.get(identifier));
}
@Override
public Optional<ReflectedMethod<I>> findFirstFor(I identifier) {
public Optional<ReflectedMethod<I, C>> findFirstFor(I identifier) {
return findFor(identifier).stream().findFirst();
}

View File

@ -7,24 +7,24 @@ import java.util.Set;
import net.tomatentum.cutin.method.ReflectedMethod;
public interface MethodContainer<I extends Object> {
public interface MethodContainer<I extends Object, C extends Object> {
MethodContainer<I> addMethod(ReflectedMethod<I> method);
default MethodContainer<I> addMethods(ReflectedMethod<I>[] methods) {
for (ReflectedMethod<I> reflectedMethod : methods) {
MethodContainer<I, C> addMethod(ReflectedMethod<I, C> method);
default MethodContainer<I, C> addMethods(ReflectedMethod<I, C>[] methods) {
for (ReflectedMethod<I, C> reflectedMethod : methods) {
this.addMethod(reflectedMethod);
}
return this;
}
MethodContainer<I> addMethods(Object containingObject, Method... methods);
default MethodContainer<I> addAllMethods(Object containingObject) {
MethodContainer<I, C> addMethods(Object containingObject, Method... methods);
default MethodContainer<I, C> addAllMethods(Object containingObject) {
this.addMethods(containingObject, containingObject.getClass().getDeclaredMethods());
return this;
}
Set<I> identifiers();
Collection<ReflectedMethod<I>> methods();
Collection<ReflectedMethod<I>> findFor(I identifier);
Optional<ReflectedMethod<I>> findFirstFor(I identifier);
Collection<ReflectedMethod<I, C>> methods();
Collection<ReflectedMethod<I, C>> findFor(I identifier);
Optional<ReflectedMethod<I, C>> findFirstFor(I identifier);
}

View File

@ -14,28 +14,28 @@ import net.tomatentum.cutin.ReflectedMethodFactory;
import net.tomatentum.cutin.method.ReflectedMethod;
import net.tomatentum.cutin.util.ReflectionUtil;
public class MultiMethodContainer<I extends Object> implements MethodContainer<I> {
public class MultiMethodContainer<I extends Object, C extends Object> implements MethodContainer<I, C> {
private Set<Entry<I>> entries;
private ReflectedMethodFactory<I> factory;
private Set<Entry<I, C>> entries;
private ReflectedMethodFactory<I, C> factory;
public MultiMethodContainer(ReflectedMethodFactory<I> factory) {
public MultiMethodContainer(ReflectedMethodFactory<I, C> factory) {
this.entries = new HashSet<>();
this.factory = factory;
}
@Override
public MethodContainer<I> addMethod(ReflectedMethod<I> method) {
Optional<Entry<I>> oentry = this.entries.stream()
public MethodContainer<I, C> addMethod(ReflectedMethod<I, C> method) {
Optional<Entry<I, C>> oentry = this.entries.stream()
.filter(e -> method.identifier().equals(e.identifier()))
.findFirst();
Entry<I> entry = oentry.orElse(new Entry<>(method.identifier())).addMethod(method);
Entry<I, C> entry = oentry.orElse(new Entry<>(method.identifier())).addMethod(method);
if (oentry.isEmpty()) this.entries.add(entry);
return this;
}
@Override
public MethodContainer<I> addMethods(Object containingObject, Method... methods) {
public MethodContainer<I, C> addMethods(Object containingObject, Method... methods) {
for (Method method : methods)
this.factory.produce(method, containingObject)
.ifPresent(this::addMethod);
@ -50,14 +50,14 @@ public class MultiMethodContainer<I extends Object> implements MethodContainer<I
}
@Override
public Collection<ReflectedMethod<I>> methods() {
public Collection<ReflectedMethod<I, C>> methods() {
return this.entries.stream()
.flatMap(e -> e.methods.stream())
.toList();
}
@Override
public Collection<ReflectedMethod<I>> findFor(I identifier) {
public Collection<ReflectedMethod<I, C>> findFor(I identifier) {
return this.entries.stream()
.filter(e -> e.identifier().equals(identifier))
.flatMap(e -> e.methods.stream())
@ -65,18 +65,18 @@ public class MultiMethodContainer<I extends Object> implements MethodContainer<I
}
@Override
public Optional<ReflectedMethod<I>> findFirstFor(I identifier) {
public Optional<ReflectedMethod<I, C>> findFirstFor(I identifier) {
return this.entries.stream()
.filter(e -> e.identifier().equals(identifier))
.flatMap(e -> e.methods.stream())
.findFirst();
}
protected Set<Entry<I>> entries() {
protected Set<Entry<I, C>> entries() {
return this.entries;
}
public static record Entry<I extends Object>(I identifier, Set<ReflectedMethod<I>> methods) {
public static record Entry<I extends Object, C extends Object>(I identifier, Set<ReflectedMethod<I, C>> methods) {
public Entry(I identifier) {
this(identifier, new HashSet<>());
@ -84,7 +84,7 @@ public class MultiMethodContainer<I extends Object> implements MethodContainer<I
private static Logger logger = LoggerFactory.getLogger(Entry.class);
public Entry<I> addMethod(ReflectedMethod<I> method) {
public Entry<I, C> addMethod(ReflectedMethod<I, C> method) {
I midentifier = method.identifier();
if (!this.identifier().equals(midentifier))
@ -95,7 +95,7 @@ public class MultiMethodContainer<I extends Object> implements MethodContainer<I
return this;
}
public Object[] runAll(Object context) {
public Object[] runAll(C context) {
logger.trace("Running all Methods from {} with context {}", this, context);
return this.methods.stream()
.map(x -> {
@ -110,7 +110,7 @@ public class MultiMethodContainer<I extends Object> implements MethodContainer<I
public boolean equals(Object obj) {
if (!(obj instanceof Entry))
return false;
Entry<?> other = (Entry<?>) obj;
Entry<?, ?> other = (Entry<?, ?>) obj;
return other.identifier().equals(identifier());
}

View File

@ -11,7 +11,7 @@ import net.tomatentum.cutin.ReflectedMethodFactory;
import net.tomatentum.cutin.container.MethodContainer;
import net.tomatentum.cutin.util.ReflectionUtil;
public class BestCandidateMethod<I> extends ReflectedMethod<I> {
public class BestCandidateMethod<I extends Object, C extends Object> extends ReflectedMethod<I, C> {
private String methodName;
private I identifier;
@ -42,7 +42,7 @@ public class BestCandidateMethod<I> extends ReflectedMethod<I> {
}
@Override
public Object run(Object context) {
public Object run(C context) {
Method[] methods = Arrays.stream(containingObject.getClass().getDeclaredMethods())
.filter(x -> x.getName().equals(methodName))
.filter(x -> !x.isBridge())
@ -63,7 +63,7 @@ public class BestCandidateMethod<I> extends ReflectedMethod<I> {
@Override
public boolean equals(Object obj) {
if (obj instanceof BestCandidateMethod<?> bcMethod) {
if (obj instanceof BestCandidateMethod<?, ?> bcMethod) {
return this.containingObject().getClass().equals(bcMethod.containingObject().getClass()) &&
this.methodName.equals(bcMethod.methodName);
}
@ -75,21 +75,21 @@ public class BestCandidateMethod<I> extends ReflectedMethod<I> {
return Objects.hash(containingObject.getClass(), methodName);
}
public abstract static class Factory<I extends Object> implements ReflectedMethodFactory.Factory<I> {
public abstract static class Factory<I extends Object, C extends Object> implements ReflectedMethodFactory.Factory<I, C> {
private MethodContainer<I> methodContainer;
private MethodContainer<I, C> methodContainer;
private String methodName;
private Object[] additionalParameters;
protected Factory(MethodContainer<I> methodContainer, String methodName, Object... additionalParameters) {
protected Factory(MethodContainer<I, C> 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<>(
public Optional<ReflectedMethod<I, C>> produce(Method method, Object containingObject) {
BestCandidateMethod<I, C> bcMethod = new BestCandidateMethod<>(
methodName, containingObject, additionalParameters);
if (methodContainer.methods().contains(bcMethod))
return Optional.empty();

View File

@ -12,7 +12,7 @@ import org.slf4j.LoggerFactory;
import net.tomatentum.cutin.util.ReflectionUtil;
public abstract class ReflectedMethod<I extends Object> {
public abstract class ReflectedMethod<I extends Object, C extends Object> {
private Logger logger = LoggerFactory.getLogger(getClass());
@ -26,11 +26,11 @@ public abstract class ReflectedMethod<I extends Object> {
this.containingObject = containingObject;
}
public abstract Object getParameter(Object context, int index);
public abstract Object getParameter(C context, int index);
public abstract I identifier();
public Object run(Object context) {
public Object run(C context) {
method.setAccessible(true);
try {
return method.invoke(containingObject, getParameters(context));
@ -48,7 +48,7 @@ public abstract class ReflectedMethod<I extends Object> {
return this.containingObject;
}
private Object[] getParameters(Object context) {
private Object[] getParameters(C context) {
int parameterCount = method.getParameterCount();
List<Object> parameters = new ArrayList<>();

View File

@ -11,14 +11,14 @@ class ReflectedMethodTest {
@Test
void methodTest() {
ReflectedMethod<String> method = new TestReflectedMethod(new TestMethodClass());
ReflectedMethod<String, String> method = new TestReflectedMethod(new TestMethodClass());
Object result = method.run("testContext");
assertTrue((boolean)result);
}
@Test
void testBCMethod() {
ReflectedMethod<?> method = new BestCandidateMethod<String>(
ReflectedMethod<String, Double> method = new BestCandidateMethod<String, Double>(
"test",
new TestMethodClass(),
"ident",

View File

@ -4,7 +4,7 @@ import java.lang.reflect.Method;
import net.tomatentum.cutin.method.ReflectedMethod;
public class TestReflectedMethod extends ReflectedMethod<String> {
public class TestReflectedMethod extends ReflectedMethod<String, String> {
protected TestReflectedMethod(Object containingObject) {
super(getMethod(containingObject), containingObject);
@ -12,7 +12,7 @@ public class TestReflectedMethod extends ReflectedMethod<String> {
}
@Override
public Object getParameter(Object context, int index) {
public Object getParameter(String context, int index) {
return 2;
}