001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.util.convert.converters;
018:
019: import java.text.NumberFormat;
020: import java.util.Locale;
021:
022: import org.apache.wicket.util.convert.ConversionException;
023:
024: /**
025: * Base class for all number converters.
026: *
027: * @author Jonathan Locke
028: */
029: public abstract class AbstractNumberConverter extends AbstractConverter {
030: /**
031: * @param locale
032: * @return Returns the numberFormat.
033: */
034: public abstract NumberFormat getNumberFormat(Locale locale);
035:
036: /**
037: * Parses a value as a String and returns a Number.
038: *
039: * @param value
040: * The object to parse (after converting with toString())
041: * @param min
042: * The minimum allowed value
043: * @param max
044: * The maximum allowed value
045: * @param locale
046: * @return The number
047: * @throws ConversionException
048: * if value is unparsable or out of range
049: */
050: protected Number parse(Object value, final double min,
051: final double max, Locale locale) {
052: if (locale == null) {
053: locale = Locale.getDefault();
054: }
055:
056: final NumberFormat numberFormat = getNumberFormat(locale);
057:
058: if (value instanceof String) {
059: // Convert spaces to no-break space (U+00A0) to fix problems with
060: // broser conversions.
061: // Space is not valid thousands-separator, but no-br space is.
062: String v = (String) value;
063: value = v.replace(' ', '\u00A0');
064: }
065:
066: final Number number = (Number) parse(numberFormat, value,
067: locale);
068:
069: if (number == null) {
070: return null;
071: }
072:
073: if (number.doubleValue() < min) {
074: throw newConversionException(
075: "Value cannot be less than " + min, value, locale)
076: .setFormat(numberFormat);
077: }
078:
079: if (number.doubleValue() > max) {
080: throw newConversionException(
081: "Value cannot be greater than " + max, value,
082: locale).setFormat(numberFormat);
083: }
084:
085: return number;
086: }
087:
088: /**
089: * @see org.apache.wicket.util.convert.IConverter#convertToString(java.lang.Object,
090: * Locale)
091: */
092: public String convertToString(final Object value, Locale locale) {
093: NumberFormat fmt = getNumberFormat(locale);
094: if (fmt != null) {
095: return fmt.format(value);
096: }
097: return value.toString();
098: }
099:
100: }
|