UNPKG

@drozdik.m/comparator-handler

Version:

Class that handles comparators and makes it easy for programmer to compare objects or primitive types inside more complicated classes.

56 lines (55 loc) 2.23 kB
import { IComparator } from "@drozdik.m/common-interfaces/IComparator"; import { IClonable } from "@drozdik.m/common-interfaces/IClonable"; import { IDisposable } from "@drozdik.m/common-interfaces/IDisposable"; /** * Class for easy handling of comparators. If explicit comparator is set, automatic * recognition of IComparable interface is turned off (on by default). It can be also turned * off manually. The class starts with default comparator for primitive types. * @author Martin Drozdík <info@bonsai-development.cz> (http://bonsai-development.cz) * */ export declare class ComparatorHandler<T> implements IClonable<ComparatorHandler<T>>, IDisposable { /** * Default comparator function. * Returns -1 if a < b * Returns 0 if a == b * Returns 1 if a > b * @param a Item #1 * @param b Item #2 */ private DefaultComparator; private comparator; protected automaticIComparableRecognition: boolean; /** * Creates new instance of ConparatorHandler. If explicit comparator is set, * automatic recognition of IEcomparable if turned off. * @param comparator Explicit comparator */ constructor(comparator?: IComparator<T>); Clone(): ComparatorHandler<T>; /** * Sets new comparator to use and turns off the automatic IComparable recognition. * @param newComparator? The new comparator (nullable) */ SetComparator(newComparator?: IComparator<T>): void; /** * Set the automatic recognition on/off * @param OnOff True for on, false for off */ SetAutomaticIComparableRecognition(OnOff: boolean): void; /** * If automatic IComparable recognition is on, tries to find new comparator. * @param item Item to dig in */ private AutomaticIComparableRecognition; /** * Comparator function. If no explicit comparator has been set and automatic IComparable recognition * is on, the comparator function is set to a.GetComparator(). * Returns -1 if a < b * Returns 0 if a == b * Returns 1 if a > b * @param a Item #1 * @param b Item #2 */ Compare(a: T, b: T): number; Dispose(): void; }