Passed
Push — master ( 08cd0d...9eabdf )
by Vincent
07:15
created

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

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
25
import java.util.Iterator;
26
27
/**
28
 * Line of sight iterator for cells with same X coordinate
29
 */
30
final class SameXLineOfSightIterator<C extends BattlefieldCell> implements Iterator<C> {
31
    private final BattlefieldSight<C> battlefield;
32
    private final CoordinateCell<C> source;
33
    private final CoordinateCell<C> target;
34
    private final int yDirection;
35
36
    private int currentY;
37
38 1
    public SameXLineOfSightIterator(BattlefieldSight<C> battlefield, CoordinateCell<C> source, CoordinateCell<C> target) {
39 1
        this.battlefield = battlefield;
40 1
        this.source = source;
41 1
        this.target = target;
42
43 1
        yDirection = source.y() > target.y() ? -1 : 1;
44 1
        currentY = source.y();
45 1
    }
46
47
    @Override
48
    public boolean hasNext() {
49 1
        return target.y() != currentY;
50
    }
51
52
    @Override
53
    public C next() {
0 ignored issues
show
Bug introduced by
Iteration beyond the the end of a collection should throw a NoSuchElementException.
Loading history...
54 1
        currentY += yDirection;
55
56 1
        return getCurrentCell();
57
    }
58
59
    /**
60
     * @return The cell on the current coordinates
61
     */
62
    private C getCurrentCell() {
63 1
        return battlefield.getCellByCoordinates(source.x(), currentY);
64
    }
65
}
66