001: /* NumberPropertyEditor.java
002: *
003: * DDSteps - Data Driven JUnit Test Steps
004: * Copyright (C) 2005 Jayway AB
005: * www.ddsteps.org
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License version 2.1 as published by the Free Software Foundation.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, visit
018: * http://www.opensource.org/licenses/lgpl-license.php
019: */
020:
021: package org.ddsteps.beans;
022:
023: import java.text.NumberFormat;
024:
025: import org.springframework.beans.propertyeditors.CustomNumberEditor;
026:
027: /**
028: * @author adam
029: * @version $Id: NumberPropertyEditor.java,v 1.1 2005/12/03 12:51:41 adamskogman Exp $
030: */
031: public class NumberPropertyEditor extends CustomNumberEditor {
032:
033: /**
034: * The target class. Don't you just hate it when fields are private, not
035: * protected, and there is no getter. Not even a protected getter... So we
036: * duplicate it here.
037: */
038: protected final Class targetClass;
039:
040: /**
041: * @param numberClass
042: * The TARGET class.
043: * @param allowEmpty
044: * @throws IllegalArgumentException
045: */
046: public NumberPropertyEditor(Class numberClass, boolean allowEmpty)
047: throws IllegalArgumentException {
048: super (numberClass, allowEmpty);
049: this .targetClass = numberClass;
050:
051: }
052:
053: /**
054: * @param numberClass
055: * The TARGET class.
056: * @param numberFormat
057: * @param allowEmpty
058: * @throws IllegalArgumentException
059: */
060: public NumberPropertyEditor(Class numberClass,
061: NumberFormat numberFormat, boolean allowEmpty)
062: throws IllegalArgumentException {
063: super (numberClass, numberFormat, allowEmpty);
064: this .targetClass = numberClass;
065: }
066:
067: /**
068: * Adds Number to Number conversions.
069: *
070: * Spring says to do this (read the comments in BeanWrapperImpl), if you
071: * want to do Object to Object conversions, and not go through a String.
072: *
073: * In this case, we handle all kinds of Numbers (that is native and wrappers
074: * for byte, short, ..., long, float and double.
075: *
076: * @see java.beans.PropertyEditor#setValue(java.lang.Object)
077: */
078: public void setValue(Object value) {
079:
080: if (value instanceof Number) {
081: Number number = (Number) value;
082:
083: // This smells. Long if-else alsways smells. Someday, this will go
084: // away. Or else we'll have to make it do so... :-)
085:
086: // OK, it's a number, so let's use the conversion methods
087: // in the java.lang.Number base class.
088:
089: // We don't need to check for the native types (int, long...)
090: // since the target class is always a Wrapper type.
091: if (targetClass == Integer.class) {
092: value = new Integer(number.intValue());
093: } else if (targetClass == Long.class) {
094: value = new Long(number.longValue());
095: } else if (targetClass == Float.class) {
096: value = new Float(number.floatValue());
097: } else if (targetClass == Double.class) {
098: value = new Double(number.doubleValue());
099: } else if (targetClass == Short.class) {
100: value = new Short(number.shortValue());
101: } else if (targetClass == Byte.class) {
102: value = new Byte(number.byteValue());
103: }
104: }
105:
106: super.setValue(value);
107: }
108: }
|