de.pewpewproject.lasertag.lasertaggame.state.synced.implementation.SettingsState   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 25
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createBaseSettings() 0 12 3
A toJson() 0 2 1
A fromJson(String) 0 5 1
1
package de.pewpewproject.lasertag.lasertaggame.state.synced.implementation;
2
3
import com.google.gson.Gson;
4
import com.google.gson.GsonBuilder;
5
import com.google.gson.ToNumberPolicy;
6
import de.pewpewproject.lasertag.lasertaggame.settings.SettingDescription;
7
8
import java.util.HashMap;
9
10
/**
11
 * The state of the settings.
12
 *
13
 * @author Étienne Muser
14
 */
15
public class SettingsState extends HashMap<String, Object> {
16
17
    public static SettingsState createBaseSettings() {
18
        var settings = new SettingsState();
19
20
        for (var setting : SettingDescription.values()) {
21
            if (setting.getDataType().isEnum()) {
22
                settings.put(setting.getName(), ((Enum<?>)setting.getDefaultValue()).name());
23
            } else {
24
                settings.put(setting.getName(), setting.getDefaultValue());
25
            }
26
        }
27
28
        return settings;
29
    }
30
31
    public String toJson() {
32
        return new Gson().toJson(this);
33
    }
34
35
    public static SettingsState fromJson(String json) {
36
        return new GsonBuilder()
37
                .setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
38
                .create()
39
                .fromJson(json, SettingsState.class);
40
    }
41
}
42