Merge pull request 'Add Mutable and Immutable Factories' (#1) from feat/mutable-factories into dev
Reviewed-on: #1
This commit is contained in:
commit
dba1a366d9
@ -12,7 +12,7 @@ import java.util.Set;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import net.tomatentum.cutin.ReflectedMethodFactory;
|
import net.tomatentum.cutin.factory.ReflectedMethodFactory;
|
||||||
import net.tomatentum.cutin.method.ReflectedMethod;
|
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||||
|
|
||||||
public class LoneMethodContainer<I extends Object, C extends Object> implements MethodContainer<I, C> {
|
public class LoneMethodContainer<I extends Object, C extends Object> implements MethodContainer<I, C> {
|
||||||
@ -29,6 +29,10 @@ public class LoneMethodContainer<I extends Object, C extends Object> implements
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MethodContainer<I, C> addMethod(ReflectedMethod<I, C> method) {
|
public MethodContainer<I, C> addMethod(ReflectedMethod<I, C> method) {
|
||||||
|
if (this.methodStore.keySet().contains(method.identifier())) {
|
||||||
|
logger.warn("Could not add {} to container because the same identifier was already present.", method);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
this.methodStore.put(method.identifier(), method);
|
this.methodStore.put(method.identifier(), method);
|
||||||
logger.debug("Added {} to container", method);
|
logger.debug("Added {} to container", method);
|
||||||
return this;
|
return this;
|
||||||
|
@ -10,7 +10,7 @@ import java.util.stream.Collectors;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import net.tomatentum.cutin.ReflectedMethodFactory;
|
import net.tomatentum.cutin.factory.ReflectedMethodFactory;
|
||||||
import net.tomatentum.cutin.method.ReflectedMethod;
|
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||||
import net.tomatentum.cutin.util.ReflectionUtil;
|
import net.tomatentum.cutin.util.ReflectionUtil;
|
||||||
|
|
||||||
|
@ -0,0 +1,20 @@
|
|||||||
|
package net.tomatentum.cutin.factory;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.MethodParser;
|
||||||
|
|
||||||
|
public abstract class ImmutableMethodFactory<I extends Object, C extends Object> implements ReflectedMethodFactory.Factory<I, C> {
|
||||||
|
|
||||||
|
protected Set<MethodParser> parser;
|
||||||
|
|
||||||
|
protected ImmutableMethodFactory(MethodParser... parser) {
|
||||||
|
this.parser = Set.of(parser);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<MethodParser> parser() {
|
||||||
|
return this.parser;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package net.tomatentum.cutin.factory;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.MethodParser;
|
||||||
|
|
||||||
|
public abstract class MutableMethodFactory<I extends Object, C extends Object> extends ImmutableMethodFactory<I, C> {
|
||||||
|
|
||||||
|
public MutableMethodFactory<I, C> addParser(MethodParser parser) {
|
||||||
|
super.parser.add(parser);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MutableMethodFactory<I, C> removeParser(MethodParser parser) {
|
||||||
|
super.parser.remove(parser);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,21 +1,23 @@
|
|||||||
package net.tomatentum.cutin;
|
package net.tomatentum.cutin.factory;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.MethodParser;
|
||||||
import net.tomatentum.cutin.method.ReflectedMethod;
|
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||||
|
|
||||||
|
|
||||||
public interface ReflectedMethodFactory<I extends Object, C extends Object> {
|
public interface ReflectedMethodFactory<I extends Object, C extends Object> {
|
||||||
Optional<ReflectedMethod<I, C>> produce(Method method, Object containingClass);
|
Optional<ReflectedMethod<I, C>> produce(Method method, Object containingClass);
|
||||||
ReflectedMethodFactory<I, C> addFactory(Factory<I, C> factory);
|
ReflectedMethodFactory<I, C> addFactory(Factory<I, C> factory);
|
||||||
|
Set<ReflectedMethodFactory.Factory<I, C>> factories();
|
||||||
|
|
||||||
public interface Factory<I extends Object, C extends Object> {
|
public interface Factory<I extends Object, C extends Object> {
|
||||||
|
|
||||||
Optional<ReflectedMethod<I, C>> produce(Method method, Object containingObject, ParserResults parserResults);
|
Optional<ReflectedMethod<I, C>> produce(Method method, Object containingObject, ParserResults parserResults);
|
||||||
void addParser(Set<MethodParser> parser);
|
Set<MethodParser> parser();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,30 +1,28 @@
|
|||||||
package net.tomatentum.cutin;
|
package net.tomatentum.cutin.factory;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.MethodParser;
|
||||||
import net.tomatentum.cutin.method.ReflectedMethod;
|
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||||
import net.tomatentum.cutin.util.ReflectionUtil;
|
import net.tomatentum.cutin.util.ReflectionUtil;
|
||||||
|
|
||||||
|
|
||||||
public class ReflectedMethodFactoryImpl<I extends Object, C extends Object> implements ReflectedMethodFactory<I, C> {
|
public class ReflectedMethodFactoryImpl<I extends Object, C extends Object> implements ReflectedMethodFactory<I, C> {
|
||||||
|
|
||||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
private List<Factory<I, C>> factories;
|
private Set<Factory<I, C>> factories;
|
||||||
|
|
||||||
public ReflectedMethodFactoryImpl() {
|
public ReflectedMethodFactoryImpl() {
|
||||||
this(new ArrayList<>());
|
this(new HashSet<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
public ReflectedMethodFactoryImpl(List<Factory<I, C>> factories) {
|
public ReflectedMethodFactoryImpl(Set<Factory<I, C>> factories) {
|
||||||
this.factories = factories;
|
this.factories = factories;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,9 +49,13 @@ public class ReflectedMethodFactoryImpl<I extends Object, C extends Object> impl
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Factory<I, C>> factories() {
|
||||||
|
return this.factories;
|
||||||
|
}
|
||||||
|
|
||||||
private Optional<ReflectedMethod<I, C>> factoryProduce(Factory<I, C> factory, Method method, Object containingClass) {
|
private Optional<ReflectedMethod<I, C>> factoryProduce(Factory<I, C> factory, Method method, Object containingClass) {
|
||||||
Set<MethodParser> parser = new HashSet<>();
|
Set<MethodParser> parser = factory.parser();
|
||||||
factory.addParser(parser);
|
|
||||||
ParserResults results = ParserResults.create(parser, method, containingClass);
|
ParserResults results = ParserResults.create(parser, method, containingClass);
|
||||||
return factory.produce(method, containingClass, results);
|
return factory.produce(method, containingClass, results);
|
||||||
}
|
}
|
@ -5,14 +5,10 @@ import java.util.ArrayList;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import net.tomatentum.cutin.ReflectedMethodFactory;
|
|
||||||
import net.tomatentum.cutin.ReflectedMethodFactory.ParserResults;
|
|
||||||
import net.tomatentum.cutin.container.MethodContainer;
|
|
||||||
import net.tomatentum.cutin.util.ReflectionUtil;
|
import net.tomatentum.cutin.util.ReflectionUtil;
|
||||||
|
|
||||||
public abstract class BestCandidateMethod<I extends Object, C extends Object> extends ReflectedMethod<I, C> {
|
public abstract class BestCandidateMethod<I extends Object, C extends Object> extends ReflectedMethod<I, C> {
|
||||||
@ -70,27 +66,5 @@ public abstract class BestCandidateMethod<I extends Object, C extends Object> ex
|
|||||||
}
|
}
|
||||||
return parameters;
|
return parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract static class Factory<I extends Object, C extends Object> implements ReflectedMethodFactory.Factory<I, C> {
|
|
||||||
|
|
||||||
private MethodContainer<I, C> methodContainer;
|
|
||||||
private String methodName;
|
|
||||||
|
|
||||||
protected Factory(MethodContainer<I, C> methodContainer, String methodName) {
|
|
||||||
this.methodContainer = methodContainer;
|
|
||||||
this.methodName = methodName;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Optional<ReflectedMethod<I, C>> produce(Method method, Object containingObject, ParserResults results) {
|
|
||||||
Optional<BestCandidateMethod<I, C>> bcMethod = bcProduce(methodName, containingObject, results);
|
|
||||||
if (bcMethod.isEmpty() || methodContainer.methods().contains(bcMethod.get()))
|
|
||||||
return Optional.empty();
|
|
||||||
return Optional.of(bcMethod.get());
|
|
||||||
}
|
|
||||||
|
|
||||||
protected abstract Optional<BestCandidateMethod<I, C>> bcProduce(String methodName, Object containingObject, ParserResults results);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user