1 package net.obsearch.result;
2 import net.obsearch.AbstractOBResult;
3 import net.obsearch.ob.OBDouble;
4
5 /*
6 OBSearch: a distributed similarity search engine
7 This project is to similarity search what 'bit-torrent' is to downloads.
8 Copyright (C) 2007 Arnoldo Jose Muller Molina
9
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23 /**
24 * This class is used to store a single match result. A single result consists
25 * of the object found, the distance of this object with the query and an
26 * internal id.
27 * Inverted results will put first smaller values in the
28 * priority queue.
29 * @author Arnoldo Jose Muller Molina
30 * @since 0.7
31 */
32
33
34 public final class OBResultInvertedDouble<O> extends AbstractOBResult<O> {
35
36
37 /**
38 * Distance of the object found and the query.
39 */
40 protected double distance;
41
42 /**
43 * Default constructor. Used mostly to minimize the amount of object
44 * creations.
45 */
46 public OBResultInvertedDouble(){
47 }
48
49 /**
50 * Create a new OBResultDouble.
51 * @param object
52 * the result found.
53 * @param id
54 * The internal id of the result.
55 * @param distance
56 * Distance of the result and the original query.
57 */
58 public OBResultInvertedDouble(O object, long id, double distance){
59
60 super(object,id);
61 this.distance = distance;
62
63 }
64
65 /**
66 * @return The distance of the result and the original query.
67 */
68 public final double getDistance(){
69 return distance;
70 }
71
72 /**
73 * Sets the distance to a new value x.
74 * @param x
75 * The new value to set.
76 */
77 public final void setDistance(double x){
78 this.distance = x;
79 }
80
81 /**
82 * We implement the interface comparable so we provide this method. The
83 * only difference is that we return bigger objects first. (The
84 * comparable contract is multiplied by -1)
85 * @param o
86 * The object that will be compared.
87 * @return 1 if this object is smaller than o 0 if this object is equal
88 * than o -1 if this object is greater than o
89 */
90 public int compareTo(Object o) {
91 assert o instanceof OBResultInvertedDouble;
92 OBResultInvertedDouble<O> comp = (OBResultInvertedDouble<O>) o;
93 int res = 0;
94 if (distance < comp.distance) {
95 res = -1;
96 } else if (distance > comp.distance) {
97 res = 1;
98 }
99 return res;
100 }
101
102 /**
103 * @return the hash code of this object.
104 */
105 /*public int hashCode(){
106 return (int) distance;
107 }*/
108
109
110
111 /**
112 * We do not care about the object itself, just that both objects are at
113 * the same distance from the query.
114 * @return true if both distances are the same.
115 */
116 /*public boolean equals(Object obj){
117 OBResultInvertedDouble<O> comp = (OBResultInvertedDouble<O>) obj;
118 // a result object is the same if the distance is the same
119 // we do not care about the id.
120 return distance == comp.distance;
121 }*/
122
123 /**
124 * Return a human readable representation of the object.
125 * @return a human readable representation of the object.
126 */
127 public String toString(){
128 return "<" + id + " " + distance + ">";
129 }
130 }
131
132