01: package it.unimi.dsi.mg4j.search.score;
02:
03: import java.util.Comparator;
04:
05: /*
06: * MG4J: Managing Gigabytes for Java
07: *
08: * Copyright (C) 2005-2007 Sebastiano Vigna
09: *
10: * This program is free software; you can redistribute it and/or modify it
11: * under the terms of the GNU General Public License as published by the Free
12: * Software Foundation; either version 2 of the License, or (at your option)
13: * any later version.
14: *
15: * This program is distributed in the hope that it will be useful, but
16: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18: * for more details.
19: *
20: * You should have received a copy of the GNU General Public License
21: * along with this program; if not, write to the Free Software
22: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23: *
24: */
25:
26: /** A container used to return scored results with additional information. */
27:
28: public final class DocumentScoreInfo<T> {
29: /** The index of the document. */
30: public int document;
31: /** Its score. */
32: public double score;
33: /** Optional additional information. */
34: public T info;
35:
36: public DocumentScoreInfo(final int document, final double score,
37: final T info) {
38: this .document = document;
39: this .score = score;
40: this .info = info;
41: }
42:
43: public DocumentScoreInfo(final int document, final double score) {
44: this .document = document;
45: this .score = score;
46: }
47:
48: public DocumentScoreInfo(final int document) {
49: this .document = document;
50: this .score = -1;
51: }
52:
53: public String toString() {
54: return "[Document: " + document + "; score: " + score
55: + "; info: " + info + "]";
56: }
57:
58: /** A comparator that sorts {@link DocumentScoreInfo} instances by <em>increasing</em> document number. */
59:
60: public static final Comparator<DocumentScoreInfo<?>> DOCUMENT_COMPARATOR = new Comparator<DocumentScoreInfo<?>>() {
61: public int compare(final DocumentScoreInfo<?> dsi0,
62: final DocumentScoreInfo<?> dsi1) {
63: return dsi0.document - dsi1.document;
64: }
65: };
66:
67: /** A comparator that sorts {@link DocumentScoreInfo} instances by increasing score order and then by <em>decreasing</em> document order. */
68: public static final Comparator<DocumentScoreInfo<?>> SCORE_DOCUMENT_COMPARATOR = new Comparator<DocumentScoreInfo<?>>() {
69: public int compare(final DocumentScoreInfo<?> x,
70: final DocumentScoreInfo<?> y) {
71: if (x.score < y.score)
72: return -1;
73: if (x.score > y.score)
74: return 1;
75: // Note that we want document in *increasing* score, but *decreasing* document number.
76: return y.document - x.document;
77: }
78: };
79:
80: }
|