org.mycore.services.fieldquery
Class MCRResults

java.lang.Object
  extended by org.mycore.services.fieldquery.MCRResults
All Implemented Interfaces:
Iterable<MCRHit>

public class MCRResults
extends Object
implements Iterable<MCRHit>

This class represents the results of a query performed by MCRSearcher. Searchers add the hits using the addHit() method. Clients can get the hits, sort the entries and do merge/and/or operations on two different result sets. Searches may add the same hit (hit with the same ID) more than once. If the hit already is contained in the result set, the data of both objects is merged.

Version:
$Revision: 15594 $ $Date: 2009-07-23 13:15:39 +0200 (Thu, 23 Jul 2009) $
Author:
Arne Seifert, Frank Lützenkirchen, Jens Kupferschmidt
See Also:
MCRHit

Field Summary
protected  ArrayList<MCRHit> hits
          The list of MCRHit objects
protected  HashMap<String,MCRHit> map
          A map containing MCRHit IDs used for and/or operations on two different MCRResult objects
 
Constructor Summary
MCRResults()
          Creates a new, empty MCRResults.
 
Method Summary
 void addHit(MCRHit hit)
          Adds a hit.
 Element buildXML()
          Returns a XML element containing all hits and their data
 Element buildXML(int min, int max)
          Returns a XML element containing hits and their data
 void cutResults(int maxResults)
          Cuts the result list to the given maximum size, if more hits are present.
 MCRHit getHit(int i)
          Gets a single MCRHit.
protected  MCRHit getHit(String key)
          Returns the MCRHit with the given key, if it is in this results.
 String getID()
          Returns the unique ID of this result set
 int getNumHits()
          Returns the number of hits currently in this results
static MCRResults intersect(MCRResults... others)
          Does a logical and of this results hits and other results hits.
 boolean isReadonly()
          returns false if addHit(MCRHit) and merge(Document, String) are safe operations.
 boolean isSorted()
          Returns true if this result list is currently sorted
 Iterator<MCRHit> iterator()
           
protected  int merge(Document doc, String hostAlias)
          Merges the hits from a remote query to this results
 void setHostConnection(String host, String msg)
          Set the state of the connection of a host alias.
 void setSorted(boolean value)
          The searcher must set this to true, if the hits already have been added in sorted order.
 void sortBy(List<MCRSortBy> sortByList)
          Sorts this results by the given sort criteria.
 String toString()
           
static MCRResults union(MCRResults... others)
          Adds all hits of another result list that are not yet in this result list.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

hits

protected ArrayList<MCRHit> hits
The list of MCRHit objects


map

protected HashMap<String,MCRHit> map
A map containing MCRHit IDs used for and/or operations on two different MCRResult objects

Constructor Detail

MCRResults

public MCRResults()
Creates a new, empty MCRResults.

Method Detail

getID

public String getID()
Returns the unique ID of this result set

Returns:
the unique ID of this result set

addHit

public void addHit(MCRHit hit)
Adds a hit. If there is already a hit with the same ID, the sort data and meta data of both hits are merged and the merged hit replaces the existing hit.

Parameters:
hit - the MCRHit to add

getHit

public MCRHit getHit(int i)
Gets a single MCRHit. As long as isSorted() returns false, the order of the hits is natural order.

Parameters:
i - the position of the hit.
Returns:
the hit at this position, or null if position is out of bounds

getHit

protected MCRHit getHit(String key)
Returns the MCRHit with the given key, if it is in this results.

Parameters:
key - the key of the hit
Returns:
the MCRHit, if it exists

getNumHits

public int getNumHits()
Returns the number of hits currently in this results

Returns:
the number of hits

cutResults

public void cutResults(int maxResults)
Cuts the result list to the given maximum size, if more hits are present.

Parameters:
maxResults - the number of results to be left

setSorted

public void setSorted(boolean value)
The searcher must set this to true, if the hits already have been added in sorted order.

Parameters:
value - true, if sorted, false otherwise

isSorted

public boolean isSorted()
Returns true if this result list is currently sorted

Returns:
true if this result list is currently sorted

sortBy

public void sortBy(List<MCRSortBy> sortByList)
Sorts this results by the given sort criteria.

Parameters:
sortByList - a List of MCRSortBy objects

buildXML

public Element buildXML(int min,
                        int max)
Returns a XML element containing hits and their data

Parameters:
min - the position of the first hit to include in output
max - the position of the last hit to include in output
Returns:
a 'results' element with attributes 'sorted' and 'numHits' and hit child elements

buildXML

public Element buildXML()
Returns a XML element containing all hits and their data

Returns:
a 'results' element with attributes 'sorted' and 'numHits' and hit child elements

merge

protected int merge(Document doc,
                    String hostAlias)
Merges the hits from a remote query to this results

Parameters:
doc - the results from the remote query as XML document
hostAlias - the alias of the host where the hits come from
Returns:
the number of hits added

toString

public String toString()
Overrides:
toString in class Object

intersect

public static MCRResults intersect(MCRResults... others)
Does a logical and of this results hits and other results hits. The hits that are contained in both results are kept, the others are removed from this results list. The data of common hits is combined from both result lists.

Parameters:
other - the other result lists

union

public static MCRResults union(MCRResults... others)
Adds all hits of another result list that are not yet in this result list. Combines the MCRHit data of both result lists.

Parameters:
other - the other result lists

iterator

public Iterator<MCRHit> iterator()
Specified by:
iterator in interface Iterable<MCRHit>

setHostConnection

public void setHostConnection(String host,
                              String msg)
Set the state of the connection of a host alias.

Parameters:
host - the host alias
msg - the exception message of the connection or an empty string

isReadonly

public boolean isReadonly()
returns false if addHit(MCRHit) and merge(Document, String) are safe operations.

Returns: