English 中文(简体)
设法使 Java方法具有通用性,以便比较长处或日期
原标题:Trying to make my Java method generic so that it can compare Strings or Dates
  • 时间:2012-01-13 17:11:34
  •  标签:
  • java

以下是我采用的方法。 它对比较“强项”做了罚款。 我也希望能够比较对日期或可能具有可接受的定义的任何类别。 方法。 寻找一种容易的办法。 我正走一条ck路。 还可以接受任何其他改进这一方法的建议。

protected <E> int compareFields(E o1, E o2,String fieldName){
    String o1Data;
    String o2Data;
    try { 
        o1Data = (String) o1.getClass().getMethod(fieldName).invoke(o1);
        o2Data = (String) o2.getClass().getMethod(fieldName).invoke(o2);
    }
    catch(Exception e) {
        throw new RuntimeException(e);
    }
    if(o1Data == null && o2Data == null){
        return 0;
    } else if (o1Data == null){
        return 1;
    } else if (o2Data == null){
        return -1;
    }
    return o2Data.compareTo(o1Data);
}
最佳回答

Do you mean like the following?

protected <E> int compareFields(E o1, E o2, String fieldName){
    try { 
        Comparable o1Data = (Comparable) o1.getClass().getMethod(fieldName).invoke(o1);
        Comparable o2Data = (Comparable) o2.getClass().getMethod(fieldName).invoke(o2);
        return o1Data == null ? o2Data == null ? 0 : 1 :
               o2Data == null ? -1 : o1Data.compareTo(o2Data);
    } catch(Exception e) {
        throw new RuntimeException(e);
    }
}

我假定你不想推翻你的数据。

问题回答

为什么不使用<代码>Comparable<T>,而这正是为此目的而使用的?

为什么不只写一个可比较的组合:

public class ComparableComparator implements Comparator<Comparable> {

    public int compare( Comparable c1, Comparable c2 ) {
        if( c1 == null && c2 != null ) {
            return 1;
        } else if( c1 != null && c2 == null ) {
            return -1;
        } else if( c1 == null && c2 == null ) {
            return 0;
        } else {
            return c1.compare( c2 );
        }
    }
}

采用可比较的类别可能是一个更好的解决办法,但是,在你试图屈服于一种强硬,而不是依靠试捕来进行逻辑流动之前,你也应进行测试。

http://www.o1.org。 String && o2 instanceof String){

不要对像现在这样的事情进行思考!

要么向该方法提供合适的<代码>Comparator,要么为提取相关财产提供某种方法(可按原类型计算),要么两者兼有。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签