71 lines
2.0 KiB
Java
71 lines
2.0 KiB
Java
package net.tomatentum.cutin.container;
|
|
|
|
import java.lang.reflect.Method;
|
|
import java.util.Arrays;
|
|
import java.util.Collection;
|
|
import java.util.Collections;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Optional;
|
|
import java.util.Set;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import net.tomatentum.cutin.ReflectedMethodFactory;
|
|
import net.tomatentum.cutin.method.ReflectedMethod;
|
|
|
|
public class LoneMethodContainer<I extends Object, C extends Object> implements MethodContainer<I, C> {
|
|
|
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
private Map<I, ReflectedMethod<I, C>> methodStore;
|
|
private ReflectedMethodFactory<I, C> factory;
|
|
|
|
public LoneMethodContainer(ReflectedMethodFactory<I, C> factory) {
|
|
this.methodStore = new HashMap<>();
|
|
this.factory = factory;
|
|
}
|
|
|
|
@Override
|
|
public MethodContainer<I, C> addMethod(ReflectedMethod<I, C> method) {
|
|
this.methodStore.put(method.identifier(), method);
|
|
logger.debug("Added {} to container", method);
|
|
return this;
|
|
}
|
|
|
|
@Override
|
|
public MethodContainer<I, C> addMethods(Object containingObject, Method... methods) {
|
|
for (Method method : methods)
|
|
this.factory.produce(method, containingObject)
|
|
.ifPresent(this::addMethod);
|
|
return this;
|
|
}
|
|
|
|
@Override
|
|
public Set<I> identifiers() {
|
|
return methodStore.keySet();
|
|
}
|
|
|
|
@Override
|
|
public Collection<ReflectedMethod<I, C>> methods() {
|
|
return this.methodStore.values();
|
|
}
|
|
|
|
@Override
|
|
public Collection<ReflectedMethod<I, C>> findFor(I identifier) {
|
|
ReflectedMethod<I, C> result = this.methodStore.get(identifier);
|
|
return result != null ? Arrays.asList(result) : Collections.emptyList();
|
|
}
|
|
|
|
@Override
|
|
public Optional<ReflectedMethod<I, C>> findFirstFor(I identifier) {
|
|
return findFor(identifier).stream().findFirst();
|
|
}
|
|
|
|
public ReflectedMethodFactory<I, C> factory() {
|
|
return this.factory;
|
|
}
|
|
|
|
}
|