Add stroke and element support to kanji result

Julien LepillerSun Jun 26 18:59:04+0200 2022

5fe5f1f

Add stroke and element support to kanji result

app/src/main/java/eu/lepiller/nani/dictionary/KanjiDict.java

4848
        List<String> on = getHuffmanStringList(file, readingHuffman);
4949
        List<String> nanori = getHuffmanStringList(file, readingHuffman);
5050
51-
        return new KanjiResult(kanji, stroke, meanings, kun, on, nanori);
51+
        return new KanjiResult(kanji, stroke, meanings, kun, on, nanori, null, null);
5252
    }
5353
5454
    KanjiResult search(final String kanji) throws IncompatibleFormatException {

app/src/main/java/eu/lepiller/nani/result/KanjiResult.java

22
33
import java.util.ArrayList;
44
import java.util.List;
5+
import java.util.Objects;
56
67
public class KanjiResult {
78
    public static class Sense {

1920
            return lang;
2021
        }
2122
    }
23+
24+
    public static class Stroke {
25+
        private final String command, posX, posY;
26+
27+
        public Stroke(String command, String posX, String posY) {
28+
            this.command = command;
29+
            this.posX = posX;
30+
            this.posY = posY;
31+
        }
32+
33+
        @Override
34+
        public int hashCode() {
35+
            return Objects.hash(command, posX, posY);
36+
        }
37+
38+
        public String getCommand() {
39+
            return command;
40+
        }
41+
42+
        public String getPosX() {
43+
            return posX;
44+
        }
45+
46+
        public String getPosY() {
47+
            return posY;
48+
        }
49+
    }
50+
2251
    private final String kanji;
23-
    private final int stroke;
24-
    private final List<Sense> senses;
25-
    private List<String> on;
26-
    private final List<String> kun;
27-
    private final List<String> nanori;
52+
    private int stroke;
53+
    private final List<Sense> senses = new ArrayList<>();
54+
    private final List<String> on = new ArrayList<>();
55+
    private final List<String> kun = new ArrayList<>();
56+
    private final List<String> nanori = new ArrayList<>();
57+
    private final List<String> elements = new ArrayList<>();
58+
    private final List<Stroke> strokes = new ArrayList<>();
2859
29-
    public KanjiResult(String kanji, int stroke, List<Sense> senses, List<String> kun, List<String> on, List<String> nanori) {
60+
    public KanjiResult(String kanji, int stroke, List<Sense> senses, List<String> kun, List<String> on, List<String> nanori,
61+
                       List<String> elements, List<Stroke> strokes) {
3062
        this.kanji = kanji;
3163
        this.stroke = stroke;
32-
        this.senses = senses;
33-
        this.kun = kun;
34-
        this.on = on;
35-
        this.nanori = nanori;
64+
        if(senses != null)
65+
            this.senses.addAll(senses);
66+
        if(kun != null)
67+
            this.kun.addAll(kun);
68+
        if(on != null)
69+
            this.on.addAll(on);
70+
        if(nanori != null)
71+
            this.nanori.addAll(nanori);
72+
        if(elements != null)
73+
            this.elements.addAll(elements);
74+
        if(strokes != null)
75+
            this.strokes.addAll(strokes);
3676
    }
3777
38-
    public List<String> addUniq(List<String> a, List<String> b) {
39-
        List<String> res = new ArrayList<>();
40-
        for(String s: a) {
41-
            if(res.contains(s))
42-
                continue;
43-
            res.add(s);
44-
        }
45-
        for(String s: b) {
46-
            if(res.contains(s))
78+
    public <T> void addUniq(List<T> a, List<T> b) {
79+
        for(T t: b) {
80+
            if(a.contains(t))
4781
                continue;
48-
            res.add(s);
82+
            a.add(t);
4983
        }
50-
        return res;
5184
    }
5285
5386
    public void merge(KanjiResult other) {
5487
        this.senses.addAll(other.senses);
55-
        this.on = addUniq(this.on, other.on);
56-
        this.on = addUniq(this.kun, other.kun);
57-
        this.on = addUniq(this.nanori, other.nanori);
88+
        addUniq(this.on, other.on);
89+
        addUniq(this.kun, other.kun);
90+
        addUniq(this.nanori, other.nanori);
91+
        addUniq(this.elements, other.elements);
92+
93+
        this.senses.addAll(other.senses);
94+
        if(this.stroke < 0)
95+
            this.stroke = other.stroke;
96+
97+
        // Arbitrarily settle for the first one that has stroke order information, we can't mix multiple orders
98+
        if(this.strokes.isEmpty()) {
99+
            this.strokes.addAll(other.strokes);
100+
        }
58101
    }
59102
60103
    public List<Sense> getSenses() {