| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 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 |
|
| 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 |
} |