Debian Codegraph @

owner-java8-extras/src/main/java/org/aeonbits/owner/converters/ByteSizeConverter.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2012-2016, Luigi R. Viggiano
3 * All rights reserved.
4 *
5 * This software is distributable under the BSD license.
6 * See the terms of the BSD license in the documentation provided with this software.
7 */
8
9 package org.aeonbits.owner.converters;
10
11 import org.aeonbits.owner.util.bytesize.ByteSize;
12 import org.aeonbits.owner.util.bytesize.ByteSizeUnit;
13 import org.aeonbits.owner.Converter;
14
15 import java.lang.reflect.Method;
16 import java.math.BigDecimal;
17
18 /**
19 * @author Stefan Freyr Stefansson
20 */
21 public class ByteSizeConverter implements Converter<ByteSize> {
22
23 public ByteSize convert(Method method, String input) {
24 return parse(input);
25 }
26
27 private static ByteSize parse(String input){
28 String[] parts = ConverterUtil.splitNumericAndChar(input);
29 String value = parts[0];
30 String unit = parts[1];
31
32 BigDecimal bdValue = new BigDecimal(value);
33 ByteSizeUnit bsuUnit = ByteSizeUnit.parse(unit);
34
35 if (bsuUnit == null){
36 throw new IllegalArgumentException("Invalid unit string: '" + unit + "'");
37 }
38
39 return new ByteSize(bdValue, bsuUnit);
40 }
41 }