aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/sdc/toscaparser/api/elements/ScalarUnit.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/onap/sdc/toscaparser/api/elements/ScalarUnit.java')
-rw-r--r--src/main/java/org/onap/sdc/toscaparser/api/elements/ScalarUnit.java281
1 files changed, 143 insertions, 138 deletions
diff --git a/src/main/java/org/onap/sdc/toscaparser/api/elements/ScalarUnit.java b/src/main/java/org/onap/sdc/toscaparser/api/elements/ScalarUnit.java
index eeaa07c..1eaa8a0 100644
--- a/src/main/java/org/onap/sdc/toscaparser/api/elements/ScalarUnit.java
+++ b/src/main/java/org/onap/sdc/toscaparser/api/elements/ScalarUnit.java
@@ -7,9 +7,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -20,147 +20,152 @@
package org.onap.sdc.toscaparser.api.elements;
-import java.util.HashMap;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
import org.onap.sdc.toscaparser.api.utils.ValidateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.util.HashMap;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
public abstract class ScalarUnit {
- private static Logger log = LoggerFactory.getLogger(ScalarUnit.class.getName());
+ private static Logger log = LoggerFactory.getLogger(ScalarUnit.class.getName());
+
+ private static final String SCALAR_UNIT_SIZE = "scalar-unit.size";
+ private static final String SCALAR_UNIT_FREQUENCY = "scalar-unit.frequency";
+ private static final String SCALAR_UNIT_TIME = "scalar-unit.time";
- private static final String SCALAR_UNIT_SIZE = "scalar-unit.size";
- private static final String SCALAR_UNIT_FREQUENCY = "scalar-unit.frequency";
- private static final String SCALAR_UNIT_TIME = "scalar-unit.time";
-
- public static final String SCALAR_UNIT_TYPES[] = {
+ public static final String[] SCALAR_UNIT_TYPES = {
SCALAR_UNIT_SIZE, SCALAR_UNIT_FREQUENCY, SCALAR_UNIT_TIME
- };
-
- private Object value;
- protected HashMap<String,Object> SCALAR_UNIT_DICT;
- protected String SCALAR_UNIT_DEFAULT;
-
- public ScalarUnit(Object _value) {
- value = _value;
- SCALAR_UNIT_DICT = new HashMap<>();
- SCALAR_UNIT_DEFAULT = "";
- }
-
-
- private String _checkUnitInScalarStandardUnits(String inputUnit) {
+ };
+
+ private Object value;
+ private HashMap<String, Object> scalarUnitDict;
+ private String scalarUnitDefault;
+
+ public ScalarUnit(Object value) {
+ this.value = value;
+ scalarUnitDict = new HashMap<>();
+ scalarUnitDefault = "";
+ }
+
+ void putToScalarUnitDict(String key, Object value) {
+ scalarUnitDict.put(key, value);
+ }
+
+ void setScalarUnitDefault(String scalarUnitDefault) {
+ this.scalarUnitDefault = scalarUnitDefault;
+ }
+
+ private String checkUnitInScalarStandardUnits(String inputUnit) {
// Check whether the input unit is following specified standard
-
+
// If unit is not following specified standard, convert it to standard
// unit after displaying a warning message.
-
- if(SCALAR_UNIT_DICT.get(inputUnit) != null) {
- return inputUnit;
- }
- else {
- for(String key: SCALAR_UNIT_DICT.keySet()) {
- if(key.toUpperCase().equals(inputUnit.toUpperCase())) {
- log.debug("ScalarUnit - _checkUnitInScalarStandardUnits - \n" +
- "The unit {} does not follow scalar unit standards\n" +
- "using {} instead",
- inputUnit, key);
- return key;
- }
- }
- ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE007", String.format(
- "'The unit \"%s\" is not valid. Valid units are \n%s",
- inputUnit,SCALAR_UNIT_DICT.keySet().toString())));
+
+ if (scalarUnitDict.get(inputUnit) != null) {
+ return inputUnit;
+ } else {
+ for (String key : scalarUnitDict.keySet()) {
+ if (key.toUpperCase().equals(inputUnit.toUpperCase())) {
+ log.debug("ScalarUnit - checkUnitInScalarStandardUnits - \n"
+ + "The unit {} does not follow scalar unit standards\n"
+ + "using {} instead",
+ inputUnit, key);
+ return key;
+ }
+ }
+ ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE007", String.format(
+ "'The unit \"%s\" is not valid. Valid units are \n%s",
+ inputUnit, scalarUnitDict.keySet().toString())));
return inputUnit;
- }
- }
-
- public Object validateScalarUnit() {
- Pattern pattern = Pattern.compile("([0-9.]+)\\s*(\\w+)");
- Matcher matcher = pattern.matcher(value.toString());
- if(matcher.find()) {
- ValidateUtils.strToNum(matcher.group(1));
- String scalarUnit = _checkUnitInScalarStandardUnits(matcher.group(2));
- value = matcher.group(1) + " " + scalarUnit;
- }
- else {
+ }
+ }
+
+ public Object validateScalarUnit() {
+ Pattern pattern = Pattern.compile("([0-9.]+)\\s*(\\w+)");
+ Matcher matcher = pattern.matcher(value.toString());
+ if (matcher.find()) {
+ ValidateUtils.strToNum(matcher.group(1));
+ String scalarUnit = checkUnitInScalarStandardUnits(matcher.group(2));
+ value = matcher.group(1) + " " + scalarUnit;
+ } else {
ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE134", String.format(
- "ValueError: \"%s\" is not a valid scalar-unit",value.toString())));
- }
- return value;
- }
-
- public double getNumFromScalarUnit(String unit) {
- if(unit != null) {
- unit = _checkUnitInScalarStandardUnits(unit);
- }
- else {
- unit = SCALAR_UNIT_DEFAULT;
- }
- Pattern pattern = Pattern.compile("([0-9.]+)\\s*(\\w+)");
- Matcher matcher = pattern.matcher(value.toString());
- if(matcher.find()) {
- ValidateUtils.strToNum(matcher.group(1));
- String scalarUnit = _checkUnitInScalarStandardUnits(matcher.group(2));
- value = matcher.group(1) + " " + scalarUnit;
- Object on1 = ValidateUtils.strToNum(matcher.group(1)) != null ? ValidateUtils.strToNum(matcher.group(1)) : 0;
- Object on2 = SCALAR_UNIT_DICT.get(matcher.group(2)) != null ? SCALAR_UNIT_DICT.get(matcher.group(2)) : 0;
- Object on3 = SCALAR_UNIT_DICT.get(unit) != null ? SCALAR_UNIT_DICT.get(unit) : 0;
-
- Double n1 = new Double(on1.toString());
- Double n2 = new Double(on2.toString());
- Double n3 = new Double(on3.toString());
- double converted = n1 * n2 / n3;
- if(Math.abs(converted - Math.round(converted)) < 0.0000000000001 ) {
- converted = Math.round(converted);
- }
- return converted;
- }
- return 0l; //???
- }
-
- protected static HashMap<String,String> scalarunitMapping = _getScalarunitMappings();
-
- private static HashMap<String,String> _getScalarunitMappings() {
- HashMap<String,String> map = new HashMap<>();
- map.put(SCALAR_UNIT_FREQUENCY,"ScalarUnitFrequency");
- map.put(SCALAR_UNIT_SIZE, "ScalarUnitSize");
- map.put(SCALAR_UNIT_TIME, "ScalarUnit_Time");
- return map;
- }
-
- public static ScalarUnit getScalarunitClass(String type,Object val) {
- if(type.equals(SCALAR_UNIT_SIZE)) {
- return new ScalarUnitSize(val);
- }
- else if(type.equals(SCALAR_UNIT_TIME)) {
- return new ScalarUnitTime(val);
- }
- else if(type.equals(SCALAR_UNIT_FREQUENCY)) {
- return new ScalarUnitFrequency(val);
- }
- return null;
- }
-
- public static double getScalarunitValue(String type, Object value, String unit) {
- if(type.equals(SCALAR_UNIT_SIZE)) {
- return (new ScalarUnitSize(value)).getNumFromScalarUnit(unit);
- }
- if(type.equals(SCALAR_UNIT_TIME)) {
- return (new ScalarUnitTime(value)).getNumFromScalarUnit(unit);
- }
- if(type.equals(SCALAR_UNIT_FREQUENCY)) {
- return (new ScalarUnitFrequency(value)).getNumFromScalarUnit(unit);
- }
+ "ValueError: \"%s\" is not a valid scalar-unit", value.toString())));
+ }
+ return value;
+ }
+
+ public double getNumFromScalarUnit(String unit) {
+ if (unit != null) {
+ unit = checkUnitInScalarStandardUnits(unit);
+ } else {
+ unit = scalarUnitDefault;
+ }
+ Pattern pattern = Pattern.compile("([0-9.]+)\\s*(\\w+)");
+ Matcher matcher = pattern.matcher(value.toString());
+ if (matcher.find()) {
+ final double minimalNum = 0.0000000000001;
+
+ ValidateUtils.strToNum(matcher.group(1));
+ String scalarUnit = checkUnitInScalarStandardUnits(matcher.group(2));
+ value = matcher.group(1) + " " + scalarUnit;
+ Object on1 = ValidateUtils.strToNum(matcher.group(1)) != null ? ValidateUtils.strToNum(matcher.group(1)) : 0;
+ Object on2 = scalarUnitDict.get(matcher.group(2)) != null ? scalarUnitDict.get(matcher.group(2)) : 0;
+ Object on3 = scalarUnitDict.get(unit) != null ? scalarUnitDict.get(unit) : 0;
+
+ Double n1 = new Double(on1.toString());
+ Double n2 = new Double(on2.toString());
+ Double n3 = new Double(on3.toString());
+ double converted = n1 * n2 / n3;
+
+ if (Math.abs(converted - Math.round(converted)) < minimalNum) {
+ converted = Math.round(converted);
+ }
+ return converted;
+ }
+ return 0.0;
+ }
+
+ private static HashMap<String, String> scalarUnitMapping = getScalarUnitMappings();
+
+ private static HashMap<String, String> getScalarUnitMappings() {
+ HashMap<String, String> map = new HashMap<>();
+ map.put(SCALAR_UNIT_FREQUENCY, "ScalarUnitFrequency");
+ map.put(SCALAR_UNIT_SIZE, "ScalarUnitSize");
+ map.put(SCALAR_UNIT_TIME, "ScalarUnit_Time");
+ return map;
+ }
+
+ public static ScalarUnit getScalarunitClass(String type, Object val) {
+ if (type.equals(SCALAR_UNIT_SIZE)) {
+ return new ScalarUnitSize(val);
+ } else if (type.equals(SCALAR_UNIT_TIME)) {
+ return new ScalarUnitTime(val);
+ } else if (type.equals(SCALAR_UNIT_FREQUENCY)) {
+ return new ScalarUnitFrequency(val);
+ }
+ return null;
+ }
+
+ public static double getScalarunitValue(String type, Object value, String unit) {
+ if (type.equals(SCALAR_UNIT_SIZE)) {
+ return (new ScalarUnitSize(value)).getNumFromScalarUnit(unit);
+ }
+ if (type.equals(SCALAR_UNIT_TIME)) {
+ return (new ScalarUnitTime(value)).getNumFromScalarUnit(unit);
+ }
+ if (type.equals(SCALAR_UNIT_FREQUENCY)) {
+ return (new ScalarUnitFrequency(value)).getNumFromScalarUnit(unit);
+ }
ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE135", String.format(
- "TypeError: \"%s\" is not a valid scalar-unit type",type)));
+ "TypeError: \"%s\" is not a valid scalar-unit type", type)));
return 0.0;
- }
-
+ }
+
}
/*python
@@ -190,10 +195,10 @@ class ScalarUnit(object):
If unit is not following specified standard, convert it to standard
unit after displaying a warning message.
"""
- if input_unit in self.SCALAR_UNIT_DICT.keys():
+ if input_unit in self.scalarUnitDict.keys():
return input_unit
else:
- for key in self.SCALAR_UNIT_DICT.keys():
+ for key in self.scalarUnitDict.keys():
if key.upper() == input_unit.upper():
log.warning(_('The unit "%(unit)s" does not follow '
'scalar unit standards; using "%(key)s" '
@@ -203,7 +208,7 @@ class ScalarUnit(object):
msg = (_('The unit "%(unit)s" is not valid. Valid units are '
'"%(valid_units)s".') %
{'unit': input_unit,
- 'valid_units': sorted(self.SCALAR_UNIT_DICT.keys())})
+ 'valid_units': sorted(self.scalarUnitDict.keys())})
ValidationIssueCollector.appendException(ValueError(msg))
def validate_scalar_unit(self):
@@ -224,14 +229,14 @@ class ScalarUnit(object):
if unit:
unit = self._check_unit_in_scalar_standard_units(unit)
else:
- unit = self.SCALAR_UNIT_DEFAULT
+ unit = self.scalarUnitDefault
self.validate_scalar_unit()
regex = re.compile('([0-9.]+)\s*(\w+)')
result = regex.match(str(self.value)).groups()
converted = (float(validateutils.str_to_num(result[0]))
- * self.SCALAR_UNIT_DICT[result[1]]
- / self.SCALAR_UNIT_DICT[unit])
+ * self.scalarUnitDict[result[1]]
+ / self.scalarUnitDict[unit])
if converted - int(converted) < 0.0000000000001:
converted = int(converted)
return converted
@@ -239,8 +244,8 @@ class ScalarUnit(object):
class ScalarUnit_Size(ScalarUnit):
- SCALAR_UNIT_DEFAULT = 'B'
- SCALAR_UNIT_DICT = {'B': 1, 'kB': 1000, 'KiB': 1024, 'MB': 1000000,
+ scalarUnitDefault = 'B'
+ scalarUnitDict = {'B': 1, 'kB': 1000, 'KiB': 1024, 'MB': 1000000,
'MiB': 1048576, 'GB': 1000000000,
'GiB': 1073741824, 'TB': 1000000000000,
'TiB': 1099511627776}
@@ -248,15 +253,15 @@ class ScalarUnit_Size(ScalarUnit):
class ScalarUnit_Time(ScalarUnit):
- SCALAR_UNIT_DEFAULT = 'ms'
- SCALAR_UNIT_DICT = {'d': 86400, 'h': 3600, 'm': 60, 's': 1,
+ scalarUnitDefault = 'ms'
+ scalarUnitDict = {'d': 86400, 'h': 3600, 'm': 60, 's': 1,
'ms': 0.001, 'us': 0.000001, 'ns': 0.000000001}
class ScalarUnit_Frequency(ScalarUnit):
- SCALAR_UNIT_DEFAULT = 'GHz'
- SCALAR_UNIT_DICT = {'Hz': 1, 'kHz': 1000,
+ scalarUnitDefault = 'GHz'
+ scalarUnitDict = {'Hz': 1, 'kHz': 1000,
'MHz': 1000000, 'GHz': 1000000000}