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 * 28 * @author Arnoldo Jose Muller Molina 29 * @since 0.7 30 */ 31 32 33 public final class OBResultDouble<O> extends AbstractOBResult<O> { 34 35 36 /** 37 * Distance of the object found and the query. 38 */ 39 protected double distance; 40 41 /** 42 * Default constructor. Used mostly to minimize the amount of object 43 * creations. 44 */ 45 public OBResultDouble(){ 46 } 47 48 /** 49 * Create a new OBResultDouble. 50 * @param object 51 * the result found. 52 * @param id 53 * The internal id of the result. 54 * @param distance 55 * Distance of the result and the original query. 56 */ 57 public OBResultDouble(O object, long id, double distance){ 58 59 super(object,id); 60 this.distance = distance; 61 62 } 63 64 /** 65 * @return The distance of the result and the original query. 66 */ 67 public final double getDistance(){ 68 return distance; 69 } 70 71 /** 72 * Sets the distance to a new value x. 73 * @param x 74 * The new value to set. 75 */ 76 public final void setDistance(double x){ 77 this.distance = x; 78 } 79 80 /** 81 * We implement the interface comparable so we provide this method. The 82 * only difference is that we return bigger objects first. (The 83 * comparable contract is multiplied by -1) 84 * @param o 85 * The object that will be compared. 86 * @return 1 if this object is smaller than o 0 if this object is equal 87 * than o -1 if this object is greater than o 88 */ 89 public int compareTo(Object o) { 90 assert o instanceof OBResultDouble; 91 OBResultDouble<O> comp = (OBResultDouble<O>) o; 92 int res = 0; 93 94 95 if (distance < comp.distance) { 96 res = 1; 97 } else if (distance > comp.distance) { 98 res = -1; 99 } 100 return res; 101 } 102 103 /** 104 * @return the hash code of this object. 105 */ 106 /*public int hashCode(){ 107 return (int) distance; 108 }*/ 109 110 111 112 /** 113 * We do not care about the object itself, just that both objects are at 114 * the same distance from the query. 115 * @return true if both distances are the same. 116 */ 117 /*public boolean equals(Object obj){ 118 OBResultDouble<O> comp = (OBResultDouble<O>) obj; 119 // a result object is the same if the distance is the same 120 // we do not care about the id. 121 return distance == comp.distance; 122 }*/ 123 124 /** 125 * Return a human readable representation of the object. 126 * @return a human readable representation of the object. 127 */ 128 public String toString(){ 129 return "<" + id + " " + distance + ">"; 130 } 131 } 132