fr.quatrevieux.araknemu.game.item.factory.DefaultItemFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 34
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A retrieve(ItemTemplate,ItemType,GameItemSet,List) 0 9 2
A type() 0 3 1
A create(ItemTemplate,ItemType,GameItemSet,boolean) 0 9 2
A DefaultItemFactory(ItemFactory) 0 3 2
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.game.item.factory;
21
22
import fr.quatrevieux.araknemu.data.value.ItemTemplateEffectEntry;
23
import fr.quatrevieux.araknemu.data.world.entity.item.ItemTemplate;
24
import fr.quatrevieux.araknemu.data.world.entity.item.ItemType;
25
import fr.quatrevieux.araknemu.game.item.GameItemSet;
26
import fr.quatrevieux.araknemu.game.item.Item;
27
import fr.quatrevieux.araknemu.game.item.SuperType;
28
import org.checkerframework.checker.nullness.qual.Nullable;
29
30
import java.util.EnumMap;
31
import java.util.List;
32
import java.util.Map;
33
import java.util.NoSuchElementException;
34
35
/**
36
 * Default item factory implementation
37
 */
38
public final class DefaultItemFactory implements ItemFactory {
39 1
    private final Map<SuperType, ItemFactory> factories = new EnumMap<>(SuperType.class);
40
41 1
    public DefaultItemFactory(ItemFactory... factories) {
42 1
        for (ItemFactory factory : factories) {
43 1
            this.factories.put(factory.type(), factory);
44
        }
45 1
    }
46
47
    @Override
48
    public Item create(ItemTemplate template, ItemType type, @Nullable GameItemSet set, boolean maximize) {
49 1
        final ItemFactory factory = factories.get(type.superType());
50
51 1
        if (factory == null) {
52 1
            throw new NoSuchElementException("Invalid type " + type);
53
        }
54
55 1
        return factory.create(template, type, set, maximize);
56
    }
57
58
    @Override
59
    public Item retrieve(ItemTemplate template, ItemType type, @Nullable GameItemSet set, List<ItemTemplateEffectEntry> effects) {
60 1
        final ItemFactory factory = factories.get(type.superType());
61
62 1
        if (factory == null) {
63 1
            throw new NoSuchElementException("Invalid type " + type);
64
        }
65
66 1
        return factory.retrieve(template, type, set, effects);
67
    }
68
69
    @Override
70
    public SuperType type() {
71
        throw new UnsupportedOperationException();
72
    }
73
}
74