从遗留代码到通用的Java通用转换

时间:2014-05-12 15:55:08

标签: java generics

如何将旧版枚举转换为“通用”枚举?我想确保枚举中每个元素的类型都是正确的。我想确保在代码中没有进一步的运行时转换错误,特别是当我没有捕获它们时。

这是我的示例代码。

import java.util.Enumeration;
import java.util.Vector;

public class TestEnumerationCast {

public static void main(String[] args) {
    new TestEnumerationCast();
}

{
    Vector stringVector = new Vector();
    stringVector.add("A");
    stringVector.add("B");
    stringVector.add("C");
    stringVector.add(new Integer(1));

    Enumeration<String> enumerationString = castEnumeration(stringVector.elements());
    while (enumerationString.hasMoreElements()) {
        String stringToPrint = enumerationString.nextElement();
        System.out.println(stringToPrint);
    }

}

private <T> Enumeration<T> castEnumeration(Enumeration<?> elements) {
    Vector<T> converstionVector = new Vector<T>();
    while (elements.hasMoreElements()) {
        try {
            converstionVector.add((T) elements.nextElement());
        } catch (Exception e) {
        }
    }
    return converstionVector.elements();
}

}

我认为方法castEnumeration会将遗留代码“转换”为任何类型的通用代码。简单地说,我遍历每个元素,试图将其转换为(T)。如果失败,我抛出一个运行时异常,但只跳过该元素。然后我有一个只有类型的枚举。但是,将类型添加到向量的行不会捕获整数。我仍然在最后一个元素的字符串转换中得到一个运行时异常,一个整数。

我知道我可以直接转换为泛型类型,忽略错误等等。所有这些都是有效的方法。但我想确保在我不寻找它时不会得到运行时异常。感谢

1 个答案:

答案 0 :(得分:2)

啊,发现它!您必须在转换中将要使用的类指定为方法参数,然后使用.cast(obj)方法。

更改来电
private <T> Enumeration<T> castEnumeration(Enumeration<?> elements) {
    Vector<T> converstionVector = new Vector<T>();
    while (elements.hasMoreElements()) {
        try {
            converstionVector.add((T) elements.nextElement());
        } catch (Exception e) {
        }
    }
    return converstionVector.elements();
}

private <T> Enumeration<T> castEnumeration(Enumeration<?> elements, Class<T> tClass) {
    Vector<T> converstionVector = new Vector<T>();
    while (elements.hasMoreElements()) {
        try {
            converstionVector.add(tClass.cast(elements.nextElement()));
        } catch (Exception e) {
        }
    }
    return converstionVector.elements();
}

并且还从

更改方法的调用
Enumeration<String> enumerationString = castEnumeration(stringVector.elements());

Enumeration<String> enumerationString = castEnumeration(stringVector.elements(), String.class);

总的来说,代码现在看起来像。

import java.util.Enumeration;
import java.util.Vector;

public class TestEnumerationCast {

public static void main(String[] args) {
    new TestEnumerationCast();
}

{
    Vector stringVector = new Vector();
    stringVector.add("A");
    stringVector.add("B");
    stringVector.add("C");
    stringVector.add(new Integer(1));

    Enumeration<String> enumerationString2 = castEnumeration(stringVector.elements(), String.class);
    while (enumerationString2.hasMoreElements()) {
        String stringToPrint = enumerationString2.nextElement();
        System.out.println(stringToPrint);
    }

}

private <T> Enumeration<T> castEnumeration(Enumeration<?> elements, Class<T> tClass) {
    Vector<T> converstionVector = new Vector<T>();
    while (elements.hasMoreElements()) {
        try {
            converstionVector.add(tClass.cast(elements.nextElement()));
        } catch (Exception e) {
        }
    }
    return converstionVector.elements();
}

}
相关问题