listeners()   C
last analyzed

Complexity

Conditions 10

Size

Total Lines 54
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 10.2537

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 33
dl 0
loc 54
ccs 19
cts 22
cp 0.8636
crap 10.2537
rs 5.9999
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
fr.quatrevieux.araknemu.game.listener.player.exchange.bank.SaveBank.$Listener$.on(ObjectDeleted) 0 9 ?
fr.quatrevieux.araknemu.game.listener.player.exchange.bank.SaveBank.$Listener$.event() 0 3 ?
fr.quatrevieux.araknemu.game.listener.player.exchange.bank.SaveBank.$Listener$.on(ObjectAdded) 0 9 ?
fr.quatrevieux.araknemu.game.listener.player.exchange.bank.SaveBank.$Listener$.on(ObjectQuantityChanged) 0 9 ?

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like fr.quatrevieux.araknemu.game.listener.player.exchange.bank.SaveBank.listeners() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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.listener.player.exchange.bank;
21
22
import fr.quatrevieux.araknemu.core.event.EventsSubscriber;
23
import fr.quatrevieux.araknemu.core.event.Listener;
24
import fr.quatrevieux.araknemu.data.living.repository.account.BankItemRepository;
25
import fr.quatrevieux.araknemu.game.account.bank.BankEntry;
26
import fr.quatrevieux.araknemu.game.item.inventory.event.ObjectAdded;
27
import fr.quatrevieux.araknemu.game.item.inventory.event.ObjectDeleted;
28
import fr.quatrevieux.araknemu.game.item.inventory.event.ObjectQuantityChanged;
29
30
/**
31
 * Save the bank on changes
32
 */
33
public final class SaveBank implements EventsSubscriber {
34
    private final BankItemRepository repository;
35
36 1
    public SaveBank(BankItemRepository repository) {
37 1
        this.repository = repository;
38 1
    }
39
40
    @Override
41
    public Listener[] listeners() {
42 1
        return new Listener[] {
43 1
            new Listener<ObjectAdded>() {
44
                @Override
45
                public void on(ObjectAdded event) {
46 1
                    if (!(event.entry() instanceof BankEntry)) {
47
                        return;
48
                    }
49
50 1
                    final BankEntry entry = (BankEntry) event.entry();
51
52 1
                    repository.add(entry.entity());
53 1
                }
54
55
                @Override
56
                public Class<ObjectAdded> event() {
57 1
                    return ObjectAdded.class;
58
                }
59
            },
60
61 1
            new Listener<ObjectDeleted>() {
62
                @Override
63
                public void on(ObjectDeleted event) {
64 1
                    if (!(event.entry() instanceof BankEntry)) {
65
                        return;
66
                    }
67
68 1
                    final BankEntry entry = (BankEntry) event.entry();
69
70 1
                    repository.delete(entry.entity());
71 1
                }
72
73
                @Override
74
                public Class<ObjectDeleted> event() {
75 1
                    return ObjectDeleted.class;
76
                }
77
            },
78
79 1
            new Listener<ObjectQuantityChanged>() {
80
                @Override
81
                public void on(ObjectQuantityChanged event) {
82 1
                    if (!(event.entry() instanceof BankEntry)) {
83
                        return;
84
                    }
85
86 1
                    final BankEntry entry = (BankEntry) event.entry();
87
88 1
                    repository.update(entry.entity());
89 1
                }
90
91
                @Override
92
                public Class<ObjectQuantityChanged> event() {
93 1
                    return ObjectQuantityChanged.class;
94
                }
95
            },
96
        };
97
    }
98
}
99