Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/******************************************************************************
* Copyright 2009-2018 Exactpro (Exactpro Systems Limited)
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.exactpro.sf.actions;
import java.math.BigDecimal;
import java.math.MathContext;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import com.exactpro.sf.aml.Description;
import com.exactpro.sf.common.impl.messages.IBaseEnumField;
import com.exactpro.sf.configuration.ResourceAliases;
import com.exactpro.sf.scriptrunner.AbstractCaller;
import com.exactpro.sf.scriptrunner.utilitymanager.UtilityMethod;
@MatrixUtils
@ResourceAliases("ConvertUtil")
public class ConvertUtil extends AbstractCaller {
@UtilityMethod
@Description("Converts a Number value with zero precision to BigDecimal. "
+ "value - a number value for converting. "
+ "Example: "
+ "#{toBigDecimal(5.444)} returns 5.444")
public BigDecimal toBigDecimal(Number value) {
return value == null ? null : toBigDecimal(value.toString(), 0);
}
@UtilityMethod
@Description("Converts a Number value with precision to BigDecimal. "
+ "value - a number value for converting. "
+ "precision - a number of digits. "
+ "Example: "
+ "#{toBigDecimal(5.444, 3)} returns 5.44")
public BigDecimal toBigDecimal(Number value, int presision) {
return value == null ? null : toBigDecimal(value.toString(), presision);
}
@UtilityMethod
@Description("Converts a String value to BigDecimal. "
+ "value - a string value for converting. "
+ "Example: "
+ "#{toBigDecimal(\"5.0\")} returns 5.0")
public BigDecimal toBigDecimal(String value) {
return value == null ? null : new BigDecimal(value);
}
@UtilityMethod
@Description("Converts a String value with precision to BigDecimal. "
+ "value - a string value for converting. "
+ "precision - a number of digits. "
+ "Example: "
+ "#{toBigDecimal(\"5.444\", 3)} returns 5.44")
public BigDecimal toBigDecimal(String value, int presision) {
if(value == null) {
return null;
}
MathContext mathContext = new MathContext(presision);
return new BigDecimal(value, mathContext);
}
@UtilityMethod
@Description("Converts a Number value to Double. "
+ "value - a number value for converting. "
+ "Example: "
+ "#{toDouble(5)} returns 5.0")
public Double toDouble(Number value) {
return value == null ? null : value.doubleValue();
}
@UtilityMethod
@Description("Converts a String value to Double. "
+ "value - a number value for converting. "
+ "Example: "
+ "#{toDouble(\"5\")} returns 5.0")
public Double toDouble(String value) {
return value == null ? null : Double.valueOf(value);
}
@UtilityMethod
@Description("Converts a Number value to Integer. "
+ "value - a number value for converting. "
+ "Example: "
+ "#{toInteger(5.0)} returns 5")
public Integer toInteger(Number value) {
return value == null ? null : value.intValue();
}
@UtilityMethod
@Description("Converts a String value to Integer. "
+ "value - a string value for converting. "
+ "Example: "
+ "#{toInteger(\"5\")} returns 5")
public Integer toInteger(String value) {
return value == null ? null : Integer.valueOf(value);
}
@UtilityMethod
@Description("Converts a Number value to Byte. "
+ "value - a number value for converting. "
+ "Example: "
+ "#{toByte(5)} returns 5")
public Byte toByte(Number value) {
return value == null ? null : value.byteValue();
}
@UtilityMethod
@Description("Converts a String value to Byte. "
+ "value - a string value for converting. "
+ "Example: "
+ "#{toByte(\"5\")} returns 5")
public Byte toByte(String value) {
return value == null ? null : Byte.valueOf(value);
}
@UtilityMethod
@Description("Converts a Number value to Short. "
+ "value - a number value for converting. "
+ "Example: "
+ "#{toShort(5)} returns 5")
public Short toShort(Number value) {
return value == null ? null : value.shortValue();
}
@UtilityMethod
@Description("Converts a String value to Short. "
+ "value - a string value for converting. "
+ "Example: "
+ "#{toShort(\"5\")} returns 5")
public Short toShort(String value) {
return value == null ? null : Short.valueOf(value);
}
@UtilityMethod
@Description("Converts a Number value to Float. "
+ "value - a number value for converting. "
+ "Example: "
+ "#{toFloat(5)} returns 5.0")
public Float toFloat(Number value) {
return value == null ? null : value.floatValue();
}
@UtilityMethod
@Description("Converts a String value to Float. "
+ "value - a string value for converting. "
+ "Example: "
+ "#{toFloat(\"5\")} returns 5.0")
public Float toFloat(String value) {
return value == null ? null : Float.valueOf(value);
}
@UtilityMethod
@Description("Converts a Number value to Long. "
+ "value - a number value for converting. "
+ "Example: "
+ "#{toLong(5)} returns 5")
public Long toLong(Number value) {
if(value == null) {
return null;
}
return value.longValue();
}
@UtilityMethod
@Description("Converts a String value to Long. "
+ "value - a string value for converting. "
+ "Example: "
+ "#{toLong(\"5\")} returns 5")
public Long toLong(String value) {
return value == null ? null : Long.valueOf(value);
}
@UtilityMethod
@Description("Converts an Object value to String. Returns null if the value is null. "
+ "value - a string value for converting. "
+ "Considering the above said, the final syntax is: "
+ "#{toString(value)}")
public String toString(Object value) {
return value != null ? value.toString() : null;
}
@UtilityMethod
@Description("Converts a Number value to String. Returns null if the value is null. "
+ "Number Format Pattern Syntax"
+ "
"
+ "
0
A digit - always displayed, even if the number has fewer digits (then 0 is displayed)
"
+ "
#
A digit, leading zeroes are omitted.
"
+ "
0
Marks decimal separator
"
+ "
,
Marks a grouping separator (e.g. a thousands separator)
"
+ "
E
Marks the separation of the mantissa and the exponent for exponential formats.
"
+ "
;
Separates formats
"
+ "
-
Marks the negative number prefix
"
+ "
%
Multiplies by 100 and shows the number as percentage
"
+ "
?
Multiplies by 1000 and shows the number as per mille
"
+ "
¤
Currency sign - replaced by the currency sign for the Locale. Also makes formatting use the monetary decimal separator instead of the official decimal separator. ¤¤ makes formatting use international monetary symbols.
"
+ "
X
Marks a character to be used in the number prefix or suffix
"
+ "
'
Marks a quote around special characters in the prefix or the suffix of the formatted number.
"
+ "
"
+ "value - a number value for converting. "
+ "pattern - a format of the resulting string. "
+ "Examples:
"
+ "
Pattern
Number
Formatted String
"
+ "
###.###
123.456
123.456
"
+ "
###.#
123.456
123.5
"
+ "
###,###.##
123456.789
123,456.79
"
+ "
000.###
9.95
009.95
"
+ "
##0.###
0.95
0.95
"
+ "
")
public String toString(Number value, String pattern) {
return toString(value, pattern, null, null);
}
@UtilityMethod
@Description("Converts a Number value to String. Returns null if the value is null. "
+ "Number Format Pattern Syntax"
+ "
"
+ "
0
A digit - always displayed, even if the number has fewer digits (then 0 is displayed)
"
+ "
#
A digit, leading zeroes are omitted.
"
+ "
0
Marks decimal separator
"
+ "
,
Marks a grouping separator (e.g. a thousands separator)
"
+ "
E
Marks the separation of the mantissa and the exponent for exponential formats.
"
+ "
;
Separates formats
"
+ "
-
Marks the negative number prefix
"
+ "
%
Multiplies by 100 and shows the number as percentage
"
+ "
?
Multiplies by 1000 and shows the number as per mille
"
+ "
¤
Currency sign - replaced by the currency sign for the Locale. Also makes formatting use the monetary decimal separator instead of the official decimal separator. ¤¤ makes formatting use international monetary symbols.
"
+ "
X
Marks a character to be used in the number prefix or suffix
"
+ "
'
Marks a quote around special characters in the prefix or the suffix of the formatted number.
"
+ "
"
+ "value - a number value for converting. "
+ "pattern - a format of the resulting string. "
+ "decimalSeparator - a character used for the decimal sign."
+ "Examples:"
+ "
"
+ "
Pattern
Number
Formatted String
"
+ "
###.###
123.456
123.456
"
+ "
###.#
123.456
123.5
"
+ "
###,###.##
123456.789
123,456.79
"
+ "
000.###
9.95
009.95
"
+ "
##0.###
0.95
0.95
"
+ "
")
public String toString(Number value, String pattern, Character decimalSeparator) {
return toString(value, pattern, decimalSeparator, null);
}
@UtilityMethod
@Description("Converts a Number value to String. Returns null if the value is null. "
+ "Number Format Pattern Syntax"
+ "
"
+ "
0
A digit - always displayed, even if the number has fewer digits (then 0 is displayed)
"
+ "
#
A digit, leading zeroes are omitted.
"
+ "
0
Marks decimal separator
"
+ "
,
Marks a grouping separator (e.g. a thousands separator)
"
+ "
E
Marks the separation of the mantissa and the exponent for exponential formats.
"
+ "
;
Separates formats
"
+ "
-
Marks the negative number prefix
"
+ "
%
Multiplies by 100 and shows the number as percentage
"
+ "
?
Multiplies by 1000 and shows the number as per mille
"
+ "
¤
Currency sign - replaced by the currency sign for the Locale. Also makes formatting use the monetary decimal separator instead of the official decimal separator. ¤¤ makes formatting use international monetary symbols.
"
+ "
X
Marks a character to be used in the number prefix or suffix
"
+ "
'
Marks a quote around special characters in the prefix or the suffix of the formatted number.
"
+ "
"
+ "value - a number value for converting. "
+ "pattern - a format of the resulting string. "
+ "decimalSeparator - a character used for the decimal sign."
+ "groupingSeparator - a character used for the thousands separator. "
+ "Examples:"
+ "
"
+ "
Pattern
Number
Formatted String
"
+ "
###.###
123.456
123.456
"
+ "
###.#
123.456
123.5
"
+ "
###,###.##
123456.789
123,456.79
"
+ "
000.###
9.95
009.95
"
+ "
##0.###
0.95
0.95
"
+ "
")
public String toString(Number value, String pattern, Character decimalSeparator, Character groupingSeparator) {
if (value != null) {
DecimalFormatSymbols formatSymbols = new DecimalFormatSymbols();
if (decimalSeparator != null) {
formatSymbols.setDecimalSeparator(decimalSeparator);
}
if (groupingSeparator != null) {
formatSymbols.setGroupingSeparator(groupingSeparator);
}
DecimalFormat format = new DecimalFormat(pattern, formatSymbols);
return format.format(value);
}
return null;
}
@UtilityMethod
@Description("Converts the incoming parameter of a String (size 1), Character, Byte, Short, Integer value type "
+ "or FIX enum, Base enum (using for non-FIX message) based on the same types. "
+ "Throws IllegalArgumentException in runtime"
+ "value - a number value for converting. "
+ "Example: "
+ "#{toChar(\"A\")} returns 'A'")
public Character toChar(Object value) {
return toChar(value, Boolean.FALSE);
}
@UtilityMethod
@Description("Converts the incoming parameter of a String (size 1), Character, Byte, Short, Integer value type "
+ "or FIX enum, Base enum (using for non-FIX message) based on the same types. "
+ "isDigit - adds 48 / '0' code. "
+ "Example: "
+ "\"#{toChar(1, true)} returns '1' "
+ "Throws IllegalArgumentException in runtime.")
public Character toChar(Object value, Boolean isDigit) {
if (value == null) {
return null;
}
value = extractValue(value);
char charValue = '\u0000';
if (value instanceof Character) {
charValue = (Character)value;
} else if (value instanceof String) {
String stringValue = (String)value;
if (stringValue.length() != 1) {
throw new IllegalArgumentException("String length is not one, value [" + value + "]");
}
charValue = stringValue.charAt(0);
} else if (value instanceof Integer || value instanceof Short || value instanceof Byte) {
charValue = (char) ((Number)value).intValue();
} else {
throw new IllegalArgumentException("No action for value type [" + value.getClass().getCanonicalName() + "]");
}
return Boolean.TRUE.equals(isDigit) ? (char)(charValue + '0') : charValue;
}
@UtilityMethod
@Description("Converts a String (size 1), Character, Byte, Short, Integer types with single char in the value "
+ "to code point at the given char. "
+ "value - a value for converting. "
+ "Example: "
+ "#{toCodePoint('A')} returns 65. "
+ "Throws IllegalArgumentException in runtime.")
public Integer toCodePoint(Object value) {
if(value == null) {
return null;
}
Character charValue = toChar(value);
return Character.codePointAt(new char[] { charValue }, 0);
}
@UtilityMethod
@Description("Converts an array of objects to map. "
+ "values - a values for converting(must be even). "
+ "Example: "
+ "#{toMap(\"Text\", \"example!\")} returns {Test=example}")
public Map