Package com.jalios.jcms
Class QueryResultSet
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractSet<E>
-
- java.util.HashSet<Publication>
-
- com.jalios.jcms.QueryResultSet
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable,java.lang.Iterable<Publication>,java.util.Collection<Publication>,java.util.Set<Publication>
public class QueryResultSet extends java.util.HashSet<Publication>
This class holds results of a query.
It keeps tracks of all results (Publication) from a query and their scores.- Since:
- jcms-5.5.0
- Author:
- Olivier Jaquemet
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classQueryResultSet.ScoreComparator<T extends Publication>ThisComparatorcompares twoObjects using a score retrieved from a map ofObjecttofloat.
-
Constructor Summary
Constructors Constructor Description QueryResultSet()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description booleanadd(Publication pub, float score)Adds the givenPublicationand its score to thisQueryResultSet.booleanaddAll(PageResult<Publication> pr)Adds the givenPageResultto thisQueryResultSet.booleanaddAll(QueryResultSet resultSet)Adds all thePublicationin the specified result Set to thisQueryResultSet.booleanaddAll(java.util.Collection<? extends Publication> coll)OverridesAbstractCollection.addAll(Collection)to call whenaddAll(QueryResultSet)if the specified collection is aQueryResultSet.booleanaddAll(java.util.Collection<? extends Publication> coll, float score)Adds all thePublicationin the specified collection to thisQueryResultSet.voidclear()Clear all Object from this result set (also remove associated score).java.util.SortedSet<Publication>getAsSortedSet()Gets all the results contained in thisQueryResultSetas aSortedSet(aTreeSet) using theScoreComparator.java.util.SortedSet<Publication>getAsSortedSet(java.lang.Class<? extends Publication> typeClazz, java.lang.String comparatorName, boolean reverse)Gets all the results contained in thisQueryResultSetas aSortedSet(aTreeSet) retrieving the appropriateComparatorthroughComparatorManager.java.util.SortedSet<Publication>getAsSortedSet(java.lang.String comparatorName, boolean reverse)Gets all the results contained in thisQueryResultSetas aSortedSet(aTreeSet) retrieving the appropriateComparatorthroughComparatorManager.java.util.SortedSet<Publication>getAsSortedSet(java.util.Comparator comparator)Gets all the results contained in thisQueryResultSetas aSortedSet(aTreeSet) using the givenComparatoror none if null.java.lang.ObjectgetAttribute(java.lang.String name)Returns the value of the named attribute as anObject, ornullif no attribute of the given name exists.java.util.Map<java.lang.String,java.lang.Object>getAttributeMap()Returns a java.util.Map of the attributes of this results.floatgetMaxScore()Retrieve the maximum score value available in thisQueryResultSet.intgetResultSize()floatgetScore(Publication pub)Retrieve score of the specifiedPublication.floatgetScore(java.lang.Object obj)Deprecated.static java.util.ComparatorgetScoreComparator(QueryResultSet resultSet, java.util.Comparator parentComparator)Gets a new instance of aScoreComparatorfor relevance sorting of the givenQueryResultSet.PageResult.StatusgetStatus()intgetTotalSize()CAUTION: This is an internal method.booleanisHybrid()booleanisTotalAccurate()java.util.Iterator<Publication>iterator()OverridesHashSet.iterator()to wrap the nativeIteratorinstance returned by the Java API with a custom one making sure the max score is updated on removal.booleanremove(java.lang.Object o)Remove the given Object from this result set (also remove associated score).java.lang.ObjectremoveAttribute(java.lang.String name)Removes an attribute from this result.booleanretainAll(QueryResultSet resultSet)Retains only the elements in thisQueryResultSetthat are contained in the specifiedQueryResultSet.booleanretainAll(java.util.Collection<?> coll)OverridesAbstractCollection.retainAll(Collection)to call whenretainAll(QueryResultSet)if the specified collection is aQueryResultSet.java.lang.ObjectsetAttribute(java.lang.String name, java.lang.Object obj)Stores an attribute in this result.voidsetHybrid(boolean isHybrid)voidsetStatus(PageResult.Status status)voidsetTotalAccurate(boolean isTotalAccurate)Indicates if the total size accuratevoidsetTotalSize(int totalSize)Assigns the total size of thisQueryResultSet.
-
-
-
Method Detail
-
setAttribute
public java.lang.Object setAttribute(java.lang.String name, java.lang.Object obj)Stores an attribute in this result.
If the object passed in is null, the effect is the same as callingremoveAttribute(java.lang.String).- Parameters:
name- aStringspecifying the name of the attributeobj- theObjectto be stored- Returns:
- previous value associated with specified name, or
nullif there was no mapping for name. Anullreturn can also indicate thatnullwas associated with the specified name.
-
removeAttribute
public java.lang.Object removeAttribute(java.lang.String name)
Removes an attribute from this result.- Parameters:
name- aStringspecifying- Returns:
- previous value associated with specified name, or
nullif there was no mapping for name. Anullreturn can also indicate thatnullwas associated with the specified name.
-
getAttribute
public java.lang.Object getAttribute(java.lang.String name)
Returns the value of the named attribute as anObject, ornullif no attribute of the given name exists.- Parameters:
name- aStringspecifying the name of the attribute- Returns:
- an
Objectcontaining the value of the attribute, ornullif the attribute does not exist
-
getAttributeMap
public java.util.Map<java.lang.String,java.lang.Object> getAttributeMap()
Returns a java.util.Map of the attributes of this results.- Returns:
- the internal Map used by this results, modify with care.
-
add
public boolean add(Publication pub, float score)
Adds the givenPublicationand its score to thisQueryResultSet.
If a score for the givenObjectwas already in thisQueryResultSetfor a given element, its score is replaced.- Parameters:
pub- thePublicationto add.score- the new score of thePublication- Returns:
trueif the set did not already contain the specified element.- Since:
- jcms-5.5.0
-
addAll
public boolean addAll(java.util.Collection<? extends Publication> coll, float score)
Adds all thePublicationin the specified collection to thisQueryResultSet.
If a score was already in theCollectionfor a given element, its score is replaced.- Parameters:
coll- collection whose elements are to be added to this collection.score- the score of thePublicationin theCollection(should be between 0.0f and 1.0f)- Returns:
trueif this collection changed as a result of the call.- Since:
- jcms-5.5.0
-
addAll
public boolean addAll(QueryResultSet resultSet)
Adds all thePublicationin the specified result Set to thisQueryResultSet. Also puts all attribute of the specified QueryResultSet into this QueryResultSet.
If aPublicationwas already in thisQueryResultSet, it is replaced with its score in the givenQueryResultSet.- Parameters:
resultSet-QueryResultSetwhose elements are to be added to this QueryResultSet.- Returns:
trueif this collection changed as a result of the call.- Since:
- jcms-5.5.0
-
retainAll
public boolean retainAll(QueryResultSet resultSet)
Retains only the elements in thisQueryResultSetthat are contained in the specifiedQueryResultSet. In other words, removes from thisQueryResultSetall of its elements that are not contained in the specifiedQueryResultSet.Also updates scores of elements kept in this
QueryResultSetif their existing score are below the score of the specifiedQueryResultSet.- Parameters:
resultSet- elements to be retained in this collection.- Returns:
trueif thisQueryResultSetchanged as a result of the call.- Since:
- jcms-5.7.0
-
iterator
public java.util.Iterator<Publication> iterator()
OverridesHashSet.iterator()to wrap the nativeIteratorinstance returned by the Java API with a custom one making sure the max score is updated on removal.Needed for bug fix JCMS-3368.
- Specified by:
iteratorin interfacejava.util.Collection<Publication>- Specified by:
iteratorin interfacejava.lang.Iterable<Publication>- Specified by:
iteratorin interfacejava.util.Set<Publication>- Overrides:
iteratorin classjava.util.HashSet<Publication>- Since:
- jcms-5.7.0
- See Also:
HashSet.iterator()
-
addAll
public boolean addAll(java.util.Collection<? extends Publication> coll)
OverridesAbstractCollection.addAll(Collection)to call whenaddAll(QueryResultSet)if the specified collection is aQueryResultSet.- Specified by:
addAllin interfacejava.util.Collection<Publication>- Specified by:
addAllin interfacejava.util.Set<Publication>- Overrides:
addAllin classjava.util.AbstractCollection<Publication>- Since:
- jcms-5.7.0
- See Also:
addAll(QueryResultSet),AbstractCollection.addAll(Collection)
-
retainAll
public boolean retainAll(java.util.Collection<?> coll)
OverridesAbstractCollection.retainAll(Collection)to call whenretainAll(QueryResultSet)if the specified collection is aQueryResultSet.- Specified by:
retainAllin interfacejava.util.Collection<Publication>- Specified by:
retainAllin interfacejava.util.Set<Publication>- Overrides:
retainAllin classjava.util.AbstractCollection<Publication>- Since:
- jcms-5.7.0
- See Also:
retainAll(QueryResultSet),AbstractCollection.retainAll(Collection)
-
addAll
public boolean addAll(PageResult<Publication> pr)
Adds the givenPageResultto thisQueryResultSet.- Parameters:
pr- thePageResult- Returns:
trueif thisQueryResultSetchanged as a result of the call.- Since:
- jcms-7.0.1
-
remove
public boolean remove(java.lang.Object o)
Remove the given Object from this result set (also remove associated score).- Specified by:
removein interfacejava.util.Collection<Publication>- Specified by:
removein interfacejava.util.Set<Publication>- Overrides:
removein classjava.util.HashSet<Publication>- Since:
- jcms-5.5.0
- See Also:
HashSet.remove(java.lang.Object)
-
clear
public void clear()
Clear all Object from this result set (also remove associated score).- Specified by:
clearin interfacejava.util.Collection<Publication>- Specified by:
clearin interfacejava.util.Set<Publication>- Overrides:
clearin classjava.util.HashSet<Publication>- Since:
- jcms-5.5.0
- See Also:
HashSet.clear()
-
getScore
@Deprecated public float getScore(java.lang.Object obj)
Deprecated.Retrieve score of the givenObject(aPublication).- Parameters:
obj- theObject(aPublication) from which to retrieve the score.- Returns:
- score associated with given
objor 0.0f if no score is available for thisObject. - Since:
- jcms-5.5.0
-
getScore
public float getScore(Publication pub)
Retrieve score of the specifiedPublication.- Parameters:
pub- thePublicationfrom which to retrieve the score.- Returns:
- score associated with given
objor 0.0f if no score is available for thisPublication. - Since:
- jcms-6.0.0
-
getMaxScore
public float getMaxScore()
Retrieve the maximum score value available in thisQueryResultSet.- Returns:
- a float with the maximum score value.
-
getAsSortedSet
public java.util.SortedSet<Publication> getAsSortedSet()
Gets all the results contained in thisQueryResultSetas aSortedSet(aTreeSet) using theScoreComparator. Because this method sort all the result in a newTreeSet, this method is expensive, be careful when using it.- Returns:
- a new
SortedSet(aTreeSet) usingScoreComparator. - Since:
- jcms-5.5.0
- See Also:
QueryResultSet.ScoreComparator
-
getAsSortedSet
public java.util.SortedSet<Publication> getAsSortedSet(java.util.Comparator comparator)
Gets all the results contained in thisQueryResultSetas aSortedSet(aTreeSet) using the givenComparatoror none if null.Because this method sort all the result in a new
TreeSet, this method is expensive, be careful when using it.- Parameters:
comparator- theComparatorto use in theSortedSet, can be null to sort using natural order.- Returns:
- a new
SortedSet(aTreeSet) usingScoreComparator. - Since:
- jcms-5.5.0
- See Also:
QueryResultSet.ScoreComparator
-
getAsSortedSet
public java.util.SortedSet<Publication> getAsSortedSet(java.lang.String comparatorName, boolean reverse)
Gets all the results contained in thisQueryResultSetas aSortedSet(aTreeSet) retrieving the appropriateComparatorthroughComparatorManager.Because this method sort all the result in a new
TreeSet, this method is expensive, be careful when using it.- Parameters:
comparatorName- the name of theComparatorto use in theSortedSet, as available throughComparatorManager.reverse- a boolean indicating whether the comparator should have its behavior reversed (true) or unchanged (false)- Returns:
- a new
SortedSet - Since:
- jcms-7.1.0
-
getAsSortedSet
public java.util.SortedSet<Publication> getAsSortedSet(java.lang.Class<? extends Publication> typeClazz, java.lang.String comparatorName, boolean reverse)
Gets all the results contained in thisQueryResultSetas aSortedSet(aTreeSet) retrieving the appropriateComparatorthroughComparatorManager.Because this method sort all the result in a new
TreeSet, this method is expensive, be careful when using it.- Parameters:
typeClazz- the Class of Publication to sortcomparatorName- the name of theComparatorto use in theSortedSet, as available throughComparatorManager.reverse- a boolean indicating whether the comparator should have its behavior reversed (true) or unchanged (false)- Returns:
- a new
SortedSet - Since:
- jcms-7.1.0
-
getScoreComparator
public static java.util.Comparator getScoreComparator(QueryResultSet resultSet, java.util.Comparator parentComparator)
Gets a new instance of aScoreComparatorfor relevance sorting of the givenQueryResultSet.- Parameters:
resultSet- theQueryResultSetfrom which to retrieve score of eachPublication.parentComparator- theComparatorto use when score of comparedObjectare identical.- Returns:
- parentComparator is resultSet is null, new instance of
ScoreComparatorusing the given parent Comparator. - See Also:
QueryResultSet.ScoreComparator
-
getTotalSize
public int getTotalSize()
CAUTION: This is an internal method. You SHOULD NOT use this method but rathergetResultSize(). Returns the total size of thisQueryResultSet(it may be much larger than getResultSize().size() with DB query).- Returns:
- the total size of this
QueryResultSet. - Since:
- jcms-7.0.1
-
setTotalSize
public void setTotalSize(int totalSize)
Assigns the total size of thisQueryResultSet.- Parameters:
totalSize- the new total size.- Since:
- jcms-7.0.1
-
getResultSize
public int getResultSize()
-
isHybrid
public boolean isHybrid()
-
setHybrid
public void setHybrid(boolean isHybrid)
-
setStatus
public void setStatus(PageResult.Status status)
-
getStatus
public PageResult.Status getStatus()
-
isTotalAccurate
public boolean isTotalAccurate()
- Returns:
- true if the total size is accurate.
- Since:
- jcms-9.0.2
-
setTotalAccurate
public void setTotalAccurate(boolean isTotalAccurate)
Indicates if the total size accurate- Parameters:
isTotalAccurate- the value- Since:
- jcms-9.0.2
-
-