Passed
Push — master ( d91f03...280472 )
by Vincent
04:06
created

fr.arakne.utils.maps.path.Path.iterator()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
eloc 3
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-2020 Vincent Quatrevieux
18
 */
19
20
package fr.arakne.utils.maps.path;
21
22
import fr.arakne.utils.maps.MapCell;
23
24
import java.util.ArrayList;
25
import java.util.Collection;
26
import java.util.Iterator;
27
import java.util.List;
28
import java.util.function.Predicate;
29
30
/**
31
 * Path for dofus map
32
 * @param <C> The cell type
33
 */
34
final public class Path<C extends MapCell> implements Collection<PathStep<C>> {
35
    final private Decoder<C> decoder;
36
    final private List<PathStep<C>> steps;
37
38
    /**
39
     * @param decoder The decoder instance
40
     * @param steps The list of steps
41
     */
42 1
    public Path(Decoder<C> decoder, List<PathStep<C>> steps) {
43 1
        this.decoder = decoder;
44 1
        this.steps = steps;
45 1
    }
46
47
    /**
48
     * Create an empty path
49
     *
50
     * @param decoder The decoder instance
51
     */
52
    public Path(Decoder<C> decoder) {
53 1
        this(decoder, new ArrayList<>());
54 1
    }
55
56
    /**
57
     * Get one step
58
     *
59
     * @param step The step number. The step zero is the current cell, The step (size - 1) is the last step (target)
60
     * @return The required step
61
     */
62
    public PathStep<C> get(int step) {
63 1
        return steps.get(step);
64
    }
65
66
    /**
67
     * Get the first step of the path
68
     *
69
     * @return The first step
70
     * @see Path#start() For get the start cell
71
     */
72
    public PathStep<C> first() {
73 1
        return steps.get(0);
74
    }
75
76
    /**
77
     * Get the last step
78
     *
79
     * @return The last step
80
     * @see Path#target() For get the last cell
81
     */
82
    public PathStep<C> last() {
83 1
        return steps.get(steps.size() - 1);
84
    }
85
86
    /**
87
     * Get the start cell
88
     *
89
     * @return The start cell
90
     */
91
    public C start() {
92 1
        return first().cell();
93
    }
94
95
    /**
96
     * Get the last cell
97
     *
98
     * @return The end cell
99
     */
100
    public C target() {
101 1
        return last().cell();
102
    }
103
104
    /**
105
     * Encode the path to string
106
     *
107
     * @return The encoded path
108
     */
109
    public String encode() {
110 1
        return decoder.encode(this);
111
    }
112
113
    /**
114
     * Encode the path to string, including the start cell
115
     *
116
     * @return The encoded path
117
     * @see Decoder#encodeWithStartCell(Path)
118
     */
119
    public String encodeWithStartCell() {
120 1
        return decoder.encodeWithStartCell(this);
121
    }
122
123
    /**
124
     * Keep the path steps while the condition is valid
125
     * The path will be stop when an invalid step is found
126
     *
127
     * @param condition The condition to apply on all steps. If returns false, the path will be stopped
128
     *
129
     * @return The new path instance
130
     */
131
    public Path<C> keepWhile(Predicate<PathStep<C>> condition) {
132 1
        Path<C> newPath = new Path<>(decoder, new ArrayList<>(size()));
133
134 1
        for (PathStep<C> step : steps) {
135 1
            if (!condition.test(step)) {
136 1
                break;
137
            }
138
139 1
            newPath.add(step);
140 1
        }
141
142 1
        return newPath;
143
    }
144
145
    /**
146
     * Truncate the path, to the new size
147
     *
148
     * @param newSize The required size
149
     * @return The new path instance
150
     */
151
    public Path<C> truncate(int newSize) {
152 1
        if (newSize > size()) {
153 1
            return this;
154
        }
155
156 1
        return new Path<>(decoder, steps.subList(0, newSize));
157
    }
158
159
    @Override
160
    public int size() {
161 1
        return steps.size();
162
    }
163
164
    @Override
165
    public boolean isEmpty() {
166 1
        return steps.isEmpty();
167
    }
168
169
    @Override
170
    public boolean contains(Object o) {
171
        return steps.contains(o);
172
    }
173
174
    @Override
175
    public Iterator<PathStep<C>> iterator() {
176 1
        return steps.iterator();
177
    }
178
179
    @Override
180
    public Object[] toArray() {
181
        return steps.toArray();
182
    }
183
184
    @Override
185
    public <T> T[] toArray(T[] a) {
186
        return steps.toArray(a);
187
    }
188
189
    @Override
190
    public boolean add(PathStep<C> step) {
191 1
        return steps.add(step);
192
    }
193
194
    @Override
195
    public boolean remove(Object o) {
196
        return steps.remove(o);
197
    }
198
199
    @Override
200
    public boolean containsAll(Collection<?> c) {
201
        return steps.containsAll(c);
202
    }
203
204
    @Override
205
    public boolean addAll(Collection<? extends PathStep<C>> c) {
206
        return steps.addAll(c);
207
    }
208
209
    @Override
210
    public boolean removeAll(Collection<?> c) {
211
        return steps.removeAll(c);
212
    }
213
214
    @Override
215
    public boolean retainAll(Collection<?> c) {
216
        return steps.retainAll(c);
217
    }
218
219
    @Override
220
    public void clear() {
221
        steps.clear();
222
    }
223
}
224