Cutin/lib/src/main/java/net/tomatentum/cutin/container/LoneMethodContainer.java
Tueem eccad71837
All checks were successful
Build / Gradle-Build (push) Successful in 13s
Publish / Gradle-Publish (push) Successful in 10s
Test / Gradle-Test (push) Successful in 11s
feat(logging): add more logging
2025-04-14 18:34:46 +02:00

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;
}
}