final class Presorter
extends java.lang.Object
Resolvable
provided by business application code
are used to sort the result set returned by the database. This class
supports the Progress semantics of "client-side" results sorting and break
groups. Multiple levels of nested break groups are supported.
Sorting criteria is specified using the addSortCriterion
methods. The order in which criteria are added is significant; the first
criterion added dictates the coarsest level of sorting. Each criterion
added subsequently only sorts rows within that criterion added immediately
previous to it.
The sort operation is performed once, and it is up to the owning query to cache these results for later record retrieval requests.
Modifier and Type | Class and Description |
---|---|
private class |
Presorter.IncrementalResults
This class should be used instead of
Presorter.SortedResults when the sort
criteria added to the Presorter match the leading components of the
presort query sort phrase (i.e. |
private static interface |
Presorter.LazyEvaluator
Interface for the classes which are used for lazy evaluation of the break rows.
|
private class |
Presorter.SortedResults
A specialized implementation of
Results which
presorts all query results according to the sort criteria
specified for this query. |
private static class |
Presorter.SortHelper
A helper object which provides services related to sorting and break
groups.
|
Modifier and Type | Field and Description |
---|---|
private boolean |
breakGroups
Are sort criteria used as break groups?
|
private java.util.Map<Resolvable,Presorter.SortHelper> |
originalSortHelpers
The original set of sort helpers.
|
private Presortable |
query
Reference to the owning query object
|
private java.util.Map<Resolvable,Presorter.SortHelper> |
sortHelpers
Map of break group keys to
SortHelper values |
Constructor and Description |
---|
Presorter(Presortable query)
Constructor which stores a reference to the owning query object.
|
Modifier and Type | Method and Description |
---|---|
(package private) void |
addDynamicSortCriterion(Resolvable sort,
boolean ascending)
Add a single sort criterion, in the form of a
Resolvable object which is
resolved at runtime. |
(package private) void |
addSortCriterion(Resolvable sort)
Add a single sort criterion, in the form of a
Resolvable
object which is resolved at sort time. |
(package private) void |
addSortCriterion(Resolvable sort,
boolean descending)
Add a single sort criterion, in the form of a
Resolvable object which is
resolved at runtime. |
private logical |
alignsWithBreakGroup(integer level,
java.util.function.Supplier<logical> wholeFn,
java.util.function.Function<Resolvable,logical> groupFn)
Determine whether the query result row currently being visited aligns with the first or
last row of the query's result set or within a break group whose category is identified by
the specified BREAK-BY level.
|
private void |
calculateBreakGroups(BaseDataType[] lastValues,
int lastIndex,
BaseDataType currentValue,
java.util.Set<java.lang.Integer> breakRows,
int currentRow)
Update break rows information for the given sort criterion.
|
(package private) void |
clearDynamicSortCriteria()
Drop all current sort criteria added dynamically.
|
(package private) Results |
createIncrementalResults(Results sorted)
Create a result set that will interrogate
sorted results
iteratively on request (not up front, as results set returned by
createSortedResults(com.goldencode.p2j.persist.Results) does). |
(package private) Results |
createSortedResults(Results unsorted)
Create a result set that is sorted according to the sort criteria
provided to this object.
|
(package private) void |
enableBreakGroups()
Enable all sort criteria set for this query to act as break group
categories.
|
private Presorter.SortHelper |
getSortHelper(Resolvable key)
Get the sort helper, if any, associated with the given break group key.
|
(package private) boolean |
isBreakGroupsEnabled()
Determines whether sort criteria set for this query acts as break group
categories.
|
(package private) logical |
isFirst()
Determine whether the query result row currently being visited is the
first row in the presorted results list.
|
(package private) logical |
isFirstOf(integer level)
Determine whether the query result row currently being visited is the first row of the
query's result set or the first row within a break group whose category is identified by
the specified BREAK-BY level.
|
(package private) logical |
isFirstOfGroup(Resolvable key)
Determine whether the query result row currently being visited is the
first row within a break group whose category is identified by the
specified resolvable object.
|
(package private) logical |
isLast()
Determine whether the query result row currently being visited is the
last row in the presorted results list.
|
(package private) logical |
isLastOf(integer level)
Determine whether the query result row currently being visited is the last row of the
query's result set or the last row within a break group whose category is identified by
the specified BREAK-BY level.
|
(package private) boolean |
isLastOfBreakGroup(Resolvable key)
Determine whether the query result row currently being visited is the
last row within a break group whose category is identified by the
specified resolvable object.
|
(package private) logical |
isLastOfGroup(Resolvable key)
Determine whether the query result row currently being visited is the
last row within a break group whose category is identified by the
specified resolvable object.
|
(package private) boolean |
isNewBreakGroup(Resolvable key)
Determine whether the query result row currently being visited is the
first row within a break group whose category is identified by the
specified resolvable object.
|
private void |
popTempContexts(RecordBuffer[] buffers)
Pop the current, temporary record context from the given buffers.
|
private void |
pushTempContexts(RecordBuffer[] buffers)
Push a temporary record context onto a set of buffers.
|
boolean |
sortCriteriaMatchSortPhrase(java.util.List<SortCriterion> criteria)
Determines whether the sort criteria added to this
Presorter match the leading
components of the given criteria. |
private final Presortable query
private final java.util.Map<Resolvable,Presorter.SortHelper> sortHelpers
SortHelper
valuesprivate java.util.Map<Resolvable,Presorter.SortHelper> originalSortHelpers
private boolean breakGroups
Presorter(Presortable query)
query
- Owning query object.public boolean sortCriteriaMatchSortPhrase(java.util.List<SortCriterion> criteria)
Presorter
match the leading
components of the given criteria.criteria
- Criteria to be compared with the sort criteria added to this
Presorter
.void addSortCriterion(Resolvable sort)
Resolvable
object which is resolved at sort time. It is the value of the resolved
result which is used for the sort.
The sort is ascending, from lowest resolved value to highest.
sort
- A resolvable object whose resolved result is used for sorting.void addSortCriterion(Resolvable sort, boolean descending)
Resolvable
object which is
resolved at runtime. It is the value of the resolved result which is used for the sort.sort
- A resolvable object whose resolved result is used for sorting. May not be
null
.descending
- if true
, the sort is descending, from highest
resolved value to lowest; otherwise, the sort is ascending.java.lang.NullPointerException
- if sort
is null
.void addDynamicSortCriterion(Resolvable sort, boolean ascending)
Resolvable
object which is
resolved at runtime. It is the value of the resolved result which is used for the sort.
Unlike addSortCriterion(com.goldencode.p2j.util.Resolvable)
methods, the criterion is added with higher priority.
sort
- A resolvable object whose resolved result is used for sorting. May not be
null
.ascending
- if true
, the sort is ascending, from lowest resolved value to highest;
otherwise, the sort is descending.java.lang.NullPointerException
- if sort
is null
.void clearDynamicSortCriteria()
void enableBreakGroups()
boolean isBreakGroupsEnabled()
logical isFirst()
true
if the current result row is the first row,
else false
.logical isFirstOfGroup(Resolvable key)
key
- Break group category key. Must be the same object reference
as was specified previously when invoking one of the
addSortCriterion
method variants.true
if break groups are enabled and current row
is first in the specified break group;
false
if break groups have not been enabled or
current row is not first in the specified break group.logical isFirstOf(integer level)
level
- Break group level, where 0 is the whole query, 1 is the first BREAK-BY sort
criterion, etc. Must not be negative or greater than the number of sort criteria.
vtrue
if the current row is first in the result set or within the specified
break group; else false
.logical isLastOf(integer level)
level
- Break group level, where 0 is the whole query, 1 is the first BREAK-BY sort
criterion, etc. Must not be negative or greater than the number of sort criteria.true
if the current row is last in the result set or within the specified
break group; else false
.logical isLast()
true
if the current result row is the last row,
else false
.logical isLastOfGroup(Resolvable key)
key
- Break group category key. Must be the same object reference
as was specified previously when invoking one of the
addSortCriterion
method variants.true
if break groups are enabled and current row
is last in the specified break group;
false
if break groups have not been enabled or
current row is not last in the specified break group.boolean isLastOfBreakGroup(Resolvable key)
key
- Break group category key. Must be the same object reference
as was specified previously when invoking one of the
addSortCriterion
method variants.true
if break groups are enabled and current row
is last in the specified break group;
false
if break groups have not been enabled or
current row is not last in the specified break group.boolean isNewBreakGroup(Resolvable key)
This is the backing worker method for isFirstOfGroup(com.goldencode.p2j.util.Resolvable)
and is
used internally by the query to manage accumulators.
key
- Break group category key. Must be the same object reference
as was specified previously when invoking one of the
addSortCriterion
method variants.true
if current row is first in a break group;
false
if it is not; unknown value if break groups
have not been enabled.Results createSortedResults(Results unsorted) throws PersistenceException
unsorted
parameter. They are sorted and stored into
another Results
object, which is returned.unsorted
- A set of unsorted, preselect results.unsorted
.PersistenceException
Results createIncrementalResults(Results sorted) throws PersistenceException
sorted
results
iteratively on request (not up front, as results set returned by
createSortedResults(com.goldencode.p2j.persist.Results)
does). Note that passed results should
already be in a proper order.sorted
- A set of sorted, preselect results.sorted
results.PersistenceException
private Presorter.SortHelper getSortHelper(Resolvable key)
key
- Break group category key.java.lang.IllegalArgumentException
- if the break group key is invalid.private logical alignsWithBreakGroup(integer level, java.util.function.Supplier<logical> wholeFn, java.util.function.Function<Resolvable,logical> groupFn)
level
- Break group level, where 0 is the whole query, 1 is the first BREAK-BY sort
criterion, etc. Must not be negative or greater than the number of sort criteria.wholeFn
- Lambda expression to be used to determine alignment with the whole query's result
set.groupFn
- Lambda expression to be used to determine alignment with a particular break group.true
if the current row aligns with the results as specified above;
else false
.private void calculateBreakGroups(BaseDataType[] lastValues, int lastIndex, BaseDataType currentValue, java.util.Set<java.lang.Integer> breakRows, int currentRow)
lastValues
- Array which contains the last sort criteria values for all criteria.lastIndex
- Index of the required criterion in the last
array.currentValue
- Current value of the given sort criterion.breakRows
- Set of break rows for the given criterion.currentRow
- Position of the current row into the whole result set.private void pushTempContexts(RecordBuffer[] buffers)
RecordBuffer.pushTempContext()
.buffers
- Buffers on which a temporary record context should be set.private void popTempContexts(RecordBuffer[] buffers) throws PersistenceException
As a convenience, the currently active temporary records for this
buffers is set to null
first.
buffers
- Buffers from which the current temporary record contexts
should be popped.PersistenceException
- if setting any current, temporary record to null
triggers a failure to evict the temporary record from the
active Hibernate session.pushTempContexts(RecordBuffer[])