aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/utils/Duration.java
blob: f65b7a62f05c7428d65c558c1c291af7be69cb3f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Parses a string on the form:
 *
 * [numbers]\s*[unit]?
 *
 * Where numbers is a double, and unit is one of:
 * d - days
 * m - minutes
 * s - seconds
 * ms - milliseconds
 *
 * Default is seconds.
 */
public class Duration {

    private static final Pattern pattern = Pattern.compile("([0-9\\.]+)\\s*([a-z]+)?");
    private static final Map<String, Integer> unitMultiplier = new HashMap<>();
    static {
        unitMultiplier.put("s", 1000);
        unitMultiplier.put("d", 1000 * 3600 * 24);
        unitMultiplier.put("ms", 1);
        unitMultiplier.put("m", 60 * 1000);
        unitMultiplier.put("h", 60 * 60 * 1000);
    }

    long value;

    public Duration(String value) {
        Matcher matcher = pattern.matcher(value);

        if (!matcher.matches()) {
            throw new IllegalArgumentException("Illegal duration format: " + value);
        }

        double num = Double.parseDouble(matcher.group(1));
        String unit = matcher.group(2);

        if (unit != null) {
            Integer multiplier = unitMultiplier.get(unit);
            if (multiplier == null) {
                throw new IllegalArgumentException("Unknown time unit: " + unit + " in time value " + value);
            }

            num *= multiplier;
        } else {
            num *= 1000;
        }

        this.value = (long)num;
    }

    public double getSeconds() {
        return value / 1000.0;
    }

    public long getMilliSeconds() {
        return value;
    }

}