net.labymod.serverapi.LabyModConfig   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 20
dl 0
loc 64
c 1
b 0
f 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A LabyModConfig(File) 0 2 1
A loadValues() 0 11 4
init(File) 0 1 ?
getValue(String) 0 1 ?
A addDefaults() 0 5 2
saveConfig() 0 1 ?
addDefault(String,Object) 0 1 ?
1
package net.labymod.serverapi;
2
3
import lombok.Getter;
4
5
import java.io.File;
6
import java.util.HashMap;
7
import java.util.Map;
8
9
/**
10
 * Class created by qlow | Jan
11
 */
12
public abstract class LabyModConfig {
13
14
    protected File file;
15
16
    @Getter
17
    private Map<Permission, Boolean> permissions = new HashMap<>();
0 ignored issues
show
Performance introduced by
When using a map whose keys are EnumValues, consider using an EnumMap instead, which is more performant in this case.

The Java documentation explain EnumMap.

Loading history...
18
19
    public LabyModConfig( File file ) {
20
        this.file = file;
21
    }
22
23
    /**
24
     * Called once to initialize the config
25
     *
26
     * @param file the config-file
27
     */
28
    public abstract void init( File file );
29
30
    /**
31
     * Gets a key's value
32
     *
33
     * @param key the key the value should be resolved from
34
     * @return the value according to this key or <code>null</code> if there is no value according to this key
35
     */
36
    public abstract Object getValue( String key );
37
38
    /**
39
     * Adds a default to the config
40
     *
41
     * @param key   the key
42
     * @param value the default value
43
     */
44
    public abstract void addDefault( String key, Object value );
45
46
    /**
47
     * Saves the config
48
     */
49
    public abstract void saveConfig();
50
51
    /**
52
     * Adds the config's defaults
53
     */
54
    public void addDefaults() {
55
        // Iterating through all permissions
56
        for ( Permission permission : Permission.values() ) {
57
            // Putting the default value in
58
            addDefault( "permissions." + permission.name(), permission.isDefaultEnabled() );
59
        }
60
    }
61
62
    /**
63
     * Loads the config values after adding the defaults
64
     */
65
    public void loadValues() {
66
        // Iterating through all permissions
67
        for ( Permission permission : Permission.values() ) {
68
            Object value = getValue( "permissions." + permission.name() );
69
70
            // Checking whether there is a value according to this permission
71
            if ( value != null && value instanceof Boolean ) {
72
                // Checking whether the permission value was modified
73
                permissions.put( permission, ( Boolean ) value );
74
            } else {
75
                permissions.put( permission, permission.isDefaultEnabled() );
76
            }
77
        }
78
    }
79
80
}
81