-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIOC.java
More file actions
38 lines (26 loc) · 1.01 KB
/
IOC.java
File metadata and controls
38 lines (26 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import jdk.nashorn.internal.codegen.types.Type;
import java.util.HashMap;
public class IOC {
/**
* This is experimental - IOC-Container doesn't inject dependencies yet.
*/
private static final IOC instance = new IOC(new HashMap<>());
public IOC(HashMap<Type, Object> injectables) {
this.injectables = injectables;
}
private final HashMap<Type, Object> injectables;
public static IOC instance() {
return instance;
}
@SuppressWarnings("unchecked cast")
public <T> T resolveObject(Class<? extends T> type) throws ClassNotFoundException {
if (!canResolve(type)) throw new ClassNotFoundException("cannot create class");
return (T) injectables.get(Type.typeFor(type)); // cast is checked by logic
}
public <T> void setClassObject(T object) {
injectables.put(Type.typeFor(object.getClass()), object);
}
public boolean canResolve(Class resolveType){
return injectables.keySet().contains(Type.typeFor(resolveType));
}
}