Move memory parsing to utility class.

This commit is contained in:
Michael Lipp 2023-08-14 17:17:34 +02:00
parent e7f572a954
commit 2404663045
2 changed files with 98 additions and 68 deletions

View file

@ -19,22 +19,18 @@
package org.jdrupes.vmoperator.runner.qemu; package org.jdrupes.vmoperator.runner.qemu;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission; import java.nio.file.attribute.PosixFilePermission;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Pattern;
import org.jdrupes.vmoperator.util.Dto; import org.jdrupes.vmoperator.util.Dto;
import org.jdrupes.vmoperator.util.FsdUtils; import org.jdrupes.vmoperator.util.FsdUtils;
import org.jdrupes.vmoperator.util.ParseUtils;
/** /**
* The configuration information from the configuration file. * The configuration information from the configuration file.
@ -44,33 +40,6 @@ public class Configuration implements Dto {
@SuppressWarnings("PMD.FieldNamingConventions") @SuppressWarnings("PMD.FieldNamingConventions")
protected final Logger logger = Logger.getLogger(getClass().getName()); protected final Logger logger = Logger.getLogger(getClass().getName());
@SuppressWarnings({ "PMD.UseConcurrentHashMap",
"PMD.FieldNamingConventions", "PMD.VariableNamingConventions" })
private static final Map<String, BigInteger> unitMap = new HashMap<>();
@SuppressWarnings({ "PMD.FieldNamingConventions",
"PMD.VariableNamingConventions" })
private static final Pattern memorySize
= Pattern.compile("^\\s*(\\d+(\\.\\d+)?)\\s*([A-Za-z]*)\\s*");
static {
// SI units and common abbreviations
BigInteger factor = BigInteger.ONE;
unitMap.put("", factor);
BigInteger scale = BigInteger.valueOf(1000);
for (var unit : List.of("B", "kB", "MB", "GB", "TB", "PB", "EB")) {
unitMap.put(unit, factor);
factor = factor.multiply(scale);
}
// Binary units
factor = BigInteger.valueOf(1024);
scale = BigInteger.valueOf(1024);
for (var unit : List.of("KiB", "MiB", "GiB", "TiB", "PiB", "EiB")) {
unitMap.put(unit, factor);
unitMap.put(unit.substring(0, 2), factor);
factor = factor.multiply(scale);
}
}
/** The data dir. */ /** The data dir. */
public Path dataDir; public Path dataDir;
@ -99,40 +68,6 @@ public class Configuration implements Dto {
@SuppressWarnings("PMD.ShortVariable") @SuppressWarnings("PMD.ShortVariable")
public Vm vm; public Vm vm;
/**
* Parses a memory size specification.
*
* @param amount the amount
* @return the big integer
*/
@SuppressWarnings("PMD.DataflowAnomalyAnalysis")
public static BigInteger parseMemory(Object amount) {
if (amount == null) {
return (BigInteger) amount;
}
if (amount instanceof BigInteger number) {
return number;
}
if (amount instanceof Number number) {
return BigInteger.valueOf(number.longValue());
}
var matcher = memorySize.matcher(amount.toString());
if (!matcher.matches()) {
throw new NumberFormatException(amount.toString());
}
var unit = BigInteger.ONE;
if (matcher.group(3) != null) {
unit = unitMap.get(matcher.group(3));
if (unit == null) {
throw new NumberFormatException("Illegal unit \""
+ matcher.group(3) + "\" in \"" + amount.toString() + "\"");
}
}
var number = matcher.group(1);
return new BigDecimal(number).multiply(new BigDecimal(unit))
.toBigInteger();
}
/** /**
* Subsection "vm". * Subsection "vm".
*/ */
@ -209,7 +144,7 @@ public class Configuration implements Dto {
* @param value the new maximum ram * @param value the new maximum ram
*/ */
public void setMaximumRam(String value) { public void setMaximumRam(String value) {
maximumRam = parseMemory(value); maximumRam = ParseUtils.parseMemory(value);
} }
/** /**
@ -218,7 +153,7 @@ public class Configuration implements Dto {
* @param value the new current ram * @param value the new current ram
*/ */
public void setCurrentRam(String value) { public void setCurrentRam(String value) {
currentRam = parseMemory(value); currentRam = ParseUtils.parseMemory(value);
} }
} }

View file

@ -0,0 +1,95 @@
/*
* VM-Operator
* Copyright (C) 2023 Michael N. Lipp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.jdrupes.vmoperator.util;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
/**
* Provides methods for parsing.
*/
@SuppressWarnings("PMD.UseUtilityClass")
public class ParseUtils {
@SuppressWarnings({ "PMD.UseConcurrentHashMap",
"PMD.FieldNamingConventions", "PMD.VariableNamingConventions" })
private static final Map<String, BigInteger> unitMap = new HashMap<>();
@SuppressWarnings({ "PMD.FieldNamingConventions",
"PMD.VariableNamingConventions" })
private static final Pattern memorySize
= Pattern.compile("^\\s*(\\d+(\\.\\d+)?)\\s*([A-Za-z]*)\\s*");
static {
// SI units and common abbreviations
BigInteger factor = BigInteger.ONE;
unitMap.put("", factor);
BigInteger scale = BigInteger.valueOf(1000);
for (var unit : List.of("B", "kB", "MB", "GB", "TB", "PB", "EB")) {
unitMap.put(unit, factor);
factor = factor.multiply(scale);
}
// Binary units
factor = BigInteger.valueOf(1024);
scale = BigInteger.valueOf(1024);
for (var unit : List.of("KiB", "MiB", "GiB", "TiB", "PiB", "EiB")) {
unitMap.put(unit, factor);
unitMap.put(unit.substring(0, 2), factor);
factor = factor.multiply(scale);
}
}
/**
* Parses a memory size specification.
*
* @param amount the amount
* @return the big integer
*/
@SuppressWarnings("PMD.DataflowAnomalyAnalysis")
public static BigInteger parseMemory(Object amount) {
if (amount == null) {
return (BigInteger) amount;
}
if (amount instanceof BigInteger number) {
return number;
}
if (amount instanceof Number number) {
return BigInteger.valueOf(number.longValue());
}
var matcher = memorySize.matcher(amount.toString());
if (!matcher.matches()) {
throw new NumberFormatException(amount.toString());
}
var unit = BigInteger.ONE;
if (matcher.group(3) != null) {
unit = unitMap.get(matcher.group(3));
if (unit == null) {
throw new NumberFormatException("Illegal unit \""
+ matcher.group(3) + "\" in \"" + amount.toString() + "\"");
}
}
var number = matcher.group(1);
return new BigDecimal(number).multiply(new BigDecimal(unit))
.toBigInteger();
}
}