aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/BinaryUnit.java
blob: e9877fe3bcf596f86e69d5d4e1e92f5c61e0bc76 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.builder.xml.dom;

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

import static com.yahoo.text.Lowercase.toLowerCase;

/**
 * @author Tony Vaagenes
 */
public class BinaryUnit {

    // The pattern must match the one given in the schema
    private static final Pattern pattern = Pattern.compile("(\\d+(\\.\\d*)?)\\s*([kmgKMG])?");

    public static double valueOf(String valueString) {
        Matcher matcher = pattern.matcher(valueString);

        if (!matcher.matches()) {
            throw new IllegalArgumentException("Value '" + valueString + "' does not match the pattern for binary unit");
        }
        double value = Double.valueOf(matcher.group(1));
        String unit = matcher.group(3);
        if (unit != null) {
            value *= unitToValue(toLowerCase(unit).charAt(0));
        }
        return value;
    }

    private static double unitToValue(char unit) {
           char[] units = {'k', 'm', 'g'};
           for (int i = 0; i < units.length; ++i) {
               if (units[i] == unit) {
                   return Math.pow(2, 10*(i+1));
               }
           }

           throw new IllegalArgumentException("No such unit: '" + unit + "'");
    }
}