use of java.math.RoundingMode in project head by mifos.
the class AccountingRulesTest method testGetInitialRoundingMode.
@Test
public void testGetInitialRoundingMode() {
RoundingMode configuredMode = AccountingRules.getInitialRoundingMode();
String roundingMode = "FLOOR";
RoundingMode configRoundingMode = RoundingMode.FLOOR;
MifosConfigurationManager configMgr = MifosConfigurationManager.getInstance();
configMgr.setProperty(AccountingRulesConstants.INITIAL_ROUNDING_MODE, roundingMode);
// return value from accounting rules class has to be the value defined
// in the config file
assertEquals(configRoundingMode, AccountingRules.getInitialRoundingMode());
// clear the RoundingRule property from the config file
configMgr.clearProperty(AccountingRulesConstants.INITIAL_ROUNDING_MODE);
RoundingMode defaultValue = AccountingRules.getInitialRoundingMode();
assertEquals(defaultValue, RoundingMode.HALF_UP);
// now set a wrong rounding mode in config
roundingMode = "UP";
configMgr.addProperty(AccountingRulesConstants.INITIAL_ROUNDING_MODE, roundingMode);
try {
AccountingRules.getInitialRoundingMode();
fail();
} catch (RuntimeException e) {
assertEquals("InitialRoundingMode defined in the config file is not CEILING, FLOOR, HALF_UP. It is " + roundingMode, e.getMessage());
}
// save it back
configMgr.setProperty(AccountingRulesConstants.INITIAL_ROUNDING_MODE, configuredMode.toString());
}
use of java.math.RoundingMode in project openhab1-addons by openhab.
the class BindingConfigParser method parse.
/**
* Parses the bindingConfig of an item and returns a WeatherBindingConfig.
*/
public WeatherBindingConfig parse(Item item, String bindingConfig) throws BindingConfigParseException {
bindingConfig = StringUtils.trimToEmpty(bindingConfig);
bindingConfig = StringUtils.removeStart(bindingConfig, "{");
bindingConfig = StringUtils.removeEnd(bindingConfig, "}");
String[] entries = bindingConfig.split("[,]");
WeatherBindingConfigHelper helper = new WeatherBindingConfigHelper();
for (String entry : entries) {
String[] entryParts = StringUtils.trimToEmpty(entry).split("[=]");
if (entryParts.length != 2) {
throw new BindingConfigParseException("A bindingConfig must have a key and a value");
}
String key = StringUtils.trim(entryParts[0]);
String value = StringUtils.trim(entryParts[1]);
value = StringUtils.removeStart(value, "\"");
value = StringUtils.removeEnd(value, "\"");
try {
helper.getClass().getDeclaredField(key).set(helper, value);
} catch (Exception e) {
throw new BindingConfigParseException("Could not set value " + value + " for attribute " + key);
}
}
if (!helper.isValid()) {
throw new BindingConfigParseException("Invalid binding: " + bindingConfig);
}
helper.type = StringUtils.replace(helper.type, "athmosphere", "atmosphere");
WeatherBindingConfig weatherConfig = null;
if (helper.isForecast()) {
Integer forecast = parseInteger(helper.forecast, bindingConfig);
if (forecast < 0) {
throw new BindingConfigParseException("Invalid binding, forecast must be >= 0: " + bindingConfig);
}
weatherConfig = new ForecastBindingConfig(helper.locationId, forecast, helper.type, helper.property);
} else {
weatherConfig = new WeatherBindingConfig(helper.locationId, helper.type, helper.property);
}
Weather validationInstance = new Weather(null);
String property = weatherConfig.getWeatherProperty();
if (!Weather.isVirtualProperty(property) && !PropertyUtils.hasProperty(validationInstance, property)) {
throw new BindingConfigParseException("Invalid binding, unknown type or property: " + bindingConfig);
}
boolean isDecimalTypeItem = item.getAcceptedDataTypes().contains(DecimalType.class);
if (isDecimalTypeItem || Weather.isVirtualProperty(property)) {
RoundingMode roundingMode = RoundingMode.HALF_UP;
if (helper.roundingMode != null) {
try {
roundingMode = RoundingMode.valueOf(StringUtils.upperCase(helper.roundingMode));
} catch (IllegalArgumentException ex) {
throw new BindingConfigParseException("Invalid binding, unknown roundingMode: " + bindingConfig);
}
}
Integer scale = 2;
if (helper.scale != null) {
scale = parseInteger(helper.scale, bindingConfig);
if (scale < 0) {
throw new BindingConfigParseException("Invalid binding, scale must be >= 0: " + bindingConfig);
}
}
weatherConfig.setScale(roundingMode, scale);
}
weatherConfig.setUnit(Unit.parse(helper.unit));
if (StringUtils.isNotBlank(helper.unit) && weatherConfig.getUnit() == null) {
throw new BindingConfigParseException("Invalid binding, unknown unit: " + bindingConfig);
}
try {
if (!Weather.isVirtualProperty(property) && weatherConfig.hasUnit()) {
String doubleTypeName = Double.class.getName();
String propertyTypeName = PropertyUtils.getPropertyTypeName(validationInstance, property);
if (!StringUtils.equals(doubleTypeName, propertyTypeName)) {
throw new BindingConfigParseException("Invalid binding, unit specified but property is not a double type: " + bindingConfig);
}
}
} catch (IllegalAccessException ex) {
logger.error(ex.getMessage(), ex);
throw new BindingConfigParseException(ex.getMessage());
}
return weatherConfig;
}
use of java.math.RoundingMode in project voltdb by VoltDB.
the class TestDecimalRoundingSuite method testEEDecimalScale.
public void testEEDecimalScale() throws Exception {
Boolean roundIsEnabled = Boolean.valueOf(m_defaultRoundingEnablement);
RoundingMode roundMode = RoundingMode.valueOf(m_defaultRoundingMode);
assert (m_config instanceof LocalCluster);
LocalCluster localCluster = (LocalCluster) m_config;
Map<String, String> props = localCluster.getAdditionalProcessEnv();
if (props != null) {
roundIsEnabled = Boolean.valueOf(props.containsKey(m_roundingEnabledProperty) ? props.get(m_roundingEnabledProperty) : "true");
roundMode = RoundingMode.valueOf(props.containsKey(m_roundingModeProperty) ? props.get(m_roundingModeProperty) : "HALF_UP");
}
doTestEEDecimalScale(roundIsEnabled, roundMode);
}
use of java.math.RoundingMode in project voltdb by VoltDB.
the class TestDecimalRoundingSuite method testDecimalScaleInsertion.
public void testDecimalScaleInsertion() throws Exception {
Boolean roundIsEnabled = Boolean.valueOf(m_defaultRoundingEnablement);
RoundingMode roundMode = RoundingMode.valueOf(m_defaultRoundingMode);
assert (m_config instanceof LocalCluster);
LocalCluster localCluster = (LocalCluster) m_config;
Map<String, String> props = localCluster.getAdditionalProcessEnv();
if (props != null) {
roundIsEnabled = Boolean.valueOf(props.containsKey(m_roundingEnabledProperty) ? props.get(m_roundingEnabledProperty) : "true");
roundMode = RoundingMode.valueOf(props.containsKey(m_roundingModeProperty) ? props.get(m_roundingModeProperty) : "HALF_UP");
System.out.printf("Rounding is %senabled, mode is %s\n", roundIsEnabled ? "" : "not ", roundMode.toString());
} else {
System.out.printf("Default rounding (%s), Default Rounding mode (%s).\n", roundIsEnabled.toString(), roundMode.toString());
}
doTestDecimalScaleInsertion(roundIsEnabled, roundMode);
}
use of java.math.RoundingMode in project Tundra by Permafrost.
the class measure method convert.
// ---( server methods )---
public static final void convert(IData pipeline) throws ServiceException {
// --- <<IS-START(convert)>> ---
// @subtype unknown
// @sigtype java 3.5
// [i] field:0:optional $value
// [i] field:0:required $unit.input {"t","kg","g","lb","oz","km","m","cm","mm","mi","yd","ft","in"}
// [i] field:0:required $unit.output {"t","kg","g","lb","oz","km","m","cm","mm","mi","yd","ft","in"}
// [i] field:0:optional $precision
// [i] field:0:optional $rounding {"HALF_UP","CEILING","DOWN","FLOOR","HALF_DOWN","HALF_EVEN","UNNECESSARY","UP"}
// [o] field:0:optional $result
IDataCursor cursor = pipeline.getCursor();
try {
BigDecimal value = IDataHelper.get(cursor, "$value", BigDecimal.class);
String inputUnit = IDataHelper.get(cursor, "$unit.input", String.class);
String outputUnit = IDataHelper.get(cursor, "$unit.output", String.class);
int precision = IDataHelper.getOrDefault(cursor, "$precision", Integer.class, -1);
RoundingMode rounding = IDataHelper.get(cursor, "$rounding", RoundingMode.class);
if (value != null) {
IDataHelper.put(cursor, "$result", BigDecimalHelper.round(MeasureHelper.convert(value, UnitHelper.parse(inputUnit), UnitHelper.parse(outputUnit), MathContext.DECIMAL128), precision, rounding), String.class);
}
} finally {
cursor.destroy();
}
// --- <<IS-END>> ---
}
Aggregations