iterator()   A
last analyzed

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
eloc 3
c 1
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
1
/*
2
 * This file is part of ArakneLangLoader.
3
 *
4
 * ArakneLangLoader 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
 * ArakneLangLoader 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 ArakneLangLoader.  If not, see <https://www.gnu.org/licenses/>.
16
 *
17
 * Copyright (c) 2020 Vincent Quatrevieux
18
 */
19
20
package fr.arakne.swflangloader.lang.hints;
21
22
import fr.arakne.swflangloader.loader.AbstractSwfFile;
23
import fr.arakne.swflangloader.loader.SwfFileLoader;
24
import fr.arakne.swflangloader.parser.mapper.MapperHydrator;
25
import fr.arakne.swflangloader.parser.mapper.SwfVariable;
26
27
import java.io.File;
28
import java.io.IOException;
29
import java.net.URL;
30
import java.util.*;
31
import java.util.function.Consumer;
32
import java.util.stream.Collectors;
33
import java.util.stream.Stream;
34
import java.util.stream.StreamSupport;
35
36
/**
37
 * File storing map hints (i.e. point of interests like Zaap, banks...)
38
 * This object is iterable : you can access to hints using a for loop
39
 */
40
final public class HintsFile extends AbstractSwfFile implements Iterable<Hint> {
41 1
    final static private MapperHydrator<HintsFile> HYDRATOR = MapperHydrator.parseAnnotations(HintsFile.class);
42
43 1
    @SwfVariable("HIC")
44
    final private Map<Integer, HintCategory> categories = new HashMap<>();
45
46
    @SwfVariable("HI")
47
    private Hint[] hints;
48
49 1
    public HintsFile(URL file, SwfFileLoader loader) throws IOException, InterruptedException {
50 1
        loader.load(file, this, HYDRATOR);
51 1
        init();
52 1
    }
53
54
    public HintsFile(URL file) throws IOException, InterruptedException {
55 1
        this(file, new SwfFileLoader());
56 1
    }
57
58
    public HintsFile(File file) throws IOException, InterruptedException {
59
        this(file.toURI().toURL(), new SwfFileLoader());
60
    }
61
62
    /**
63
     * Get a hint category by its id
64
     *
65
     * @param id The color id
66
     *
67
     * @return The color or null is not found
68
     */
69
    public HintCategory category(int id) {
70 1
        return categories.get(id);
71
    }
72
73
    /**
74
     * @return Stream of all hints
75
     */
76
    public Stream<Hint> stream() {
77 1
        return StreamSupport.stream(spliterator(), false);
78
    }
79
80
    /**
81
     * Get all hints of the given type
82
     *
83
     * @param type The hint type id. One of the Hint.TYPE_* constant
84
     *
85
     * @return All matching hints
86
     */
87
    public Collection<Hint> byType(int type) {
88 1
        return stream().filter(hint -> hint.is(type)).collect(Collectors.toList());
89
    }
90
91
    @Override
92
    public Iterator<Hint> iterator() {
93 1
        return stream().iterator();
94
    }
95
96
    @Override
97
    public Spliterator<Hint> spliterator() {
98 1
        return Arrays.spliterator(hints);
99
    }
100
101
    @Override
102
    public void forEach(Consumer<? super Hint> action) {
103 1
        for (Hint hint : hints) {
104 1
            action.accept(hint);
105
        }
106 1
    }
107
108
    private void init() {
109 1
        categories.forEach((id, color) -> color.setId(id));
110 1
        forEach(hint -> hint.setHints(this));
111 1
    }
112
}
113