feat(container): add MethodContainers
This commit is contained in:
parent
075f0e13c9
commit
52a05dd9d7
@ -0,0 +1,50 @@
|
|||||||
|
package net.tomatentum.cutin.container;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.ReflectedMethod;
|
||||||
|
import net.tomatentum.cutin.ReflectedMethodFactory;
|
||||||
|
|
||||||
|
public class LoneMethodContainer<I extends Object> implements MethodContainer<I> {
|
||||||
|
|
||||||
|
private Map<I, ReflectedMethod<I>> methodStore;
|
||||||
|
private ReflectedMethodFactory factory;
|
||||||
|
|
||||||
|
public LoneMethodContainer(ReflectedMethodFactory factory) {
|
||||||
|
this.methodStore = new HashMap<>();
|
||||||
|
this.factory = factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addMethods(ReflectedMethod<I>... methods) {
|
||||||
|
for (ReflectedMethod<I> reflectedMethod : methods)
|
||||||
|
this.methodStore.put(reflectedMethod.identifier(), reflectedMethod);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addMethods(Object containingObject, Method... methods) {
|
||||||
|
for (Method method : methods)
|
||||||
|
this.addMethods(this.factory.produce(method, containingObject));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<ReflectedMethod<I>> methods() {
|
||||||
|
return this.methodStore.values();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<ReflectedMethod<I>> findFor(I identifier) {
|
||||||
|
return Arrays.asList(this.methodStore.get(identifier));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<ReflectedMethod<I>> findFirstFor(I identifier) {
|
||||||
|
return findFor(identifier).stream().findFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package net.tomatentum.cutin.container;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.ReflectedMethod;
|
||||||
|
|
||||||
|
public interface MethodContainer<I extends Object> {
|
||||||
|
|
||||||
|
void addMethods(ReflectedMethod<I>... methods);
|
||||||
|
void addMethods(Object containingObject, Method... methods);
|
||||||
|
default void addAllMethods(Object containingObject) {
|
||||||
|
this.addMethods(containingObject, containingObject.getClass().getDeclaredMethods());
|
||||||
|
};
|
||||||
|
|
||||||
|
Collection<ReflectedMethod<I>> methods();
|
||||||
|
Collection<ReflectedMethod<I>> findFor(I identifier);
|
||||||
|
Optional<ReflectedMethod<I>> findFirstFor(I identifier);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,115 @@
|
|||||||
|
package net.tomatentum.cutin.container;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.ReflectedMethod;
|
||||||
|
import net.tomatentum.cutin.ReflectedMethodFactory;
|
||||||
|
import net.tomatentum.cutin.util.ReflectionUtil;
|
||||||
|
|
||||||
|
public class MultiMethodContainer<I extends Object> implements MethodContainer<I> {
|
||||||
|
|
||||||
|
private Set<Entry<I>> entries;
|
||||||
|
private ReflectedMethodFactory factory;
|
||||||
|
|
||||||
|
public MultiMethodContainer(ReflectedMethodFactory factory) {
|
||||||
|
this.entries = new HashSet<>();
|
||||||
|
this.factory = factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addMethods(ReflectedMethod<I>... methods) {
|
||||||
|
for (ReflectedMethod<I> rMethod : methods) {
|
||||||
|
Optional<Entry<I>> oentry = this.entries.stream()
|
||||||
|
.filter(e -> rMethod.identifier().equals(e.identifier()))
|
||||||
|
.findFirst();
|
||||||
|
Entry<I> entry = oentry.orElse(new Entry<>(rMethod.identifier())).addMethod(rMethod);
|
||||||
|
if (oentry.isEmpty()) this.entries.add(entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addMethods(Object containingObject, Method... methods) {
|
||||||
|
for (Method method : methods)
|
||||||
|
this.addMethods(this.factory.produce(method, containingObject));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<ReflectedMethod<I>> methods() {
|
||||||
|
return this.entries.stream()
|
||||||
|
.flatMap(e -> e.methods.stream())
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<ReflectedMethod<I>> findFor(I identifier) {
|
||||||
|
return this.entries.stream()
|
||||||
|
.filter(e -> e.identifier().equals(identifier))
|
||||||
|
.flatMap(e -> e.methods.stream())
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<ReflectedMethod<I>> findFirstFor(I identifier) {
|
||||||
|
return this.entries.stream()
|
||||||
|
.filter(e -> e.identifier().equals(identifier))
|
||||||
|
.flatMap(e -> e.methods.stream())
|
||||||
|
.findFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static record Entry<I extends Object>(I identifier, Set<ReflectedMethod<I>> methods) {
|
||||||
|
|
||||||
|
public Entry(I identifier) {
|
||||||
|
this(identifier, new HashSet<>());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Logger logger = LoggerFactory.getLogger(Entry.class);
|
||||||
|
|
||||||
|
public Entry<I> addMethod(ReflectedMethod<I> method) {
|
||||||
|
I midentifier = method.identifier();
|
||||||
|
|
||||||
|
if (!this.identifier().equals(midentifier))
|
||||||
|
throw new IllegalArgumentException("Method's identifier did not equal the entry's identifier");
|
||||||
|
|
||||||
|
this.methods.add(method);
|
||||||
|
logger.debug("Added method {} to entry {}", method.method().getName(), this);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object[] runAll(Object context) {
|
||||||
|
logger.trace("Running all Methods from {} with context {}", this, context);
|
||||||
|
return this.methods.stream()
|
||||||
|
.map(x -> {
|
||||||
|
logger.debug("Running Method {} from {} with context {}", x, this, context);
|
||||||
|
return x.run(context);
|
||||||
|
})
|
||||||
|
.flatMap(ReflectionUtil::getReturnAsStream)
|
||||||
|
.toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (!(obj instanceof Entry))
|
||||||
|
return false;
|
||||||
|
Entry<?> other = (Entry<?>) obj;
|
||||||
|
return other.identifier().equals(identifier());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return this.identifier().hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Content(%s)".formatted(identifier().toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user