Skip to content

Commit

Permalink
Add throwing supplier
Browse files Browse the repository at this point in the history
  • Loading branch information
P3ridot committed Jul 17, 2023
1 parent f646773 commit 0f1bc8f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 9 deletions.
22 changes: 20 additions & 2 deletions di/src/main/java/org/panda_lang/utilities/inject/Bind.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package org.panda_lang.utilities.inject;

import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import panda.std.function.ThrowingSupplier;
import panda.std.function.ThrowingTriFunction;
import panda.std.function.TriFunction;
import java.lang.annotation.Annotation;
Expand All @@ -25,7 +27,7 @@
public interface Bind<A extends Annotation> extends Comparable<Bind<A>> {

/**
* Assign object to the bind
* Assign an object to the bind
*
* @param value the instance to assign
*/
Expand All @@ -36,7 +38,23 @@ public interface Bind<A extends Annotation> extends Comparable<Bind<A>> {
*
* @param valueSupplier the supplier to assign
*/
void assignInstance(Supplier<?> valueSupplier);
void assignSupplier(Supplier<?> valueSupplier);

/**
* @deprecated use {@link #assignSupplier(Supplier)} instead
*/
@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion = "2.0.0")
default void assignInstance(Supplier<?> valueSupplier) {
this.assignSupplier(valueSupplier);
}

/**
* Assign value supplier to the bind which can throw an exception
*
* @param valueSupplier the supplier to assign
*/
void assignThrowingSupplier(ThrowingSupplier<?, ? extends Exception> valueSupplier);

/**
* Assign custom handler to the bind
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.panda_lang.utilities.inject;

import panda.std.function.ThrowingSupplier;
import panda.std.function.ThrowingTriFunction;
import panda.std.function.TriFunction;
import panda.utilities.ObjectUtils;
Expand Down Expand Up @@ -51,7 +52,12 @@ public void assignInstance(Object value) {
}

@Override
public void assignInstance(Supplier<?> valueSupplier) {
public void assignSupplier(Supplier<?> valueSupplier) {
with(new StaticBindValue<>(valueSupplier));
}

@Override
public void assignThrowingSupplier(ThrowingSupplier<?, ? extends Exception> valueSupplier) {
with(new StaticBindValue<>(valueSupplier));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@
package org.panda_lang.utilities.inject;

import java.lang.annotation.Annotation;
import java.util.function.Supplier;
import panda.std.function.ThrowingSupplier;

final class StaticBindValue<A extends Annotation> implements BindValue<A> {

private final Supplier<?> valueSupplier;
private final ThrowingSupplier<?, ? extends Exception> valueSupplier;

StaticBindValue(Object value) {
this(() -> value);
}

StaticBindValue(Supplier<?> valueSupplier) {
StaticBindValue(ThrowingSupplier<?, ? extends Exception> valueSupplier) {
this.valueSupplier = valueSupplier;
}

@Override
public Object getValue(Property required, A annotation, Object... injectorArgs) {
public Object getValue(Property required, A annotation, Object... injectorArgs) throws Exception {
return valueSupplier.get();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static class Service2 {
}

@Test
void shouldNotCreateInstance() {
void shouldNotCreateInstanceAndFail() {
Injector injector = DependencyInjection.createInjector(resources -> {
resources.on(String.class).assignThrowingHandler((type, annotation, args) -> {
throw new Exception("Failed");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

final class DependencyInjectionInstancesTest {

Expand All @@ -13,12 +14,26 @@ void shouldInjectInstances() {
// some logic, a few hours later...

injector.getResources().on(Custom.class).assignInstance(new CustomImpl()); // singleton
injector.getResources().on(Bean.class).assignInstance(Bean::new); // new instance per call
injector.getResources().on(Bean.class).assignSupplier(Bean::new); // new instance per call

Service service = injector.newInstance(Service.class);
assertNotNull(service);
}

@Test
void shouldNotInjectInstances() {
Injector injector = DependencyInjection.createInjector(resources -> {
resources.on(Custom.class).assignThrowingSupplier(() -> {
throw new Exception("Failed");
});
resources.on(Bean.class).assignThrowingSupplier(() -> {
throw new Exception("Failed");
});
});

assertThrows(DependencyInjectionException.class, () -> injector.newInstance(Service.class));
}

public static class Bean { }

public interface Custom { }
Expand Down

0 comments on commit 0f1bc8f

Please sign in to comment.