fr.quatrevieux.araknemu.data.world.repository.implementation.local.NpcTemplateRepositoryCache   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 100 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 45
loc 45
ccs 18
cts 18
cp 1
rs 10
c 1
b 0
f 0
eloc 28
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A all() 9 9 2
A get(int) 3 3 1
A initialize() 3 3 1
A has(NpcTemplate) 5 5 1
A destroy() 3 3 1
A get(NpcTemplate) 3 3 1
A NpcTemplateRepositoryCache(NpcTemplateRepository) 2 2 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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