1
|
|
|
/* |
2
|
|
|
* This file is part of Araknemu. |
3
|
|
|
* |
4
|
|
|
* Araknemu 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
|
|
|
* Araknemu 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 Araknemu. If not, see <https://www.gnu.org/licenses/>. |
16
|
|
|
* |
17
|
|
|
* Copyright (c) 2017-2020 Vincent Quatrevieux |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
package fr.quatrevieux.araknemu.data.world.transformer; |
21
|
|
|
|
22
|
|
|
import fr.arakne.utils.encoding.Base64; |
23
|
|
|
import fr.quatrevieux.araknemu.data.transformer.Transformer; |
24
|
|
|
import fr.quatrevieux.araknemu.data.transformer.TransformerException; |
25
|
|
|
import fr.quatrevieux.araknemu.util.Asserter; |
26
|
|
|
import org.apache.commons.lang3.StringUtils; |
27
|
|
|
import org.checkerframework.checker.index.qual.NonNegative; |
28
|
|
|
import org.checkerframework.checker.nullness.qual.PolyNull; |
29
|
|
|
|
30
|
|
|
import java.util.Arrays; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Transform map fight places |
34
|
|
|
*/ |
35
|
|
|
public final class FightPlacesTransformer implements Transformer<@NonNegative int[][]> { |
36
|
|
|
@Override |
37
|
|
|
public @PolyNull String serialize(@NonNegative int @PolyNull [][] value) { |
38
|
1 |
|
throw new UnsupportedOperationException(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
@Override |
42
|
|
|
@SuppressWarnings("return") // returned values are non negative |
43
|
|
|
public @NonNegative int[][] unserialize(@PolyNull String serialize) { |
44
|
|
|
if (serialize == null) { |
45
|
|
|
return new int[0][]; |
46
|
1 |
|
} |
47
|
1 |
|
|
48
|
|
|
try { |
49
|
|
|
return Arrays.stream(StringUtils.split(serialize, "|", 2)) |
50
|
|
|
.map(this::parseTeamPlaces) |
51
|
1 |
|
.toArray(int[][]::new) |
52
|
1 |
|
; |
53
|
1 |
|
} catch (RuntimeException e) { |
54
|
|
|
throw new TransformerException("Cannot parse places '" + serialize + "'", e); |
55
|
1 |
|
} |
56
|
1 |
|
} |
57
|
|
|
|
58
|
|
|
@SuppressWarnings("argument") // String indexes are safe |
59
|
|
|
private @NonNegative int[] parseTeamPlaces(String places) { |
60
|
|
|
final @NonNegative int[] cells = new int[places.length() / 2]; |
61
|
1 |
|
|
62
|
|
|
for (int i = 0; i < cells.length; i++) { |
63
|
1 |
|
cells[i] = Asserter.assertNonNegative(Base64.decode(places.substring(2 * i, 2 * i + 2))); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
return cells; |
67
|
1 |
|
} |
68
|
|
|
} |
69
|
|
|
|