|
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-2019 Vincent Quatrevieux |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
package fr.quatrevieux.araknemu.core.di; |
|
21
|
|
|
|
|
22
|
|
|
import fr.quatrevieux.araknemu.core.di.item.CachedItem; |
|
23
|
|
|
import fr.quatrevieux.araknemu.core.di.item.ContainerItem; |
|
24
|
|
|
import fr.quatrevieux.araknemu.core.di.item.FactoryItem; |
|
25
|
|
|
import fr.quatrevieux.araknemu.core.di.item.ValueItem; |
|
26
|
|
|
import org.checkerframework.checker.nullness.qual.NonNull; |
|
27
|
|
|
|
|
28
|
|
|
import java.util.HashMap; |
|
29
|
|
|
import java.util.Map; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Container implementation using {@link fr.quatrevieux.araknemu.core.di.item.ContainerItem} |
|
33
|
|
|
*/ |
|
34
|
1 |
|
public final class ItemPoolContainer implements Container { |
|
35
|
1 |
|
private final Map<Class, ContainerItem> items = new HashMap<>(); |
|
36
|
|
|
|
|
37
|
|
|
@Override |
|
38
|
|
|
public <@NonNull T> T get(Class<T> type) throws ContainerException { |
|
39
|
1 |
|
final ContainerItem<@NonNull T> item = (ContainerItem<@NonNull T>) items.get(type); |
|
40
|
|
|
|
|
41
|
1 |
|
if (item == null) { |
|
42
|
1 |
|
throw new ItemNotFoundException(type); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
return item.value(this); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
@Override |
|
49
|
|
|
public boolean has(Class type) { |
|
50
|
1 |
|
return items.containsKey(type); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
@Override |
|
54
|
|
|
public void register(ContainerModule module) { |
|
55
|
1 |
|
module.configure(new Configurator()); |
|
56
|
1 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get pool configurator |
|
60
|
|
|
*/ |
|
61
|
|
|
public Configurator configurator() { |
|
62
|
1 |
|
return new Configurator(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Set a new item to the container |
|
67
|
|
|
*/ |
|
68
|
|
|
private void set(ContainerItem item) { |
|
69
|
1 |
|
items.put(item.type(), item); |
|
70
|
1 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Configuration class for {@link ItemPoolContainer} |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public final class Configurator implements ContainerConfigurator { |
|
76
|
|
|
@Override |
|
77
|
|
|
public Configurator set(Object object) { |
|
78
|
1 |
|
ItemPoolContainer.this.set(new ValueItem<>(object)); |
|
79
|
|
|
|
|
80
|
1 |
|
return this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
@Override |
|
84
|
|
|
public <@NonNull T> Configurator set(Class<T> type, @NonNull T object) { |
|
85
|
1 |
|
ItemPoolContainer.this.set(new ValueItem<>(type, object)); |
|
86
|
|
|
|
|
87
|
1 |
|
return this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
@Override |
|
91
|
|
|
public <@NonNull T> Configurator factory(Class<T> type, FactoryItem.Factory<T> factory) { |
|
92
|
1 |
|
ItemPoolContainer.this.set(new FactoryItem<>(type, factory)); |
|
93
|
|
|
|
|
94
|
1 |
|
return this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
@Override |
|
98
|
|
|
public <@NonNull T> Configurator persist(Class<T> type, FactoryItem.Factory<T> factory) { |
|
99
|
1 |
|
ItemPoolContainer.this.set(new CachedItem<>( |
|
100
|
|
|
new FactoryItem<>(type, factory) |
|
101
|
|
|
)); |
|
102
|
|
|
|
|
103
|
1 |
|
return this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Register an item to the container |
|
108
|
|
|
* |
|
109
|
|
|
* @param item Item to register |
|
110
|
|
|
* |
|
111
|
|
|
* @return this |
|
112
|
|
|
*/ |
|
113
|
|
|
public Configurator item(ContainerItem item) { |
|
114
|
|
|
ItemPoolContainer.this.set(item); |
|
115
|
|
|
|
|
116
|
|
|
return this; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|