All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.primefaces.util.BigDecimalValidator Maven / Gradle / Ivy

There is a newer version: 14.0.7
Show newest version
/*
 * The MIT License
 *
 * Copyright (c) 2009-2024 PrimeTek Informatics
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.primefaces.util;


import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParsePosition;

/**
 * 

BigDecimal Validation and Conversion routines (java.math.BigDecimal).

* *

This validator provides a number of methods for * validating/converting a String value to * a BigDecimal using a provided java.text.DecimalFormat.

* *

Fraction/decimal values are automatically trimmed to the appropriate length.

* * Modified from Commons Validator to fit PF needs * @see Commons Validator */ class BigDecimalValidator implements Serializable { private static final long serialVersionUID = -670320911490506772L; private static final BigDecimalValidator VALIDATOR = new BigDecimalValidator(); /** * Return a singleton instance of this validator. * * @return A singleton instance of the BigDecimalValidator. */ public static BigDecimalValidator getInstance() { return VALIDATOR; } /** *

* Validate/convert a BigDecimal using the specified DecimalFormat. * * @param value The value validation is being performed on. * @param format The format used to validate the value against. * @return The parsed BigDecimal if valid or null if invalid. */ public BigDecimal validate(String value, DecimalFormat format) { return (BigDecimal) parse(value, format); } /** *

* Parse the value with the specified DecimalFormat. *

* * @param value The value to be parsed. * @param formatter The Format to parse the value with. * @return The parsed value if valid or null if invalid. */ protected Number parse(String value, DecimalFormat formatter) { ParsePosition pos = new ParsePosition(0); Number parsedValue = formatter.parse(value, pos); if (pos.getErrorIndex() > -1) { return null; } if (pos.getIndex() < value.length()) { return null; } if (parsedValue != null) { if (Double.isInfinite(parsedValue.doubleValue())) { return null; } parsedValue = processParsedValue(parsedValue, formatter); } return parsedValue; } /** * Convert the parsed value to a BigDecimal. * * @param value The parsed Number object created. * @param formatter The Format used to parse the value with. * @return The parsed Number converted to a BigDecimal. */ protected BigDecimal processParsedValue(Number value, DecimalFormat formatter) { BigDecimal decimal; if (value instanceof Long) { decimal = BigDecimal.valueOf((Long) value); } else { decimal = new BigDecimal(value.toString()); } int scale = determineScale(formatter); if (scale >= 0) { decimal = decimal.setScale(scale, RoundingMode.DOWN); } return decimal; } /** *

* Returns the multiplier of the NumberFormat. *

* * @param format The NumberFormat to determine the multiplier of. * @return The multiplying factor for the format. */ protected int determineScale(NumberFormat format) { int minimumFraction = format.getMinimumFractionDigits(); int maximumFraction = format.getMaximumFractionDigits(); if (minimumFraction != maximumFraction) { return -1; } int scale = minimumFraction; if (format instanceof DecimalFormat) { int multiplier = ((DecimalFormat) format).getMultiplier(); if (multiplier == 100) { // CHECKSTYLE IGNORE MagicNumber scale += 2; // CHECKSTYLE IGNORE MagicNumber } else if (multiplier == 1000) { // CHECKSTYLE IGNORE MagicNumber scale += 3; // CHECKSTYLE IGNORE MagicNumber } } return scale; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy