Add stroke and element support to kanji result
app/src/main/java/eu/lepiller/nani/dictionary/KanjiDict.java
48 | 48 | List<String> on = getHuffmanStringList(file, readingHuffman); | |
49 | 49 | List<String> nanori = getHuffmanStringList(file, readingHuffman); | |
50 | 50 | ||
51 | - | return new KanjiResult(kanji, stroke, meanings, kun, on, nanori); | |
51 | + | return new KanjiResult(kanji, stroke, meanings, kun, on, nanori, null, null); | |
52 | 52 | } | |
53 | 53 | ||
54 | 54 | KanjiResult search(final String kanji) throws IncompatibleFormatException { |
app/src/main/java/eu/lepiller/nani/result/KanjiResult.java
2 | 2 | ||
3 | 3 | import java.util.ArrayList; | |
4 | 4 | import java.util.List; | |
5 | + | import java.util.Objects; | |
5 | 6 | ||
6 | 7 | public class KanjiResult { | |
7 | 8 | public static class Sense { | |
… | |||
19 | 20 | return lang; | |
20 | 21 | } | |
21 | 22 | } | |
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 | + | ||
22 | 51 | 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<>(); | |
28 | 59 | ||
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) { | |
30 | 62 | this.kanji = kanji; | |
31 | 63 | 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); | |
36 | 76 | } | |
37 | 77 | ||
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)) | |
47 | 81 | continue; | |
48 | - | res.add(s); | |
82 | + | a.add(t); | |
49 | 83 | } | |
50 | - | return res; | |
51 | 84 | } | |
52 | 85 | ||
53 | 86 | public void merge(KanjiResult other) { | |
54 | 87 | 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 | + | } | |
58 | 101 | } | |
59 | 102 | ||
60 | 103 | public List<Sense> getSenses() { |