assoc(String)   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 2
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;
21
22
import com.google.gson.JsonElement;
23
import fr.arakne.swflangloader.loader.AbstractSwfFile;
24
import fr.arakne.swflangloader.loader.SwfFileLoader;
25
import fr.arakne.swflangloader.parser.mapper.MapperHydrator;
26
import fr.arakne.swflangloader.parser.mapper.SwfDefault;
27
28
import java.io.IOException;
29
import java.net.URL;
30
import java.util.Collections;
31
import java.util.HashMap;
32
import java.util.Map;
33
import java.util.NoSuchElementException;
34
35
/**
36
 * Simple lang file structure
37
 * Forward all variable to a default field
38
 */
39 1
public class BaseLangFile extends AbstractSwfFile implements JsonMapGetter {
40 1
    final static private MapperHydrator<BaseLangFile> HYDRATOR = MapperHydrator.parseAnnotations(BaseLangFile.class);
41
42 1
    @SwfDefault
43
    final protected Map<String, Object> values = new HashMap<>();
44
45
    final static public class AssocVariable implements JsonMapGetter {
46
        final private String varName;
47
        final private Map<String, JsonElement> assoc;
48
49 1
        public AssocVariable(String varName, Map<String, JsonElement> assoc) {
50 1
            this.varName = varName;
51 1
            this.assoc = assoc;
52 1
        }
53
54
        @Override
55
        public JsonElement json(String variable) {
56 1
            if (!assoc.containsKey(variable)) {
57
                throw new NoSuchElementException("The variable " + varName + "[" + variable + "] is not found");
58
            }
59
60 1
            return assoc.get(variable);
61
        }
62
63
        /**
64
         * @return All elements
65
         */
66
        public Map<String, JsonElement> all() {
67 1
            return Collections.unmodifiableMap(assoc);
68
        }
69
    }
70
71
    /**
72
     * Get a raw JSON element value
73
     *
74
     * @param variable The variable name
75
     *
76
     * @return The variable value
77
     */
78
    final public JsonElement json(String variable) {
79 1
        final Object value = values.get(variable);
80
81 1
        if (value instanceof JsonElement) {
82 1
            return (JsonElement) value;
83
        }
84
85 1
        throw new NoSuchElementException("Variable " + variable + " is not found or not a valid JSON value");
86
    }
87
88
    /**
89
     * Get an associative variable
90
     *
91
     * @param variableName The variable name
92
     *
93
     * @return The associative value
94
     */
95
    @SuppressWarnings("unchecked")
96
    final public AssocVariable assoc(String variableName) {
97 1
        final Object value = values.get(variableName);
98
99 1
        if (value instanceof Map) {
100 1
            return new AssocVariable(variableName, (Map<String, JsonElement>) value);
101
        }
102
103 1
        throw new NoSuchElementException("Variable " + variableName + " is not found or not a valid assoc value");
104
    }
105
106
    static public BaseLangFile load(URL url, SwfFileLoader loader) throws IOException, InterruptedException {
107 1
        BaseLangFile file = new BaseLangFile();
108
109 1
        loader.load(url, file, HYDRATOR);
110
111 1
        return file;
112
    }
113
}
114