Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2004-2008 the original author or authors.
   *
   * 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 org.springframework.binding.convert.converters;
 
 
A converter for common number types such as integers and big decimals. Allows the configuration of an explicit number pattern and locale. Works with a general purpose java.text.DecimalFormat instance returned by calling java.text.NumberFormat.getInstance(java.util.Locale) by default. This instance supports parsing any number type generally and will not perform special type-specific logic such as rounding or truncation. Subclasses may override. Will coerse parsed Numbers to the desired numberClass as necessary. If type-coersion results in an overflow condition; for example, what can occur with a Long being coersed to a Short, an exception will be thrown.

Author(s):
Keith Donald
See also:
java.text.NumberFormat
java.text.DecimalFormat
 
 public class FormattedStringToNumber extends StringToObject {
 
 	private static Log logger = LogFactory.getLog(FormattedStringToNumber.class);
 
 
 	private boolean lenient;
 
 		super(Number.class);
 	}
 
 	public FormattedStringToNumber(Class numberClass) {
 		super(numberClass);
 	}

Sets the factory that returns the java.text.NumberFormat instance that will format numbers handled by this converter.

Parameters:
numberFormatFactory the number format factory
 
 	public void setNumberFormatFactory(NumberFormatFactory numberFormatFactory) {
 		this. = numberFormatFactory;
 	}

If this Converter is "lenient" in parsing number strings. A lenient converter does not require that all characters in the String be parsed successfully. Default is false.

Returns:
the lenient flag
 
 	public boolean getLenient() {
 		return ;
 	}

Sets if this Converter should parse leniently.

Parameters:
lenient the lenient flag
 
 	public void setLenient(boolean lenient) {
 		this. = lenient;
 	}
 
 	protected Object toObject(String stringClass targetClassthrows Exception {
 		ParsePosition parsePosition = new ParsePosition(0);
 		Number number = format.parse(stringparsePosition);
 		if (number == null) {
 			// no object could be parsed
 			throw new InvalidFormatException(stringgetPattern(format));
 		}
 		if (!) {
 			if (string.length() != parsePosition.getIndex()) {
 				// indicates a part of the string that was not parsed; e.g. ".5" in 1234.5 when parsing an Integer
 				throw new InvalidFormatException(stringgetPattern(format));
 			}
		}
		return convertToNumberClass(numbertargetClass);
	}
	protected String toString(Object objectthrows Exception {
		Number number = (Numberobject;
	}

Coerces the Number object returned by NumberFormat to the desired numberClass. Subclasses may override.

Parameters:
number the parsed number
Returns:
the coersed number
Throws:
java.lang.IllegalArgumentException when an overflow condition occurs during coersion
	protected Number convertToNumberClass(Number numberClass numberClassthrows IllegalArgumentException {
		return NumberUtils.convertNumberToTargetClass(numbernumberClass);
	}
	// internal helpers
	private String getPattern(NumberFormat format) {
		if (format instanceof DecimalFormat) {
			return ((DecimalFormatformat).toPattern();
else {
			.warn("Pattern string cannot be determined because NumberFormat is not a DecimalFormat");
			return "defaultNumberFormatInstance";
		}
	}
New to GrepCode? Check out our FAQ X