Java泛型和数字类型 [英] Java generics and numeric types

查看:85
本文介绍了Java泛型和数字类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class MyClass {
$ b我想创建一个通用的方法$ b static< T extends Number> T sloppyParseNumber(String str){
try {
return T.valueOf(str);
} catch(Exception e){
return(T)0;



$ $ $
$ b $现在上面不编译为原因有两个:没有 Number.valueOf()方法,0不能转换为 T



示例用法:

 字符串值=0.00000001; 

System.out.println(Double:+ MyClass。< Double> sloppyParseNumber(value));
System.out.println(Float:+ MyClass。< Float> sloppyParseNumber(value));

double d = MyClass.sloppyParseNumber(value);
float f = MyClass.sloppyParseNumber(value);

在Java中实现上述通用方法可能吗?如果是,如何?如果不是,有什么好的替代方法?






编辑:似乎有一些可能的重复项,但我没有找到一个,其中涵盖了这一点。我希望有一些技巧可以解决,这将允许这两个操作:将字符串解析为 Number 子类,并为数字返回0值子类。

解决方案

我同意TofuBeer 100%但是如果你想避免冗长的话,这应该也是这样:

  static< T extends Number> T sloppyParseNumber(String str,Class< T> class){

if(clas == null)throw new NullPointerException(clas is null);

{b
$ b if(clas.equals(Integer.class)){
return(T)Integer.valueOf(str);
}
else if(clas.equals(Double.class)){
return(T)Double.valueOf(str);

//
$ b $ catch(NumberFormatException | NullPointerException ex){
//强制使用有效参数调用
return sloppyParseNumber(0 ,clas);
}

抛出新的IllegalArgumentException(无效的分类+ clas);


但纯粹来自 T ,你不能在运行时获得类型。


I'd like to create a generic method which does effectively this:

class MyClass {

    static <T extends Number> T sloppyParseNumber(String str) {
        try {
            return T.valueOf(str);
        } catch (Exception e) {
            return (T)0;
        }
    }
}

Now above does not compile for two reasons: there's no Number.valueOf() method and 0 can't be cast to T.

Example usage:

String value = "0.00000001";

System.out.println("Double: " + MyClass.<Double>sloppyParseNumber(value));
System.out.println("Float: " + MyClass.<Float>sloppyParseNumber(value));

double d = MyClass.sloppyParseNumber(value);
float f = MyClass.sloppyParseNumber(value);

Is implementing above generic method possible with Java? If yes, how? If no, what's a good alternative approach?


Edit: there seems to be a few possible duplicates, but I did not find one, which covers exactly this. I'm hoping there's some trick to pull, which would allow these two operations: parse string to a Number subclass, and return 0 value for a Number subclass.

解决方案

I agree 100% with TofuBeer. But in case you wish to avoid verbosity for time sake, this should also do:

static <T extends Number> T sloppyParseNumber(String str,Class<T> clas) {

    if (clas == null) throw new NullPointerException("clas is null");

    try {

        if(clas.equals(Integer.class)) {
            return (T) Integer.valueOf(str);
        }
        else if(clas.equals(Double.class)) {
            return (T) Double.valueOf(str);
        }
        //so on

    catch(NumberFormatException|NullPointerException ex) {
        // force call with valid arguments
        return sloppyParseNumber("0", clas);
    }

    throw new IllegalArgumentException("Invalid clas " + clas);

}

But purely from T, you cannot get the type at runtime.

这篇关于Java泛型和数字类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆