Top Description Inners Fields Constructors Methods
io.github.darvil.utils

public Class MultiComparator<T>

extends Object
Class Inheritance
Type Parameters
<T>
The type of the objects to compare.
Imports
java.util.ArrayList, .Comparator, java.util.function.Predicate

A class that allows you to compare two objects using a list of predicates.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static record
MultiComparator.Pred<
The type of the object the predicate takes.
T
>

A record that stores a predicate and its priority.

Field Summary

Modifier and TypeField and Description
private final ArrayList<MultiComparator.Pred<T>>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public MultiComparator<T>
addPredicate(Predicate<T>
The predicate to add.
p
,
int
The priority of the predicate. The higher the priority, the earlier the predicate will be checked.
priority
)

Adds a predicate to the list of predicates to be used when comparing.

public MultiComparator<T>
addPredicate(Predicate<T>
The predicate to add.
p
)

Adds a predicate to the list of predicates to be used when comparing.

public int

Returns:

-1 if the first object is "greater" than the second, 1 if the second object is "greater" than the first, 0 if they are equal.
compare
(T
The first object to compare.
first
,
T
The second object to compare.
second
)

Compares the two objects given using the predicates added to this comparator.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

predicatesback to summary
private final ArrayList<MultiComparator.Pred<T>> predicates

Constructor Detail

MultiComparatorback to summary
public MultiComparator()

Method Detail

addPredicateback to summary
public MultiComparator<T> addPredicate(Predicate<T> p, int priority)

Adds a predicate to the list of predicates to be used when comparing.

Parameters
p:Predicate<T>

The predicate to add.

priority:int

The priority of the predicate. The higher the priority, the earlier the predicate will be checked.

addPredicateback to summary
public MultiComparator<T> addPredicate(Predicate<T> p)

Adds a predicate to the list of predicates to be used when comparing. The priority of the predicate will be 0.

Parameters
p:Predicate<T>

The predicate to add.

compareback to summary
public int compare(T first, T second)

Compares the two objects given using the predicates added to this comparator.

Parameters
first:T

The first object to compare.

second:T

The second object to compare.

Returns:int

-1 if the first object is "greater" than the second, 1 if the second object is "greater" than the first, 0 if they are equal.

io.github.darvil.utils back to summary

private final Record MultiComparator.Pred<T>

extends Record
Class Inheritance
Record Components
priority:int
The priority of the predicate.
predicateCb:Predicate<T>
The predicate.
Type Parameters
<T>
The type of the object the predicate takes.

A record that stores a predicate and its priority.

Field Summary

Modifier and TypeField and Description
private final Predicate<T>
predicateCb

Record Component accessed by predicateCb().

The predicate.
private final int
priority

Record Component accessed by priority().

The priority of the predicate.

Constructor Summary

AccessConstructor and Description
pack-priv
Pred(int priority, Predicate<T> predicateCb)

Method Summary

Modifier and TypeMethod and Description
public final boolean
equals(Object o)

Implements abstract java.lang.Record.equals.

Indicates whether some other object is "equal to" this one.

public final int
hashCode()

Implements abstract java.lang.Record.hashCode.

Returns a hash code value for the record.

public Predicate<T>
predicateCb()

Record Component getter of predicateCb.

Record Component The predicate.

public int
priority()

Record Component getter of priority.

Record Component The priority of the predicate.

public final String
toString()

Implements abstract java.lang.Record.toString.

Returns a string representation of the record.

Field Detail

predicateCbback to summary
private final Predicate<T> predicateCb

Record Component accessed by predicateCb().

The predicate.

priorityback to summary
private final int priority

Record Component accessed by priority().

The priority of the predicate.

Constructor Detail

Predback to summary
pack-priv Pred(int priority, Predicate<T> predicateCb)

Method Detail

equalsback to summary
public final boolean equals(Object o)

Implements abstract java.lang.Record.equals.

Doc from java.lang.Record.equals.

Indicates whether some other object is "equal to" this one. In addition to the general contract of Object.equals, record classes must further obey the invariant that when a record instance is "copied" by passing the result of the record component accessor methods to the canonical constructor, as follows:

    R copy = new R(r.c1(), r.c2(), ..., r.cn());
then it must be the case that r.equals(copy).
hashCodeback to summary
public final int hashCode()

Implements abstract java.lang.Record.hashCode.

Doc from java.lang.Record.hashCode.

Returns a hash code value for the record. Obeys the general contract of Object.hashCode. For records, hashing behavior is constrained by the refined contract of Record.equals, so that any two records created from the same components must have the same hash code.

predicateCbback to summary
public Predicate<T> predicateCb()

Record Component getter of predicateCb.

Record Component
The predicate.
priorityback to summary
public int priority()

Record Component getter of priority.

Record Component
The priority of the predicate.
toStringback to summary
public final String toString()

Implements abstract java.lang.Record.toString.

Doc from java.lang.Record.toString.

Returns a string representation of the record. In accordance with the general contract of Object#toString(), the toString method returns a string that "textually represents" this record. The result should be a concise but informative representation that is easy for a person to read.

In addition to this general contract, record classes must further participate in the invariant that any two records which are equal must produce equal strings. This invariant is necessarily relaxed in the rare case where corresponding equal component values might fail to produce equal strings for themselves.