fr.arakne.utils.maps.sight.SameXLineOfSightIterator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 34
ccs 11
cts 11
cp 1
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hasNext() 0 3 1
A getCurrentCell() 0 2 1
A next() 0 5 1
A SameXLineOfSightIterator(BattlefieldSight,CoordinateCell,CoordinateCell) 0 7 2
1
/*
2
 * This file is part of ArakneUtils.
3
 *
4
 * ArakneUtils 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
 * ArakneUtils 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 ArakneUtils.  If not, see <https://www.gnu.org/licenses/>.
16
 *
17
 * Copyright (c) 2017-2021 Vincent Quatrevieux
18
 */
19
20
package fr.arakne.utils.maps.sight;
21
22
import fr.arakne.utils.maps.BattlefieldCell;
23
import fr.arakne.utils.maps.CoordinateCell;
24
import org.checkerframework.checker.nullness.qual.NonNull;
25
26
import java.util.Iterator;
27
28
/**
29
 * Line of sight iterator for cells with same X coordinate
30
 */
31
final class SameXLineOfSightIterator<C extends @NonNull BattlefieldCell> implements Iterator<C> {
32
    private final BattlefieldSight<C> battlefield;
33
    private final CoordinateCell<C> source;
34
    private final CoordinateCell<C> target;
35
    private final int yDirection;
36
37
    private int currentY;
38
39 1
    public SameXLineOfSightIterator(BattlefieldSight<C> battlefield, CoordinateCell<C> source, CoordinateCell<C> target) {
40 1
        this.battlefield = battlefield;
41 1
        this.source = source;
42 1
        this.target = target;
43
44 1
        yDirection = source.y() > target.y() ? -1 : 1;
45 1
        currentY = source.y();
46 1
    }
47
48
    @Override
49
    public boolean hasNext() {
50 1
        return target.y() != currentY;
51
    }
52
53
    @Override
54
    public C next() {
0 ignored issues
show
Bug introduced by
Iteration beyond the the end of a collection should throw a NoSuchElementException.
Loading history...
55 1
        currentY += yDirection;
56
57 1
        return getCurrentCell();
58
    }
59
60
    /**
61
     * @return The cell on the current coordinates
62
     */
63
    private C getCurrentCell() {
64 1
        return battlefield.getCellByCoordinates(source.x(), currentY);
65
    }
66
}
67