|
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.data.world.repository.implementation.local; |
|
21
|
|
|
|
|
22
|
|
|
import fr.quatrevieux.araknemu.core.dbal.repository.RepositoryException; |
|
23
|
|
|
import fr.quatrevieux.araknemu.data.world.entity.environment.npc.NpcTemplate; |
|
24
|
|
|
import fr.quatrevieux.araknemu.data.world.repository.environment.npc.NpcTemplateRepository; |
|
25
|
|
|
|
|
26
|
|
|
import java.util.Collection; |
|
27
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
28
|
|
|
import java.util.concurrent.ConcurrentMap; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Cache repository for {@link NpcTemplate} |
|
32
|
|
|
*/ |
|
33
|
|
View Code Duplication |
public final class NpcTemplateRepositoryCache implements NpcTemplateRepository { |
|
|
|
|
|
|
34
|
|
|
private final NpcTemplateRepository repository; |
|
35
|
|
|
|
|
36
|
1 |
|
private final ConcurrentMap<Integer, NpcTemplate> cacheById = new ConcurrentHashMap<>(); |
|
37
|
|
|
|
|
38
|
1 |
|
public NpcTemplateRepositoryCache(NpcTemplateRepository repository) { |
|
39
|
1 |
|
this.repository = repository; |
|
40
|
1 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
@Override |
|
43
|
|
|
public NpcTemplate get(int id) { |
|
44
|
1 |
|
return cacheById.computeIfAbsent(id, repository::get); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
@Override |
|
48
|
|
|
public Collection<NpcTemplate> all() { |
|
49
|
1 |
|
final Collection<NpcTemplate> loaded = repository.all(); |
|
50
|
|
|
|
|
51
|
1 |
|
for (NpcTemplate template : loaded) { |
|
52
|
1 |
|
cacheById.put(template.id(), template); |
|
53
|
1 |
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
return loaded; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
@Override |
|
59
|
|
|
public void initialize() throws RepositoryException { |
|
60
|
1 |
|
repository.initialize(); |
|
61
|
1 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
@Override |
|
64
|
|
|
public void destroy() throws RepositoryException { |
|
65
|
1 |
|
repository.destroy(); |
|
66
|
1 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
@Override |
|
69
|
|
|
public NpcTemplate get(NpcTemplate entity) throws RepositoryException { |
|
70
|
1 |
|
return get(entity.id()); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
@Override |
|
74
|
|
|
public boolean has(NpcTemplate entity) throws RepositoryException { |
|
75
|
1 |
|
return |
|
76
|
1 |
|
cacheById.containsKey(entity.id()) |
|
77
|
1 |
|
|| repository.has(entity) |
|
78
|
|
|
; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|