equals(Object)   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 3
crap 3
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.spell.effect.target;
21
22
import fr.quatrevieux.araknemu.game.fight.fighter.FighterData;
23
import fr.quatrevieux.araknemu.game.fight.team.Team;
24
import org.checkerframework.checker.nullness.qual.Nullable;
25
26
import java.util.Objects;
27
28
/**
29
 * Implementation of effect target using spell flags
30
 */
31
public final class SpellEffectTarget implements EffectTarget {
32 1
    public static final SpellEffectTarget DEFAULT = new SpellEffectTarget(0);
33
34
    public static final int NOT_TEAM    = 1;
35
    public static final int NOT_SELF    = 2;
36
    public static final int NOT_ENEMY   = 4;
37
    public static final int ONLY_INVOC  = 8;
38
    public static final int NOT_INVOC   = 16;
39
    public static final int ONLY_CASTER = 32;
40
41
    private final int flags;
42
43 1
    public SpellEffectTarget(int flags) {
44 1
        this.flags = flags;
45 1
    }
46
47
    @Override
48
    public boolean onlyCaster() {
49 1
        return check(ONLY_CASTER);
50
    }
51
52
    @Override
53
    public boolean test(FighterData caster, FighterData fighter) {
54 1
        return checkSelf(caster, fighter)
55 1
            && checkTeam(caster.team(), fighter.team())
56 1
            && checkInvocation(fighter)
57
        ;
58
    }
59
60
    private boolean check(int flag) {
61 1
        return (flags & flag) == flag;
62
    }
63
64
    @Override
65
    public boolean equals(@Nullable Object obj) {
66 1
        if (obj == this) {
67 1
            return true;
68
        }
69
70 1
        if (obj == null) {
71 1
            return false;
72
        }
73
74 1
        return getClass().equals(obj.getClass()) && flags == ((SpellEffectTarget) obj).flags;
75
    }
76
77
    @Override
78
    public int hashCode() {
79 1
        return Objects.hash(flags);
80
    }
81
82
    private boolean checkTeam(Team<?> casterTeam, Team<?> targetTeam) {
83 1
        if (check(NOT_TEAM) && casterTeam.equals(targetTeam)) {
84 1
            return false;
85
        }
86
87 1
        if (check(NOT_ENEMY) && !casterTeam.equals(targetTeam)) {
88 1
            return false;
89
        }
90
91 1
        return true;
92
    }
93
94
    private boolean checkInvocation(FighterData target) {
95 1
        final boolean isInvocation = target.invoked();
96
97 1
        if (check(ONLY_INVOC) && !isInvocation) {
98 1
            return false;
99
        }
100
101 1
        if (check(NOT_INVOC) && isInvocation) {
102 1
            return false;
103
        }
104
105 1
        return true;
106
    }
107
108
    private boolean checkSelf(FighterData caster, FighterData target) {
109 1
        return !check(NOT_SELF) || !caster.equals(target);
110
    }
111
}
112