2012-02-01 107 views
1

我得到的比較方法違反了它與此compareTo方法的一般合同例外,但我無法追查究竟是什麼導致了問題。我正在嘗試按特定方式按擴展名排序文件。請注意,這不會發生在所有手機上,只有那些我無法使用的手機會使其難以測試。比較方法違反了其總體合同

public int compareTo(NzbFile another) 
{ 
    if (this.getFileName() != null && another.getFileName() != null) 
    { 
     if (this.getFileName().toLowerCase().endsWith(".nfo")) 
      return -1000; 
     else if (another.getFileName().toLowerCase().endsWith(".nfo")) 
      return 1000; 
     else if (this.getFileName().toLowerCase().endsWith(".sfv")) 
      return -999; 
     else if (another.getFileName().toLowerCase().endsWith(".sfv")) 
      return 1001; 
     else if (this.getFileName().toLowerCase().endsWith(".srr")) 
      return -998; 
     else if (another.getFileName().toLowerCase().endsWith(".srr")) 
      return 1002; 
     else if (this.getFileName().toLowerCase().endsWith(".nzb")) 
      return -997; 
     else if (another.getFileName().toLowerCase().endsWith(".nzb")) 
      return 1003; 
     else if (this.getFileName().toLowerCase().endsWith(".srt")) 
      return -996; 
     else if (another.getFileName().toLowerCase().endsWith(".srt")) 
      return 1004; 
     else 
      return this.getFileName().compareTo(another.getFileName()); 
    } 
    else if (this.getFileName() != null && another.getFileName() == null) 
    { 
     return -995; 
    } 
    else if (this.getFileName() == null && another.getFileName() != null) 
    { 
     return 1005; 
    } 
    else 
    { 
     return this.getSubject().compareTo(another.getSubject()); 
    } 

} 

回答

5

如果您的文件名相同,並且例如結束於.nfo,那麼這將返回它們不相等。我認爲他們是平等的。

我強烈懷疑有更好的方法來做到這一點。對於我的示例,我將使用Guava,但這不是嚴格必要的。

static final List<String> EXTENSIONS = ImmutableList.of("nfo", "sfv", "srr", "nzb", "srt"); 
final Ordering<String> fileNameOrdering = new Ordering<String>() { 
    public int compare(String a, String b) { 
    String extA = Files.getFileExtension(a); 
    String extB = Files.getFileExtension(b); 
    int extAIndex = EXTENSIONS.indexOf(extA); 
    int extBIndex = EXTENSIONS.indexOf(extB); 
    if ((extAIndex >= 0) == (extBIndex >= 0)) { // if they are both known extensions or both unknown 
     return extAIndex - extBIndex; 
    } else if (extAIndex < 0) { // a is unknown, b is known 
     return -1; 
    } else if (extBIndex < 0) { // b is unknown, a is known 
     return 1; 
    } 
    return a.compareTo(b); 
    } 
}.nullsLast(); 
return new Ordering<NzbFile>() { 
    public int compare(NzbFile a, NzbFile b) { 
    return ComparisonChain.start() 
     .compare(a.getFileName(), b.getFileName(), fileNameOrdering) 
     .compare(a.getSubject(), b.getSubject()) 
     .result(); 
    } 
}; 
+0

其實這並不給我我想要的,應該詳細闡述。我在代碼中給出這麼高/低的數字的原因是因爲我希望這些擴展名的文件位於排序列表的頂部,而其他文件擴展名如.rar 我認爲最簡單的解決方案是將特殊文件從清單之前排序其餘的? – user1159819 2012-02-02 18:30:57

+1

更高和更低的比較差異不會使_any_變化,除非您正在編寫自己的排序函數或其他。根據Comparator的合同,返回'-1'和返回'-1000'沒有實際區別。 – 2012-02-02 18:52:57

+0

東西是不同的,因爲我的函數將這些特殊文件排序到頂部,以及其他文件,如.r01,.r02等下面的那些特殊文件,其中你的函數按照它們的文件擴展名排序,即sfv和nfo在.r01下面排序。 。 – user1159819 2012-02-02 19:16:23

2

實現程序必須確保sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y.

這似乎並不如果比較相同的擴展兩個文件的情況發生。比較說,a.nfob.nfo返回-1000,兩種方式。