Overview

Namespaces

  • PhpCommon
    • Comparison
      • Equivalence
      • Hasher

Interfaces

  • Comparable
  • Comparator
  • Equatable
  • Equivalence
  • Hashable
  • Hasher

Exceptions

  • IncomparableException
  • UnexpectedTypeException
  • Overview
  • Namespace
  • Class

Interface Comparable

Defines a method that a class implements to provide natural order of sorting to instances.

This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the natural ordering of the class, and the method PhpCommon\Comparison\Comparable::compareTo() is referred to as its natural comparison method.

The natural ordering for a class C is said to be consistent with PhpCommon\Comparison\Equatable::equals() if and only if $a->compareTo($b) === 0 has the same boolean value as $a->equals($b) for every $a and $b of class C.

It is strongly recommended (though not required) that natural orderings be consistent with PhpCommon\Comparison\Equatable::equals(). This is so because in certain cases these methods can be used together in a complementary way to achieve specific results. For example, some structures, such as sets and maps, may use the methods PhpCommon\Comparison\Equatable::equals() and PhpCommon\Comparison\Comparable::compareTo() as part of the strategy to determine where to store or retrieve data. In that way, such inconsistency may lead to unwanted results.

It is inspired by the Comparable interface, from Java API.

Namespace: PhpCommon\Comparison
Author: Marcos Passos marcos@croct.com
Link: Java Comparable interface
Located at Comparable.php
Methods summary
public boolean
# compareTo( PhpCommon\Comparison\Comparable $other )

Compares the current object with another for order.

Compares the current object with another for order.

This method has the following properties:

  • It is reflexive: for any instance of PhpCommon\Comparison\Equatable $x, $x->compareTo($x) === 0.
  • It is antisymmetric: for any instances of PhpCommon\Comparison\Equatable $x and $y, if $y->compareTo($x) <= 0 and $x->compareTo($y) <= 0, then $x->compareTo($y) === 0.
  • It is transitive: for any instances of PhpCommon\Comparison\Equatable $x, $y, and $z, if $x->compareTo($y) <= 0 and $y->compareTo($z) <= 0, then $x->compareTo($z) <= 0.

It is strongly recommended, but not strictly required, that ($x->compareTo($y) === 0) === ($x->equalTo($y)). Generally speaking, any class that implements the PhpCommon\Comparison\Comparable interface and violates this condition should clearly indicate this fact. The recommended language is "Note: this class has a natural ordering that is inconsistent with its natural equality relation.".

Parameters

$other
The object to compare to.

Returns

boolean

Returns a negative integer, zero, or a positive integer as this object is less than, equal to or greater than the specified object.

Throws

PhpCommon\Comparison\IncomparableException

If the specified object is not comparable to current object.

PHPCommon Comparison API API documentation generated by ApiGen