001    package jcolibri.method.maintenance;
002    
003    import java.util.Collections;
004    import java.util.LinkedList;
005    import java.util.List;
006    
007    import jcolibri.cbrcore.CBRCase;
008    import jcolibri.cbrcore.CBRQuery;
009    
010    /**
011     * Stores the query result information. It contains a <query, result> pair.
012     * The result is some double value related to the query. 
013     *
014     * @author Lisa Cummins
015     */
016    public class QueryResult implements Comparable
017    {
018            protected CBRQuery _case;
019            protected double result;
020            
021            /**
022             * Sets up a <query, result> pair.
023             * @param _case The query case to be stored
024             * @param result The result associated with this case.
025             */
026            public QueryResult(CBRQuery _case, double result)
027            {       this._case = _case;
028                    this.result = result;
029            }
030    
031            /**
032             * Returns the case.
033             * @return the case.
034             */
035            public CBRQuery getCase() 
036            {       return _case;
037            }
038    
039            /**
040             * Set the given case to be the query case
041             * associated with this <query, result> pair.
042             * @param _case The case to set.
043             */
044            public void setCase(CBRCase _case) 
045            {       this._case = _case;
046            }
047    
048            /**
049             * Returns the result.
050             * @return the result.
051             */
052            public double getResult() 
053            {       return result;
054            }
055    
056            /**
057             * Set the given result to be the result
058             * associated with this <query, result> pair.
059             * @param result The result to set.
060             */
061            public void setEval(double result) {
062                    this.result = result;
063            }
064            
065            /**
066             * Returns a String representation of this object. 
067             * @return a String representation of this object. 
068             */
069            public String toString()
070            {       return _case + " -> "+ result;
071            }
072    
073            /**
074             * Returns the result of comparing this object to 
075             * the given object.
076             * This returns 0 if the object is not of the same class
077             * as this object, -1 if the result of this object
078             * is less than the result of the given object, 0 if
079             * the result of this object is equal to the result of the 
080             * given object and 1 if the result of this object is 
081             * greater than the result of the given object.
082             * @return the result of comparing this object to 
083             * the given object.
084             */
085            public int compareTo(Object o) 
086            {       if(!(o instanceof QueryResult))
087                            return 0;
088                    
089                    QueryResult other = (QueryResult)o;
090                    if(other.getResult() > result)
091                            return -1;
092                    else if(other.getResult() < result)
093                            return 1;
094                    else
095                            return 0;
096            }
097            
098            /**
099             * Sorts the given list of CaseResults in the given order and
100             * returns the sorted list. 
101             * @param ascending The order in which to sort the elements. 
102             * @param toSort The list of CaseResults to sort.
103             * @return the sorted list.
104             */
105            @SuppressWarnings("unchecked")
106            public static List sortResults(boolean ascending, List<QueryResult> toSort)
107            {   Collections.sort(toSort);
108                if(ascending)
109                {   return toSort;              
110                }
111                List<QueryResult> sorted = new LinkedList<QueryResult>();
112                for(QueryResult res: toSort)
113                {   sorted.add(0, res);                 
114                }
115                return sorted;
116            }
117    }