-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat. Added simple dependency injector auxiliary class
- Loading branch information
Showing
13 changed files
with
207 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/io/eigr/spawn/api/extensions/DependencyInjector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package io.eigr.spawn.api.extensions; | ||
|
||
public interface DependencyInjector { | ||
|
||
<T extends Object> void bind(Class<T> type, Object instance); | ||
|
||
<T extends Object> T getInstance(Class<T> type); | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/io/eigr/spawn/api/extensions/SimpleDependencyInjector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.eigr.spawn.api.extensions; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public final class SimpleDependencyInjector implements DependencyInjector { | ||
|
||
private final Map<Class<?>, Object> bucket; | ||
|
||
private SimpleDependencyInjector() { | ||
this.bucket = new HashMap<>(); | ||
} | ||
|
||
private static class SimpleDependencyInjectorHelper{ | ||
private static final SimpleDependencyInjector INSTANCE = new SimpleDependencyInjector(); | ||
} | ||
|
||
public static DependencyInjector createInjector(){ | ||
return SimpleDependencyInjectorHelper.INSTANCE; | ||
} | ||
|
||
@Override | ||
public <T> void bind(Class<T> type, Object instance) { | ||
if (this.bucket.containsKey(type)) { | ||
throw new IllegalArgumentException("There is already an instance associated with this type in the bucket"); | ||
} | ||
|
||
this.bucket.put(type, instance); | ||
} | ||
|
||
@Override | ||
public <T> T getInstance(Class<T> type) { | ||
return type.cast(this.bucket.get(type)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package io.eigr.spawn.test.extensions; | ||
|
||
public interface A { | ||
String say(String name); | ||
} |
8 changes: 8 additions & 0 deletions
8
src/test/java/io/eigr/spawn/test/extensions/AImplementation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package io.eigr.spawn.test.extensions; | ||
|
||
public class AImplementation implements A { | ||
@Override | ||
public String say(String name) { | ||
return String.format("Hello %s", name); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/test/java/io/eigr/spawn/test/extensions/SimpleDependencyInjectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.eigr.spawn.test.extensions; | ||
|
||
import io.eigr.spawn.api.extensions.DependencyInjector; | ||
import io.eigr.spawn.api.extensions.SimpleDependencyInjector; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class SimpleDependencyInjectorTest { | ||
|
||
private DependencyInjector injector; | ||
|
||
@Before | ||
public void before() { | ||
injector = SimpleDependencyInjector.createInjector(); | ||
} | ||
|
||
@Test | ||
public void testInjection() { | ||
injector.bind(A.class, new AImplementation()); | ||
|
||
A implementation = injector.getInstance(A.class); | ||
String actual = implementation.say("Spawn"); | ||
assertEquals("Hello Spawn", actual); | ||
} | ||
} |