Skip to content

Commit

Permalink
Include YADIC in class names
Browse files Browse the repository at this point in the history
  • Loading branch information
ref-humbold committed Oct 27, 2024
1 parent c0a8e35 commit 69ef7d1
Show file tree
Hide file tree
Showing 64 changed files with 272 additions and 225 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package yadic;

import yadic.annotation.Dependency;
import yadic.registry.DependencyRegistry;
import yadic.resolver.TypesResolver;

public final class DiContainer
implements DiResolver
public final class YadicContainer
implements YadicResolver
{
private final DependencyRegistry registry;
private final TypesResolver resolver;

public DiContainer()
public YadicContainer()
{
registry = new DependencyRegistry();
resolver = new TypesResolver(registry);
Expand All @@ -22,7 +21,7 @@ public DiContainer()
* @param policy construction policy of instances
* @return {@code this} for method chaining
*/
public <T> DiContainer registerType(Class<T> type, ConstructionPolicy policy)
public <T> YadicContainer registerType(Class<T> type, ConstructionPolicy policy)
{
registry.addType(type, policy);
return this;
Expand All @@ -35,7 +34,7 @@ public <T> DiContainer registerType(Class<T> type, ConstructionPolicy policy)
* @param policy construction policy of instances
* @return {@code this} for method chaining
*/
public <T> DiContainer registerType(
public <T> YadicContainer registerType(
Class<T> supertype, Class<? extends T> subtype, ConstructionPolicy policy)
{
registry.addType(supertype, subtype, policy);
Expand All @@ -48,18 +47,12 @@ public <T> DiContainer registerType(
* @param instance concrete instance
* @return {@code this} for method chaining
*/
public <T> DiContainer registerInstance(Class<T> type, T instance)
public <T> YadicContainer registerInstance(Class<T> type, T instance)
{
registry.addInstance(type, instance);
return this;
}

/**
* Resolve all depencencies and construct a new instance of given type using {@link Dependency}.
* @param type type class
* @return new instance
* @throws DiException if type cannot be resolved
*/
@Override
public <T> T resolve(Class<T> type)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

import java.io.Serial;

public class DiException
public class YadicException
extends RuntimeException
{
@Serial private static final long serialVersionUID = -3019200382390630637L;

public DiException(String message)
public YadicException(String message)
{
super(message);
}

public DiException(String message, Throwable cause)
public YadicException(String message, Throwable cause)
{
super(message, cause);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package yadic;

import yadic.annotation.Dependency;

public interface DiResolver
public interface YadicResolver
{
/**
* Resolve all dependencies and construct a new instance of given type using {@link Dependency}.
* Resolve all dependencies and construct a new instance of given type.
* @param type type class
* @return new instance
* @throws DiException if type cannot be resolved
* @throws YadicException if type cannot be resolved
*/
<T> T resolve(Class<T> type);

/**
* Resolve all dependencies and construct a new instance of given type using {@link Dependency}.
* Resolve all dependencies and construct a new instance of given type.
* @param type type class
* @return new instance, or {@code null} if type cannot be resolved.
*/
Expand All @@ -23,7 +21,7 @@ default <T> T resolveOrNull(Class<T> type)
{
return resolve(type);
}
catch(DiException ex)
catch(YadicException ex)
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD})
public @interface Dependency
public @interface YadicDependency
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Register
public @interface YadicRegister
{
Class<?> value();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface SelfRegister
public @interface YadicRegisterSelf
{
ConstructionPolicy policy() default ConstructionPolicy.CONSTRUCTION;
}
3 changes: 2 additions & 1 deletion src/main/java/yadic/registry/DependencyRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import yadic.registry.exception.RegistrationException;
import yadic.registry.valuetypes.Instance;
import yadic.registry.valuetypes.TypeConstruction;
import yadic.utils.TypeUtils;

public class DependencyRegistry
{
Expand Down Expand Up @@ -62,7 +63,7 @@ private <T> void validateRegisteredType(Class<T> type)

private <T> void validateRegisteredInstance(Class<T> type)
{
if(TypesUtils.isAnnotatedType(type))
if(TypeUtils.isAnnotatedType(type))
throw new RegistrationException(
String.format("Cannot register instance for annotated type %s",
type.getSimpleName()));
Expand Down
59 changes: 33 additions & 26 deletions src/main/java/yadic/registry/TypesDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
import java.util.Map;

import yadic.ConstructionPolicy;
import yadic.annotation.Register;
import yadic.annotation.SelfRegister;
import yadic.annotation.YadicRegister;
import yadic.annotation.YadicRegisterSelf;
import yadic.registry.exception.AbstractTypeException;
import yadic.registry.exception.AnnotatedTypeRegistrationException;
import yadic.registry.exception.MixingPoliciesException;
import yadic.registry.exception.NotDerivedTypeException;
import yadic.registry.valuetypes.Instance;
import yadic.registry.valuetypes.TypeConstruction;
import yadic.resolver.exception.MissingDependenciesException;
import yadic.utils.TypeUtils;

class TypesDictionary
{
Expand All @@ -24,34 +25,35 @@ <T> void insert(Class<T> type, ConstructionPolicy policy)
{
validateAnnotation(type);

if(type.isAnnotationPresent(Register.class))
if(type.isAnnotationPresent(YadicRegister.class))
{
Register annotation = type.getAnnotation(Register.class);
YadicRegister annotation = type.getAnnotation(YadicRegister.class);

doInsert(type, new TypeConstruction<>((Class<? extends T>)annotation.value(),
annotation.policy()));
}
else if(type.isAnnotationPresent(SelfRegister.class))
else if(type.isAnnotationPresent(YadicRegisterSelf.class))
{
SelfRegister annotation = type.getAnnotation(SelfRegister.class);
YadicRegisterSelf annotation = type.getAnnotation(YadicRegisterSelf.class);

doInsert(type, new TypeConstruction<>(type, annotation.policy()));
}
else
{
if(TypesUtils.isAbstractReferenceType(type))
if(TypeUtils.isAbstractReferenceType(type))
throw new AbstractTypeException(
String.format("Cannot register abstract type %s", type.getName()));
String.format("Cannot register abstract type %s", type.getTypeName()));

doInsert(type, new TypeConstruction<>(type, policy));
}
}

<T> void insert(Class<T> type, Class<? extends T> subtype, ConstructionPolicy policy)
{
if(TypesUtils.isAnnotatedType(type))
if(TypeUtils.isAnnotatedType(type))
throw new AnnotatedTypeRegistrationException(
String.format("Cannot register type for annotated type %s", type.getName()));
String.format("Cannot register type for annotated type %s",
type.getTypeName()));

doInsert(type, new TypeConstruction<>(subtype, policy));
}
Expand All @@ -67,7 +69,7 @@ boolean contains(Class<?> type)
return false;
}

return TypesUtils.isAnnotatedType(type) || typesMap.containsKey(type);
return TypeUtils.isAnnotatedType(type) || typesMap.containsKey(type);
}

<T> TypeConstruction<? extends T> find(Class<T> type)
Expand All @@ -76,7 +78,7 @@ <T> TypeConstruction<? extends T> find(Class<T> type)
ConstructionPolicy desiredPolicy = mapping.policy();
Class<?> supertype = type;

while(TypesUtils.isAbstractReferenceType(mapping.type())
while(TypeUtils.isAbstractReferenceType(mapping.type())
|| contains(mapping.type()) && !mapping.type().equals(supertype))
{
supertype = mapping.type();
Expand Down Expand Up @@ -109,18 +111,18 @@ <T> Instance<T> getSingleton(Class<T> type)
@SuppressWarnings("unchecked")
private <T> TypeConstruction<? extends T> get(Class<T> type)
{
if(TypesUtils.isAnnotatedType(type) && !typesMap.containsKey(type))
if(TypeUtils.isAnnotatedType(type) && !typesMap.containsKey(type))
insert(type, null);

TypeConstruction<? extends T> mapping = (TypeConstruction<? extends T>)typesMap.get(type);

if(mapping != null)
return mapping;

if(TypesUtils.isAbstractReferenceType(type))
if(TypeUtils.isAbstractReferenceType(type))
throw new MissingDependenciesException(
String.format("Abstract type %s has no registered concrete subclass",
type.getName()));
type.getTypeName()));

return new TypeConstruction<>(type, ConstructionPolicy.CONSTRUCTION);
}
Expand All @@ -133,27 +135,32 @@ private <T> void doInsert(Class<T> type, TypeConstruction<? extends T> mapping)

private void validateAnnotation(Class<?> type)
{
if(type.isAnnotationPresent(Register.class))
if(type.isAnnotationPresent(YadicRegister.class))
{
Register annotation = type.getAnnotation(Register.class);
YadicRegister annotation = type.getAnnotation(YadicRegister.class);
Class<?> subtype = annotation.value();

if(!type.isAssignableFrom(subtype))
throw new NotDerivedTypeException(
String.format("Type %s registered via @Register is not derived type of %s",
subtype.getName(), type.getName()));
String.format("Type %s registered via %s is not derived type of %s",
subtype.getTypeName(),
TypeUtils.getAnnotationName(YadicRegister.class),
type.getTypeName()));

if(TypesUtils.isAbstractReferenceType(subtype))
if(TypeUtils.isAbstractReferenceType(subtype))
throw new AbstractTypeException(
String.format("Type %s registered via @Register in %s is abstract",
subtype.getName(), type.getName()));
String.format("Type %s registered via %s in %s is abstract",
subtype.getTypeName(),
TypeUtils.getAnnotationName(YadicRegister.class),
type.getTypeName()));
}
else if(type.isAnnotationPresent(SelfRegister.class))
else if(type.isAnnotationPresent(YadicRegisterSelf.class))
{
if(TypesUtils.isAbstractReferenceType(type))
if(TypeUtils.isAbstractReferenceType(type))
throw new AbstractTypeException(
String.format("Abstract type %s cannot be annotated with @SelfRegister",
type.getName()));
String.format("Abstract type %s cannot be annotated with %s",
type.getTypeName(),
TypeUtils.getAnnotationName(YadicRegisterSelf.class)));
}
}
}
21 changes: 0 additions & 21 deletions src/main/java/yadic/registry/TypesUtils.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import java.io.Serial;

import yadic.DiException;
import yadic.YadicException;

public class AbstractTypeException
extends DiException
extends YadicException
{
@Serial private static final long serialVersionUID = 5573956461991224741L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import java.io.Serial;

import yadic.DiException;
import yadic.YadicException;

public class AnnotatedTypeRegistrationException
extends DiException
extends YadicException
{
@Serial private static final long serialVersionUID = 4583310501511173747L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import java.io.Serial;

import yadic.DiException;
import yadic.YadicException;

public class MixingPoliciesException
extends DiException
extends YadicException
{
@Serial private static final long serialVersionUID = 7409446718002438943L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import java.io.Serial;

import yadic.DiException;
import yadic.YadicException;

public class NotDerivedTypeException
extends DiException
extends YadicException
{
@Serial private static final long serialVersionUID = -3180961583302361880L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import java.io.Serial;

import yadic.DiException;
import yadic.YadicException;

public class RegistrationException
extends DiException
extends YadicException
{
@Serial private static final long serialVersionUID = 2937758897207647321L;

Expand Down
Loading

0 comments on commit 69ef7d1

Please sign in to comment.