From: <ki...@us...> - 2012-11-02 14:24:51
|
Revision: 3864 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3864&view=rev Author: kirdie Date: 2012-11-02 14:24:44 +0000 (Fri, 02 Nov 2012) Log Message: ----------- added Comparable interface, equals and hashcode. Modified Paths: -------------- branches/hmm/components-ext/src/main/java/org/dllearner/common/index/IndexResultItem.java Modified: branches/hmm/components-ext/src/main/java/org/dllearner/common/index/IndexResultItem.java =================================================================== --- branches/hmm/components-ext/src/main/java/org/dllearner/common/index/IndexResultItem.java 2012-11-01 10:22:25 UTC (rev 3863) +++ branches/hmm/components-ext/src/main/java/org/dllearner/common/index/IndexResultItem.java 2012-11-02 14:24:44 UTC (rev 3864) @@ -3,7 +3,10 @@ import java.util.Collections; import java.util.Map; -public class IndexResultItem { +/** Natural ordering is negated natural order of scores (highest to lowest score) if score is different, else lexical order of urls if urls are equal else lexical ordering of labels. + * This keeps the ordering consistent with equals and hashCode because those ignore the fields too. **/ +public class IndexResultItem implements Comparable<IndexResultItem> +{ private final String uri; private final String label; private final float score; @@ -26,9 +29,52 @@ public float getScore() {return score;} public Map<String,? extends Object> getFields() {return fields;} - @Override public String toString() + @Override public String toString() {return "label:" + label + "--uri:" + uri + "--fields:" + fields;} + + @Override public int compareTo(IndexResultItem item) { - // TODO Auto-generated method stub - return "label:" + label + "--uri:" + uri + "--fields:" + fields; + int i; + i = -Float.compare(this.score, item.score); + if(i!=0) return i; + i = this.uri.compareTo(item.uri); + if(i!=0) return i; + i = this.label.compareTo(item.label); + return i; } -} + + @Override public int hashCode() + { + final int prime = 31; + int result = 1; +// result = prime * result + ((fields == null) ? 0 : fields.hashCode()); + result = prime * result + ((label == null) ? 0 : label.hashCode()); + result = prime * result + Float.floatToIntBits(score); + result = prime * result + ((uri == null) ? 0 : uri.hashCode()); + return result; + } + + @Override public boolean equals(Object obj) + { + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; + IndexResultItem other = (IndexResultItem) obj; +// if (fields == null) +// { +// if (other.fields != null) return false; +// } +// else if (!fields.equals(other.fields)) return false; + if (label == null) + { + if (other.label != null) return false; + } + else if (!label.equals(other.label)) return false; + if (Float.floatToIntBits(score) != Float.floatToIntBits(other.score)) return false; + if (uri == null) + { + if (other.uri != null) return false; + } + else if (!uri.equals(other.uri)) return false; + return true; + } +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |