/** * This file is auto generated from JavaDoc. Do not modify it manually. */ import type { String } from "../../types/java/lang/String"; import type { Comparator } from "../../types/java/util/Comparator"; import type { Locale } from "../../types/java/util/Locale"; import type { CompoundComparatorBuilder } from "../CompoundComparatorBuilder"; import type { Resolver } from "../../types/senselogic/sitevision/api/base/Resolver"; /** * Node comparator utility interface. * *
* Node comparators created by this utility can be used for easy node comparison and when sorting a collection
* of nodes via Collections.sort(List<Node>, Comparator<Node>) or such.
*
* An instance of the Sitevision class implementing this interface can be obtained via
* {@link senselogic.sitevision.api.Utils#getNodeComparatorUtil()}.
* See {@link senselogic.sitevision.api.Utils} for how to obtain an instance of the Utils interface.
*
* This is a convenience for the {@link #getPropertyComparator(String, java.util.Locale)} method, using the * current locale as returned by {@link senselogic.sitevision.api.context.PortletContextUtil#getCurrentLocale()}. *
*
* Example
*
This Velocity code will sort the $listOfNodes List by comparing the 'displayName' property for each Node
*
* #set ($nodeComparatorUtil = $sitevisionUtils.NodeComparatorUtil)
* #set ($displayNameComparator = $nodeComparatorUtil.getPropertyComparator('displayName'))
*
* #set ($instanceCreatorUtil = $sitevisionUtils.InstanceCreatorUtil)
* #set ($collections = $instanceCreatorUtil.CollectionsInstance)
*
* $collections.sort($listOfNodes, $displayNameComparator)
*
* * Note! This comparator imposes orderings that are inconsistent with equals. *
* @param aPropertyName the name of the property * @return a comparator * @see #getPropertyComparator(String, java.util.Locale) */ getPropertyComparator(aPropertyName: String | string): Comparator; /** * Returns a comparator that compares nodes by the string value of a property in a case-insensitive manner using a specified locale. * *
* The property value will be extracted as a string using {@link senselogic.sitevision.api.property.PropertyUtil},
* and a null value will be treated as an empty string.
*
Why using proper Locale is important
*
* The comparators returned from this method will rely on a Collator and they have different comparison
* rules for different locales. The returned comparator might give unexpected results if a locale is used that doesn't
* match actual property values.
*
* This is best illustrated by an example of how typical swedish values will be compared with different locales.
* Assume we have a List of nodes that should be compared by a certain property and these are the actual string values
* for the property of the nodes in the list:
*
* "a", "A", "b", "B", "z", "Z", "å", "Å", "ä", "Ä", "ö", "Ö"
*
* When using the * {@link senselogic.sitevision.api.script.factory.CollectionsInstance#sort(java.util.List, java.util.Comparator) * Collections.sort(List<Node>, Comparator<Node>)} * method to sort the list, this is how they typically(*) will be sorted with different locales: *
*| Locale | Result (*) | *
|---|---|
| Swedish | "a", "A", "b", "B", "z", "Z", "å", "Å", "ä", "Ä", "ö", "Ö" |
*
| Norwegian | "a", "A", "b", "B", "z", "Z", "ä", "Ä", "ö", "Ö", "å", "Å" |
*
| English | "a", "A", "å", "Å", "ä", "Ä", "b", "B", "ö", "Ö", "z", "Z" |
*
| German | "a", "A", "å", "Å", "ä", "Ä", "b", "B", "ö", "Ö", "z", "Z" |
*
* (* this comparator is case-insensitive so ["a", "A"] can as well be ordered ["A", "a"]
* depending on initial order)
*
* Note! This comparator imposes orderings that are inconsistent with equals. *
* @param aPropertyName the name of the property. Note! If aPropertyName isnull or whitespace only, the returned comparator will be non-comparing,
i.e. a NO-OP comparator that always will return 0 ("equal") for all nodes.
* @param aLocale the locale that should be used when comparing two properties.
If aLocale is null, the current locale (as of
{@link senselogic.sitevision.api.context.PortletContextUtil#getCurrentLocale()} will be used).
* @return a comparator
*/
getPropertyComparator(
aPropertyName: String | string,
aLocale: Locale
): Comparator;
/**
* Returns a comparator that compares nodes by the value of a calendar property.
*
*
* The property value will be extracted as a calendar using {@link senselogic.sitevision.api.property.PropertyUtil},
* and comparing a null value will result in -1 (i.e. less than a non-null).
*
* Note! This comparator imposes orderings that are inconsistent with equals. *
* @param aCalendarPropertyName the name of the calendar property Note! If aCalendarPropertyName isnull or whitespace only, the returned comparator will be non-comparing,
i.e. a NO-OP comparator that always will return 0 ("equal") for all nodes.
* @return a comparator
* @since Sitevision 3.6.3
*/
getCalendarPropertyComparator(
aCalendarPropertyName: String | string
): Comparator;
/**
* Returns a comparator that compares nodes by the value of a calendar property.
*
*
* The property value will be extracted as an int using {@link senselogic.sitevision.api.property.PropertyUtil},
* and a no value value will be treated as 0.
*
* Note! This comparator imposes orderings that are inconsistent with equals. *
* @param aIntPropertyName the name of the int property Note! If aIntPropertyName isnull or whitespace only, the returned comparator will be non-comparing,
i.e. a NO-OP comparator that always will return 0 ("equal") for all nodes.
* @return a comparator
* @since Sitevision 3.6.3
*/
getIntPropertyComparator(aIntPropertyName: String | string): Comparator;
/**
* Returns a comparator that reverses the result of another.
*
* * The compareTo method of this comparator multiplies the result of another comparator's compareTo method with -1. * This will typically lead to a completely reversed order when sorted. *
* ** Note! This comparator imposes orderings that are inconsistent with equals. *
* @param aNodeComparator a comparator Note! If aNodeComparator isnull, the returned comparator will be non-comparing,
i.e. a NO-OP comparator that always will return 0 ("equal") for all nodes.
* @return a comparator
* @since Sitevision 3.6.3
*/
getReversedComparator(aNodeComparator: Comparator): Comparator;
/**
* Gets a builder for creating a compound comparator that allows comparison based on multiple node comparators.
* @return a CompoundComparatorBuilder
* @since Sitevision 3.6.3
*/
getCompoundComparatorBuilder(): CompoundComparatorBuilder;
/**
* Gets a comparator that compares nodes by the values extracted by a resolver.
*
*
* Example
* This Comparator can be helpful when you want to sort sv:collaborationGroup nodes in "most active" order.
* An example of how this can be implemented this in server-side Javascript is demonstrated below:
*
var myIdentity = require('PortletContextUtil').getCurrentUserIdentity(),
* userIdentityWrapper = require('UserFactory').getUserIdentityWrapper(myIdentity),
* myGroups,
* group,
* count,
* i;
*
* if (userIdentityWrapper) {
* myGroups = userIdentityWrapper.getCollaborationGroups();
* myGroups = toSortedList(myGroups);
* count = myGroups.size();
*
* // Render my groups
* for (i = 0; i < count; i++) {
* group = myGroups.get(i);
* ...
* }
* }
*
* // Helper function that uses a ResolverComparator to sort a collection of Collaboration groups
* function toSortedList(groups) {
* var sortedGroups = require('InstanceCreatorUtil').getList(),
* collections,
* groupDateResolver,
* resolverComparator;
*
* sortedGroups.addAll(groups); // Wrap in list to make it sortable
*
* if (sortedGroups.size() > 1) {
* // Create a resolver that can resolve appropriate Date from a collaboration group
* groupDateResolver = require('NodeResolverUtil').getCollaborationDateResolver();
*
* // Create a comparator that compares values extracted by the resolver
* resolverComparator = require('NodeComparatorUtil').getResolverComparator(groupDateResolver);
*
* // Get Collections and Sort!
* collections = require('CollectionsInstance');
* collections.sort(sortedGroups, resolverComparator);
* collections.reverse(sortedGroups); // ...and reverse result to get 'latest' first
* }
* return sortedGroups;
* }
*
*
*
* Note! Beware of resolved null values (null will always be "less" than a non-null)!
*