初用org.apache.commons.beanutils.Convert

org.apache.commons.beanutils.ConvertUtils

这是一个接口,其中只包含一个方法convert(java.lang.Class type, java.lang.Object value),其功能是将输入参数value转换为指定类型type的实例。

范例:

我建立了一个Rectangle对象,并为这rectangle对象建立了一个RectangleConverter,随后将RectangleConverter注册进ConvertUtils中。形为{3,2}的字符串可以转变为一个rectangle

这里唯一要说明的就是RectangleConverter中要实现两个converter方法,也就是要对接口Converter中定义的方法进行overload,从而可以实现字符串和Rectangle之间的互相转换了。

代码如下:

package beanutil;

 

 

import java.util.StringTokenizer;

 

 

import org.apache.commons.beanutils.ConvertUtils;

import org.apache.commons.beanutils.Converter;

 

 

/**

 * @author samepoint

 *

 */

public class RectangleConverter implements Converter {

 

 

       public Object convert(Class arg0, Object arg1) {

              if (arg1 instanceof String)

                     return convert(arg0, arg1.toString());

              return convert(arg0, (Rectangle) arg1);

       }

       /**

        * convert a string to a instance of rectangle

        * @param rectangleType

        * @param para

        * @return

        */

       public Rectangle convert(Class rectangleType, String para) {

              StringTokenizer token = new StringTokenizer(para, ",");

              Double temp1, temp2 = null;

              temp1 = (Double) ConvertUtils.convert(token.nextToken(), Double.class);

              temp2 = (Double) ConvertUtils.convert(token.nextToken(), Double.class);

              Rectangle rect = new Rectangle();

              rect.setLength(Math.max(temp1.doubleValue(), temp2.doubleValue()));

              rect.setWidth(Math.min(temp1.doubleValue(), temp2.doubleValue()));

              return rect;

       }

       /**

        * convert a instance of rectangle to string

        * @param string

        * @param rect

        * @return

        */

       public String convert(Class string, Rectangle rect) {

              StringBuffer buffer = new StringBuffer("(");

              buffer.append(rect.getLength()).append(",");

              buffer.append(rect.getWidth());

              return buffer.toString();

       }

 

 

}

 

 

 

RectangleConverter注册到ConvertUtils中,我们就可以把形为{4,5}的字符串转换为Rectangle的实例,同样可以将Rectangle的实例转换为形为{4,5}的字符串。

 

 

另附Rectangle的代码:

/*

 * Created on 2005-7-5

 */

package beanutil;

 

 

/**

 * @author samepoint

 */

public class Rectangle {

       double length = 0.0d;

       double width = 0.0d;

      

       /**

        * @return Returns the length.

        */

       public double getLength() {

              return length;

       }

       /**

        * @param length The length to set.

        */

       public void setLength(double length) {

              this.length = length;

       }

       /**

        * @return Returns the width.

        */

       public double getWidth() {

              return width;

       }

       /**

        * @param width The width to set.

        */

       public void setWidth(double width) {

              this.width = width;

       }

      

       public double computeArea(){

              return length*width;

       }

      

       public String toString(){

              StringBuffer buffer = new StringBuffer("(");

              buffer.append(getLength()).append(",");

              buffer.append(getWidth()).append(")");

              return buffer.toString();

       }

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值