|
1
|
|
|
/* |
|
2
|
|
|
* This file is part of Araknemu. |
|
3
|
|
|
* |
|
4
|
|
|
* Araknemu is free software: you can redistribute it and/or modify |
|
5
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
|
6
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
7
|
|
|
* (at your option) any later version. |
|
8
|
|
|
* |
|
9
|
|
|
* Araknemu is distributed in the hope that it will be useful, |
|
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12
|
|
|
* GNU Lesser General Public License for more details. |
|
13
|
|
|
* |
|
14
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
|
15
|
|
|
* along with Araknemu. If not, see <https://www.gnu.org/licenses/>. |
|
16
|
|
|
* |
|
17
|
|
|
* Copyright (c) 2017-2021 Vincent Quatrevieux |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
package fr.quatrevieux.araknemu.core.di; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Dependency injection container |
|
24
|
|
|
*/ |
|
25
|
|
|
public interface Container { |
|
26
|
|
|
/** |
|
27
|
|
|
* Get instance from the container |
|
28
|
|
|
* The return value of get, with same type SHOULD return equals value |
|
29
|
|
|
* The value MAY reference to another instance |
|
30
|
|
|
* |
|
31
|
|
|
* @param type Type to find |
|
32
|
|
|
* @param <T> Type |
|
33
|
|
|
* |
|
34
|
|
|
* @return The contained instance |
|
35
|
|
|
* |
|
36
|
|
|
* @throws ItemNotFoundException When cannot found given type |
|
37
|
|
|
* @throws ContainerException When cannot instantiate the element |
|
38
|
|
|
*/ |
|
39
|
|
|
public <T> T get(Class<T> type) throws ContainerException; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Check if the container contains the type |
|
43
|
|
|
* |
|
44
|
|
|
* @param type Type to find |
|
45
|
|
|
* |
|
46
|
|
|
* @return true if the contains contains the type |
|
47
|
|
|
*/ |
|
48
|
|
|
public boolean has(Class type); |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Register a module into the container |
|
52
|
|
|
* |
|
53
|
|
|
* @param module Module to register |
|
54
|
|
|
*/ |
|
55
|
|
|
public void register(ContainerModule module); |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Create a scoped container with a single defined service |
|
59
|
|
|
* |
|
60
|
|
|
* Note: By default, the value will be mapped with it's own class |
|
61
|
|
|
* |
|
62
|
|
|
* Usage: |
|
63
|
|
|
* <pre>{@code |
|
64
|
|
|
* Foo foo = new Foo(); |
|
65
|
|
|
* |
|
66
|
|
|
* Container scoped = container.with(Foo.class, FooInterface.class); |
|
67
|
|
|
* |
|
68
|
|
|
* scoped.get(Foo.class); // Get foo |
|
69
|
|
|
* scoped.get(FooInterface.class); // Same as above |
|
70
|
|
|
* scope.get(OtherClass.class); // Get the service OtherClass from the base container |
|
71
|
|
|
* }</pre> |
|
72
|
|
|
* |
|
73
|
|
|
* @param value The value to store |
|
74
|
|
|
* @param interfaces Interfaces to defined |
|
75
|
|
|
* |
|
76
|
|
|
* @param <T> The value type |
|
77
|
|
|
* |
|
78
|
|
|
* @return The scoped container |
|
79
|
|
|
* @see ScopedContainer |
|
80
|
|
|
*/ |
|
81
|
|
|
public default <T> Container with(T value, Class<? super T>... interfaces) { |
|
82
|
1 |
|
return withAll(new ScopedContainer.Mapping<>(value, interfaces)); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Create a scoped container with multiple services |
|
87
|
|
|
* |
|
88
|
|
|
* Note: By default, the value will be mapped with it's own class |
|
89
|
|
|
* |
|
90
|
|
|
* Usage: |
|
91
|
|
|
* <pre>{@code |
|
92
|
|
|
* Foo foo = new Foo(); |
|
93
|
|
|
* |
|
94
|
|
|
* Container scoped = container.withAll( |
|
95
|
|
|
* new ScopedContainer.Mapping<>(Foo.class, FooInterface.class), |
|
96
|
|
|
* new ScopedContainer.Mapping<>(Bar.class), |
|
97
|
|
|
* ); |
|
98
|
|
|
* |
|
99
|
|
|
* scoped.get(Foo.class); // Get foo |
|
100
|
|
|
* scoped.get(FooInterface.class); // Same as above |
|
101
|
|
|
* scope.get(Bar.class); // Get bar |
|
102
|
|
|
* }</pre> |
|
103
|
|
|
* |
|
104
|
|
|
* @param mappings The interfaces / value mappings |
|
105
|
|
|
* |
|
106
|
|
|
* @return The scoped container |
|
107
|
|
|
* @see ScopedContainer |
|
108
|
|
|
*/ |
|
109
|
|
|
public default Container withAll(ScopedContainer.Mapping<?>... mappings) { |
|
110
|
1 |
|
return ScopedContainer.fromMapping(this, mappings); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|