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 class
QueryResultSet.ScoreComparator<T extends Publication>
ThisComparator
compares twoObject
s using a score retrieved from a map ofObject
tofloat
.
-
Constructor Summary
Constructors Constructor Description QueryResultSet()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description boolean
add(Publication pub, float score)
Adds the givenPublication
and its score to thisQueryResultSet
.boolean
addAll(PageResult<Publication> pr)
Adds the givenPageResult
to thisQueryResultSet
.boolean
addAll(QueryResultSet resultSet)
Adds all thePublication
in the specified result Set to thisQueryResultSet
.boolean
addAll(java.util.Collection<? extends Publication> coll)
OverridesAbstractCollection.addAll(Collection)
to call whenaddAll(QueryResultSet)
if the specified collection is aQueryResultSet
.boolean
addAll(java.util.Collection<? extends Publication> coll, float score)
Adds all thePublication
in the specified collection to thisQueryResultSet
.void
clear()
Clear all Object from this result set (also remove associated score).java.util.SortedSet<Publication>
getAsSortedSet()
Gets all the results contained in thisQueryResultSet
as 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 thisQueryResultSet
as aSortedSet
(aTreeSet
) retrieving the appropriateComparator
throughComparatorManager
.java.util.SortedSet<Publication>
getAsSortedSet(java.lang.String comparatorName, boolean reverse)
Gets all the results contained in thisQueryResultSet
as aSortedSet
(aTreeSet
) retrieving the appropriateComparator
throughComparatorManager
.java.util.SortedSet<Publication>
getAsSortedSet(java.util.Comparator comparator)
Gets all the results contained in thisQueryResultSet
as aSortedSet
(aTreeSet
) using the givenComparator
or none if null.java.lang.Object
getAttribute(java.lang.String name)
Returns the value of the named attribute as anObject
, ornull
if 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.float
getMaxScore()
Retrieve the maximum score value available in thisQueryResultSet
.int
getResultSize()
float
getScore(Publication pub)
Retrieve score of the specifiedPublication
.float
getScore(java.lang.Object obj)
Deprecated.static java.util.Comparator
getScoreComparator(QueryResultSet resultSet, java.util.Comparator parentComparator)
Gets a new instance of aScoreComparator
for relevance sorting of the givenQueryResultSet
.PageResult.Status
getStatus()
int
getTotalSize()
CAUTION: This is an internal method.boolean
isHybrid()
boolean
isTotalAccurate()
java.util.Iterator<Publication>
iterator()
OverridesHashSet.iterator()
to wrap the nativeIterator
instance returned by the Java API with a custom one making sure the max score is updated on removal.boolean
remove(java.lang.Object o)
Remove the given Object from this result set (also remove associated score).java.lang.Object
removeAttribute(java.lang.String name)
Removes an attribute from this result.boolean
retainAll(QueryResultSet resultSet)
Retains only the elements in thisQueryResultSet
that are contained in the specifiedQueryResultSet
.boolean
retainAll(java.util.Collection<?> coll)
OverridesAbstractCollection.retainAll(Collection)
to call whenretainAll(QueryResultSet)
if the specified collection is aQueryResultSet
.java.lang.Object
setAttribute(java.lang.String name, java.lang.Object obj)
Stores an attribute in this result.void
setHybrid(boolean isHybrid)
void
setStatus(PageResult.Status status)
void
setTotalAccurate(boolean isTotalAccurate)
Indicates if the total size accuratevoid
setTotalSize(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
- aString
specifying the name of the attributeobj
- theObject
to be stored- Returns:
- previous value associated with specified name, or
null
if there was no mapping for name. Anull
return can also indicate thatnull
was associated with the specified name.
-
removeAttribute
public java.lang.Object removeAttribute(java.lang.String name)
Removes an attribute from this result.- Parameters:
name
- aString
specifying- Returns:
- previous value associated with specified name, or
null
if there was no mapping for name. Anull
return can also indicate thatnull
was associated with the specified name.
-
getAttribute
public java.lang.Object getAttribute(java.lang.String name)
Returns the value of the named attribute as anObject
, ornull
if no attribute of the given name exists.- Parameters:
name
- aString
specifying the name of the attribute- Returns:
- an
Object
containing the value of the attribute, ornull
if 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 givenPublication
and its score to thisQueryResultSet
.
If a score for the givenObject
was already in thisQueryResultSet
for a given element, its score is replaced.- Parameters:
pub
- thePublication
to add.score
- the new score of thePublication
- Returns:
true
if 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 thePublication
in the specified collection to thisQueryResultSet
.
If a score was already in theCollection
for a given element, its score is replaced.- Parameters:
coll
- collection whose elements are to be added to this collection.score
- the score of thePublication
in theCollection
(should be between 0.0f and 1.0f)- Returns:
true
if this collection changed as a result of the call.- Since:
- jcms-5.5.0
-
addAll
public boolean addAll(QueryResultSet resultSet)
Adds all thePublication
in the specified result Set to thisQueryResultSet
. Also puts all attribute of the specified QueryResultSet into this QueryResultSet.
If aPublication
was already in thisQueryResultSet
, it is replaced with its score in the givenQueryResultSet
.- Parameters:
resultSet
-QueryResultSet
whose elements are to be added to this QueryResultSet.- Returns:
true
if 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 thisQueryResultSet
that are contained in the specifiedQueryResultSet
. In other words, removes from thisQueryResultSet
all of its elements that are not contained in the specifiedQueryResultSet
.Also updates scores of elements kept in this
QueryResultSet
if their existing score are below the score of the specifiedQueryResultSet
.- Parameters:
resultSet
- elements to be retained in this collection.- Returns:
true
if thisQueryResultSet
changed as a result of the call.- Since:
- jcms-5.7.0
-
iterator
public java.util.Iterator<Publication> iterator()
OverridesHashSet.iterator()
to wrap the nativeIterator
instance 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:
iterator
in interfacejava.util.Collection<Publication>
- Specified by:
iterator
in interfacejava.lang.Iterable<Publication>
- Specified by:
iterator
in interfacejava.util.Set<Publication>
- Overrides:
iterator
in 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:
addAll
in interfacejava.util.Collection<Publication>
- Specified by:
addAll
in interfacejava.util.Set<Publication>
- Overrides:
addAll
in 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:
retainAll
in interfacejava.util.Collection<Publication>
- Specified by:
retainAll
in interfacejava.util.Set<Publication>
- Overrides:
retainAll
in 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 givenPageResult
to thisQueryResultSet
.- Parameters:
pr
- thePageResult
- Returns:
true
if thisQueryResultSet
changed 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:
remove
in interfacejava.util.Collection<Publication>
- Specified by:
remove
in interfacejava.util.Set<Publication>
- Overrides:
remove
in 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:
clear
in interfacejava.util.Collection<Publication>
- Specified by:
clear
in interfacejava.util.Set<Publication>
- Overrides:
clear
in 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
obj
or 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
- thePublication
from which to retrieve the score.- Returns:
- score associated with given
obj
or 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 thisQueryResultSet
as 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 thisQueryResultSet
as aSortedSet
(aTreeSet
) using the givenComparator
or 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
- theComparator
to 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 thisQueryResultSet
as aSortedSet
(aTreeSet
) retrieving the appropriateComparator
throughComparatorManager
.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 theComparator
to 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 thisQueryResultSet
as aSortedSet
(aTreeSet
) retrieving the appropriateComparator
throughComparatorManager
.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 theComparator
to 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 aScoreComparator
for relevance sorting of the givenQueryResultSet
.- Parameters:
resultSet
- theQueryResultSet
from which to retrieve score of eachPublication
.parentComparator
- theComparator
to use when score of comparedObject
are identical.- Returns:
- parentComparator is resultSet is null, new instance of
ScoreComparator
using 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
-
-