Passed
Pull Request — master (#273)
by Vincent
20:25 queued 07:44
created

iterator()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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-2019 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.core.config;
21
22
import fr.quatrevieux.araknemu.util.Asserter;
23
import org.checkerframework.checker.index.qual.NonNegative;
24
import org.checkerframework.checker.index.qual.Positive;
25
import org.checkerframework.checker.nullness.qual.Nullable;
26
import org.checkerframework.common.value.qual.IntRange;
27
28
import java.time.Duration;
29
import java.util.Iterator;
30
import java.util.List;
31
import java.util.Map;
32
33
/**
34
 * Define utility methods for Pool
35
 */
36
public final class PoolUtils implements Pool {
37
    private final Pool pool;
38
39 1
    public PoolUtils(Pool pool) {
40 1
        this.pool = pool;
41 1
    }
42
43
    @Override
44
    public boolean has(String key) {
45 1
        return pool.has(key);
46
    }
47
48
    @Override
49
    public @Nullable String get(String key) {
50 1
        return pool.get(key);
51
    }
52
53
    @Override
54
    public List<String> getAll(String key) {
55 1
        return pool.getAll(key);
56
    }
57
58
    @Override
59
    public Iterator<Map.Entry<String, String>> iterator() {
60 1
        return pool.iterator();
61
    }
62
63
    /**
64
     * Parse a config item as integer
65
     */
66
    public int integer(String key, int defaultValue) {
67 1
        final String value = pool.get(key);
68
69 1
        return value != null
70 1
            ? Integer.parseInt(value)
71 1
            : defaultValue
72
        ;
73
    }
74
75
    /**
76
     * Parse a config item as a positive integer (i.e. >= 1)
77
     */
78
    public @Positive int positiveInteger(String key, @Positive int defaultValue) {
79 1
        return Asserter.assertPositive(integer(key, defaultValue));
80
    }
81
82
    /**
83
     * Parse a config item as a non-negative integer (i.e. >= 0)
84
     */
85
    public @NonNegative int nonNegativeInteger(String key, @NonNegative int defaultValue) {
86 1
        return Asserter.assertNonNegative(integer(key, defaultValue));
87
    }
88
89
    /**
90
     * Parse a config item as a percent integer (i.e. in interval [0, 100])
91
     */
92
    public @IntRange(from = 0, to = 100) int percent(String key, @IntRange(from = 0, to = 100) int defaultValue) {
93 1
        return Asserter.assertPercent(integer(key, defaultValue));
94
    }
95
96
    /**
97
     * Parse a config item as integer
98
     * If the item is not configured, will return zero
99
     */
100
    public int integer(String key) {
101 1
        return integer(key, 0);
102
    }
103
104
    /**
105
     * Parse a config item as boolean
106
     * This method handle values like "1", "true", "yes", "on" as true
107
     */
108
    public boolean bool(String key, boolean defaultValue) {
109 1
        String value = pool.get(key);
110
111 1
        if (value == null) {
112 1
            return defaultValue;
113
        }
114
115 1
        value = value.toLowerCase();
116
117 1
        for (String v : new String[] {"1", "true", "yes", "on"}) {
118 1
            if (value.equals(v)) {
119 1
                return true;
120
            }
121
        }
122
123 1
        return false;
124
    }
125
126
    /**
127
     * Parse a config item as boolean
128
     * This method handle values like "1", "true", "yes", "on" as true
129
     * If the item is not configured, will return false
130
     */
131
    public boolean bool(String key) {
132 1
        return bool(key, false);
133
    }
134
135
    /**
136
     * Parse a config item as String
137
     */
138
    public String string(String key, String defaultValue) {
139 1
        final String value = pool.get(key);
140
141 1
        return value != null
142 1
            ? value
143 1
            : defaultValue
144
        ;
145
    }
146
147
    /**
148
     * Parse a config item as String
149
     * If the item is not configured, will return an empty string
150
     */
151
    public String string(String key) {
152 1
        return string(key, "");
153
    }
154
155
    /**
156
     * Parse a config item as double
157
     */
158
    public double decimal(String key, double defaultValue) {
159 1
        final String value = pool.get(key);
160
161 1
        return value != null
162 1
            ? Double.parseDouble(value)
163 1
            : defaultValue
164
        ;
165
    }
166
167
    /**
168
     * Parse a config item as integer
169
     * If the item is not configured, will return zero
170
     */
171
    public double decimal(String key) {
172 1
        return decimal(key, 0d);
173
    }
174
175
    /**
176
     * Get a duration duration config item
177
     * Use the standard duration format (ISO-8601) with some differences :
178
     * - The string is case insensitive
179
     * - The prefix "PT" is not required
180
     *
181
     * Ex: "15s"    => 15 seconds
182
     *     "2m5s"   => 2 minutes and 5 seconds
183
     *     "p1dt5h" => 1 day and 5 hours
184
     *
185
     * @param key The config item key
186
     * @param defaultValue The value to use when the config item is not defined
187
     *
188
     * @return The parsed duration
189
     *
190
     * @see Duration#parse(CharSequence)
191
     */
192
    @SuppressWarnings("argument") // because of toUpperCase() call, checker miss the value length
193
    public Duration duration(String key, Duration defaultValue) {
194 1
        String value = pool.get(key);
195
196 1
        if (value == null || value.isEmpty()) {
197 1
            return defaultValue;
198
        }
199
200 1
        value = value.toUpperCase();
201
202 1
        if (value.charAt(0) != 'P') {
203 1
            value = "PT" + value;
204
        }
205
206 1
        return Duration.parse(value);
207
    }
208
209
    /**
210
     * Get a duration duration config item
211
     * Use the standard duration format (ISO-8601) with some differences :
212
     * - The string is case insensitive
213
     * - The prefix "PT" is not required
214
     *
215
     * Ex: "15s"    => 15 seconds
216
     *     "2m5s"   => 2 minutes and 5 seconds
217
     *     "p1dt5h" => 1 day and 5 hours
218
     *
219
     * @param key The config item key
220
     *
221
     * @return The parsed duration
222
     *
223
     * @see Duration#parse(CharSequence)
224
     */
225
    public Duration duration(String key) {
226 1
        return duration(key, Duration.ZERO);
227
    }
228
}
229