Class 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
    Version:
    $Revision: 133461 $
    Author:
    Olivier Jaquemet
    See Also:
    Serialized Form
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  QueryResultSet.ScoreComparator<T extends Publication>
      This Comparator compares two Objects using a score retrieved from a map of Object to float.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String REVISION  
    • 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 given Publication and its score to this QueryResultSet.
      boolean addAll​(PageResult<Publication> pr)
      Adds the given PageResult to this QueryResultSet.
      boolean addAll​(QueryResultSet resultSet)
      Adds all the Publication in the specified result Set to this QueryResultSet.
      boolean addAll​(java.util.Collection<? extends Publication> coll)
      Overrides AbstractCollection.addAll(Collection) to call when addAll(QueryResultSet) if the specified collection is a QueryResultSet.
      boolean addAll​(java.util.Collection<? extends Publication> coll, float score)
      Adds all the Publication in the specified collection to this QueryResultSet.
      void clear()
      Clear all Object from this result set (also remove associated score).
      java.util.SortedSet<Publication> getAsSortedSet()
      Gets all the results contained in this QueryResultSet as a SortedSet (a TreeSet) using the ScoreComparator.
      java.util.SortedSet<Publication> getAsSortedSet​(java.lang.Class<? extends Publication> typeClazz, java.lang.String comparatorName, boolean reverse)
      Gets all the results contained in this QueryResultSet as a SortedSet (a TreeSet) retrieving the appropriate Comparator through ComparatorManager.
      java.util.SortedSet<Publication> getAsSortedSet​(java.lang.String comparatorName, boolean reverse)
      Gets all the results contained in this QueryResultSet as a SortedSet (a TreeSet) retrieving the appropriate Comparator through ComparatorManager.
      java.util.SortedSet<Publication> getAsSortedSet​(java.util.Comparator comparator)
      Gets all the results contained in this QueryResultSet as a SortedSet (a TreeSet) using the given Comparator or none if null.
      java.lang.Object getAttribute​(java.lang.String name)
      Returns the value of the named attribute as an Object, or null 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 this QueryResultSet.
      int getResultSize()  
      float getScore​(Publication pub)
      Retrieve score of the specified Publication.
      float getScore​(java.lang.Object obj)
      Deprecated.
      static java.util.Comparator getScoreComparator​(QueryResultSet resultSet, java.util.Comparator parentComparator)
      Gets a new instance of a ScoreComparator for relevance sorting of the given QueryResultSet.
      PageResult.Status getStatus()  
      int getTotalSize()
      CAUTION: This is an internal method.
      boolean isHybrid()  
      boolean isTotalAccurate()  
      java.util.Iterator<Publication> iterator()
      Overrides HashSet.iterator() to wrap the native Iterator 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 this QueryResultSet that are contained in the specified QueryResultSet.
      boolean retainAll​(java.util.Collection<?> coll)
      Overrides AbstractCollection.retainAll(Collection) to call when retainAll(QueryResultSet) if the specified collection is a QueryResultSet.
      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 accurate
      void setTotalSize​(int totalSize)
      Assigns the total size of this QueryResultSet.
      • Methods inherited from class java.util.HashSet

        add, clone, contains, isEmpty, size, spliterator
      • Methods inherited from class java.util.AbstractSet

        equals, hashCode, removeAll
      • Methods inherited from class java.util.AbstractCollection

        containsAll, toArray, toArray, toString
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        parallelStream, removeIf, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
      • Methods inherited from interface java.util.Set

        containsAll, equals, hashCode, removeAll, toArray, toArray
    • Constructor Detail

      • QueryResultSet

        public QueryResultSet()
    • 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 calling removeAttribute(java.lang.String).
        Parameters:
        name - a String specifying the name of the attribute
        obj - the Object to be stored
        Returns:
        previous value associated with specified name, or null if there was no mapping for name. A null return can also indicate that null was associated with the specified name.
      • removeAttribute

        public java.lang.Object removeAttribute​(java.lang.String name)
        Removes an attribute from this result.
        Parameters:
        name - a String specifying
        Returns:
        previous value associated with specified name, or null if there was no mapping for name. A null return can also indicate that null was associated with the specified name.
      • getAttribute

        public java.lang.Object getAttribute​(java.lang.String name)
        Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.
        Parameters:
        name - a String specifying the name of the attribute
        Returns:
        an Object containing the value of the attribute, or null 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 given Publication and its score to this QueryResultSet.
        If a score for the given Object was already in this QueryResultSet for a given element, its score is replaced.
        Parameters:
        pub - the Publication to add.
        score - the new score of the Publication
        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 the Publication in the specified collection to this QueryResultSet.
        If a score was already in the Collection for a given element, its score is replaced.
        Parameters:
        coll - collection whose elements are to be added to this collection.
        score - the score of the Publication in the Collection (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 the Publication in the specified result Set to this QueryResultSet. Also puts all attribute of the specified QueryResultSet into this QueryResultSet.
        If a Publication was already in this QueryResultSet, it is replaced with its score in the given QueryResultSet.
        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 this QueryResultSet that are contained in the specified QueryResultSet. In other words, removes from this QueryResultSet all of its elements that are not contained in the specified QueryResultSet.

        Also updates scores of elements kept in this QueryResultSet if their existing score are below the score of the specified QueryResultSet.

        Parameters:
        resultSet - elements to be retained in this collection.
        Returns:
        true if this QueryResultSet changed as a result of the call.
        Since:
        jcms-5.7.0
      • iterator

        public java.util.Iterator<Publication> iterator()
        Overrides HashSet.iterator() to wrap the native Iterator 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 interface java.util.Collection<Publication>
        Specified by:
        iterator in interface java.lang.Iterable<Publication>
        Specified by:
        iterator in interface java.util.Set<Publication>
        Overrides:
        iterator in class java.util.HashSet<Publication>
        Since:
        jcms-5.7.0
        See Also:
        HashSet.iterator()
      • addAll

        public boolean addAll​(java.util.Collection<? extends Publication> coll)
        Overrides AbstractCollection.addAll(Collection) to call when addAll(QueryResultSet) if the specified collection is a QueryResultSet.
        Specified by:
        addAll in interface java.util.Collection<Publication>
        Specified by:
        addAll in interface java.util.Set<Publication>
        Overrides:
        addAll in class java.util.AbstractCollection<Publication>
        Since:
        jcms-5.7.0
        See Also:
        addAll(QueryResultSet), AbstractCollection.addAll(Collection)
      • retainAll

        public boolean retainAll​(java.util.Collection<?> coll)
        Overrides AbstractCollection.retainAll(Collection) to call when retainAll(QueryResultSet) if the specified collection is a QueryResultSet.
        Specified by:
        retainAll in interface java.util.Collection<Publication>
        Specified by:
        retainAll in interface java.util.Set<Publication>
        Overrides:
        retainAll in class java.util.AbstractCollection<Publication>
        Since:
        jcms-5.7.0
        See Also:
        retainAll(QueryResultSet), AbstractCollection.retainAll(Collection)
      • addAll

        public boolean addAll​(PageResult<Publication> pr)
        Adds the given PageResult to this QueryResultSet.
        Parameters:
        pr - the PageResult
        Returns:
        true if this QueryResultSet 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 interface java.util.Collection<Publication>
        Specified by:
        remove in interface java.util.Set<Publication>
        Overrides:
        remove in class java.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 interface java.util.Collection<Publication>
        Specified by:
        clear in interface java.util.Set<Publication>
        Overrides:
        clear in class java.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 given Object (a Publication).
        Parameters:
        obj - the Object (a Publication) from which to retrieve the score.
        Returns:
        score associated with given obj or 0.0f if no score is available for this Object.
        Since:
        jcms-5.5.0
      • getScore

        public float getScore​(Publication pub)
        Retrieve score of the specified Publication.
        Parameters:
        pub - the Publication from which to retrieve the score.
        Returns:
        score associated with given obj or 0.0f if no score is available for this Publication.
        Since:
        jcms-6.0.0
      • getMaxScore

        public float getMaxScore()
        Retrieve the maximum score value available in this QueryResultSet.
        Returns:
        a float with the maximum score value.
      • getAsSortedSet

        public java.util.SortedSet<Publication> getAsSortedSet()
        Gets all the results contained in this QueryResultSet as a SortedSet (a TreeSet) using the ScoreComparator. Because this method sort all the result in a new TreeSet, this method is expensive, be careful when using it.
        Returns:
        a new SortedSet (a TreeSet) using ScoreComparator.
        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 this QueryResultSet as a SortedSet (a TreeSet) using the given Comparator 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 - the Comparator to use in the SortedSet, can be null to sort using natural order.
        Returns:
        a new SortedSet (a TreeSet) using ScoreComparator.
        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 this QueryResultSet as a SortedSet (a TreeSet) retrieving the appropriate Comparator through ComparatorManager.

        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 the Comparator to use in the SortedSet, as available through ComparatorManager.
        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 this QueryResultSet as a SortedSet (a TreeSet) retrieving the appropriate Comparator through ComparatorManager.

        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 sort
        comparatorName - the name of the Comparator to use in the SortedSet, as available through ComparatorManager.
        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 a ScoreComparator for relevance sorting of the given QueryResultSet.
        Parameters:
        resultSet - the QueryResultSet from which to retrieve score of each Publication.
        parentComparator - the Comparator to use when score of compared Object 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 rather getResultSize(). Returns the total size of this QueryResultSet (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 this QueryResultSet.
        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)
      • 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