no.abmu.questionnaire.propertyeditors.FloatAdvancedCustomNumberEditor.java Source code

Java tutorial

Introduction

Here is the source code for no.abmu.questionnaire.propertyeditors.FloatAdvancedCustomNumberEditor.java

Source

/*$Id: DateEditor.java 15543 2010-04-20 15:53:14Z jens $*/
/*
 ****************************************************************************
 *                                                                          *
 *                   (c) Copyright 2010 ABM-utvikling                       *
 *                                                                          *
 * This program is free software; you can redistribute it and/or modify it  *
 * under the terms of the GNU General Public License as published by the    *
 * Free Software Foundation; either version 2 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 General *
 * Public License for more details. http://www.gnu.org/licenses/gpl.html    *
 *                                                                          *
 ****************************************************************************
 */

package no.abmu.questionnaire.propertyeditors;

import java.beans.PropertyEditorSupport;
import java.text.DateFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.record.formula.functions.Char;
import org.springframework.beans.propertyeditors.CustomNumberEditor;

/**
 * DateEditor.
 *
 * @author Thomas Oldervoll
 * @author $Author: jens $
 * @version $Rev: 15543 $
 * $Date: 2010-04-20 17:53:14 +0200 (Tue, 20 Apr 2010) $
 * copyright ABM-Utvikling
 * @since 2009-02-01
 */
public class FloatAdvancedCustomNumberEditor extends CustomNumberEditor {

    private static final Log logger = (Log) LogFactory.getLog(FloatAdvancedCustomNumberEditor.class);

    private Pattern decimalSeparatorPattern;
    private Pattern groupingSeparatorPattern;
    private char decimalSeparator;
    private char groupingSeparator;
    private Pattern patternNonNumbers = Pattern.compile("\\D+");

    Locale locale;

    public FloatAdvancedCustomNumberEditor(Class numberClass, NumberFormat numberFormat, boolean allowEmpty,
            Locale locale) {
        super(numberClass, numberFormat, allowEmpty);
        this.locale = locale;
        DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(locale);
        this.decimalSeparator = ',';//decimalFormatSymbols.getDecimalSeparator();
        this.groupingSeparator = '.';//decimalFormatSymbols.getGroupingSeparator();
        decimalSeparatorPattern = Pattern.compile("[" + decimalSeparator + "]");
        groupingSeparatorPattern = Pattern.compile("[" + groupingSeparator + "]");
    }

    public FloatAdvancedCustomNumberEditor(Class numberClass, NumberFormat numberFormat, boolean allowEmpty) {
        super(numberClass, numberFormat, allowEmpty);
        this.decimalSeparator = ',';//decimalFormatSymbols.getDecimalSeparator();
        this.groupingSeparator = '.';//decimalFormatSymbols.getGroupingSeparator();
        decimalSeparatorPattern = Pattern.compile("[" + decimalSeparator + "]");
        groupingSeparatorPattern = Pattern.compile("[" + groupingSeparator + "]");
    }

    public String getAsText() {
        if (logger.isDebugEnabled()) {
            logger.debug("Execute getAsText getValue()=[" + getValue() + "]");
        }

        return super.getAsText();
    }

    public void setAsText(String string) throws IllegalArgumentException {
        if (logger.isDebugEnabled()) {
            logger.debug("Execute setAsText with string=[" + string + "]");
        }

        String trimmed = string.trim().replaceAll("(\\s|\\xA0|\\x20)+", "");

        boolean groupingPresent = false;

        //find any decimals or groupings
        Matcher matcherDecimalSeparatorPattern = decimalSeparatorPattern.matcher(trimmed);
        matcherDecimalSeparatorPattern.find();//find once
        Matcher matcherGroupingSeparatorPattern = groupingSeparatorPattern.matcher(trimmed);
        if (matcherGroupingSeparatorPattern.find()) {
            groupingPresent = true;
        }

        //if found again
        if (matcherDecimalSeparatorPattern.find() || matcherGroupingSeparatorPattern.find()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Found several separators or groupings " + trimmed);
            }
            throw new IllegalArgumentException("Found several separators or groupings " + trimmed);
        }

        if (groupingPresent) {
            trimmed = trimmed.replace(groupingSeparator, decimalSeparator);
        }

        //match against string without separators 
        Matcher nonNumbersMatcher = patternNonNumbers.matcher(trimmed.replace(decimalSeparator, '0'));
        if (nonNumbersMatcher.find()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Only numbers allowed, got " + trimmed);
            }
            throw new IllegalArgumentException("Only numbers allowed, got " + trimmed);
        }

        if (trimmed.contains(String.valueOf(decimalSeparator))
                && trimmed.substring(trimmed.indexOf(decimalSeparator)).length() > 3) {
            if (logger.isDebugEnabled()) {
                logger.debug("Only 2 decimal points allowed " + trimmed);
            }
            throw new IllegalArgumentException("Only 2 decimal points allowed " + trimmed);
        }

        super.setAsText(trimmed);
    }
}