Compare commits

..

2 Commits

Author SHA1 Message Date
c0da5ee75d
feat(container): add chaining, single method add method for overriding, key getter and protected entries getter
All checks were successful
Build / Gradle-Build (push) Successful in 16s
Publish / Gradle-Publish (push) Successful in 12s
Test / Gradle-Test (push) Successful in 12s
2025-04-10 14:44:57 +02:00
3deee2fd5d
bump version 2025-04-10 14:25:12 +02:00
4 changed files with 47 additions and 17 deletions

View File

@ -6,7 +6,7 @@ plugins {
allprojects {
group = "net.tomatentum.cutin"
version = "0.1.0" + (if (!project.hasProperty("release")) ("-" + getGitHash()) else "")
version = "0.1.1" + (if (!project.hasProperty("release")) ("-" + getGitHash()) else "")
description = "A lightweight Reflection abstraction specifically but not exclusively made for tueem/Marinara."
plugins.withType<JavaPlugin> {
tasks.withType<Jar>().configureEach {

View File

@ -6,6 +6,7 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import net.tomatentum.cutin.ReflectedMethod;
import net.tomatentum.cutin.ReflectedMethodFactory;
@ -21,15 +22,22 @@ public class LoneMethodContainer<I extends Object> implements MethodContainer<I>
}
@Override
public void addMethods(ReflectedMethod<I>... methods) {
for (ReflectedMethod<I> reflectedMethod : methods)
this.methodStore.put(reflectedMethod.identifier(), reflectedMethod);
public MethodContainer<I> addMethod(ReflectedMethod<I> method) {
this.methodStore.put(method.identifier(), method);
return this;
}
@Override
public void addMethods(Object containingObject, Method... methods) {
public MethodContainer<I> addMethods(Object containingObject, Method... methods) {
for (Method method : methods)
this.addMethods(this.factory.produce(method, containingObject));
this.factory.produce(method, containingObject)
.ifPresent(this::addMethod);
return this;
}
@Override
public Set<I> identifiers() {
return methodStore.keySet();
}
@Override

View File

@ -3,17 +3,26 @@ package net.tomatentum.cutin.container;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Optional;
import java.util.Set;
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) {
MethodContainer<I> addMethod(ReflectedMethod<I> method);
default MethodContainer<I> addMethods(ReflectedMethod<I>[] methods) {
for (ReflectedMethod<I> reflectedMethod : methods) {
this.addMethod(reflectedMethod);
}
return this;
}
MethodContainer<I> addMethods(Object containingObject, Method... methods);
default MethodContainer<I> 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);

View File

@ -5,6 +5,7 @@ import java.util.Collection;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -24,20 +25,28 @@ public class MultiMethodContainer<I extends Object> implements MethodContainer<I
}
@Override
public void addMethods(ReflectedMethod<I>... methods) {
for (ReflectedMethod<I> rMethod : methods) {
public MethodContainer<I> addMethod(ReflectedMethod<I> method) {
Optional<Entry<I>> oentry = this.entries.stream()
.filter(e -> rMethod.identifier().equals(e.identifier()))
.filter(e -> method.identifier().equals(e.identifier()))
.findFirst();
Entry<I> entry = oentry.orElse(new Entry<>(rMethod.identifier())).addMethod(rMethod);
Entry<I> entry = oentry.orElse(new Entry<>(method.identifier())).addMethod(method);
if (oentry.isEmpty()) this.entries.add(entry);
}
return this;
}
@Override
public void addMethods(Object containingObject, Method... methods) {
public MethodContainer<I> addMethods(Object containingObject, Method... methods) {
for (Method method : methods)
this.addMethods(this.factory.produce(method, containingObject));
this.factory.produce(method, containingObject)
.ifPresent(this::addMethod);
return this;
}
@Override
public Set<I> identifiers() {
return entries().stream()
.map(Entry::identifier)
.collect(Collectors.toSet());
}
@Override
@ -63,6 +72,10 @@ public class MultiMethodContainer<I extends Object> implements MethodContainer<I
.findFirst();
}
protected Set<Entry<I>> entries() {
return this.entries;
}
public static record Entry<I extends Object>(I identifier, Set<ReflectedMethod<I>> methods) {
public Entry(I identifier) {