Class TypeComparators

java.lang.Object
org.assertj.core.internal.TypeComparators

public class TypeComparators extends Object
An internal holder of the comparators for type. It is used to store comparators for registered classes. When looking for a Comparator for a given class the holder returns the most relevant comparator.
Author:
Filip Hrisafov
  • Field Details

  • Constructor Details

    • TypeComparators

      public TypeComparators()
  • Method Details

    • defaultTypeComparators

      public static TypeComparators defaultTypeComparators()
    • getComparatorForType

      public Comparator<?> getComparatorForType(Class<?> clazz)
      This method returns the most relevant comparator for the given class. The most relevant comparator is the comparator which is registered for the class that is closest in the inheritance chain of the given clazz. The order of checks is the following: 1. If there is a registered comparator for clazz then this one is used 2. We check if there is a registered comparator for a superclass of clazz 3. We check if there is a registered comparator for an interface of clazz
      Parameters:
      clazz - the class for which to find a comparator
      Returns:
      the most relevant comparator, or null if no comparator could be found
    • hasComparatorForType

      public boolean hasComparatorForType(Class<?> type)
      Checks, whether an any custom comparator is associated with the giving type.
      Parameters:
      type - the type for which to check a comparator
      Returns:
      is the giving type associated with any custom comparator
    • registerComparator

      public <T> void registerComparator(Class<T> clazz, Comparator<? super T> comparator)
      Puts the comparator for the given clazz.
      Type Parameters:
      T - the type of the objects for the comparator
      Parameters:
      clazz - the class for the comparator
      comparator - the comparator itself
    • comparatorByTypes

      public Stream<Map.Entry<Class<?>,Comparator<?>>> comparatorByTypes()
      Returns a sequence of all type-comparator pairs which the current holder supplies.
      Returns:
      sequence of field-comparator pairs
    • get

      public Comparator<?> get(Class<?> clazz)
      This method returns the most relevant entity for the given class. The most relevant entity is the entity which is registered for the class that is closest in the inheritance chain of the given clazz. The order of checks is the following: 1. If there is a registered entity for clazz then this one is used 2. We check if there is a registered entity for a superclass of clazz 3. We check if there is a registered entity for an interface of clazz
      Parameters:
      clazz - the class for which to find a entity
      Returns:
      the most relevant entity, or null if on entity could be found
    • put

      public void put(Class<?> clazz, Comparator<?> entity)
      Puts the entity for the given clazz.
      Parameters:
      clazz - the class for the comparator
      entity - the entity itself
    • hasEntity

      public boolean hasEntity(Class<?> type)
      Checks, whether an entity is associated with the giving type.
      Parameters:
      type - the type for which to check an entity
      Returns:
      is the giving type associated with any entity
    • isEmpty

      public boolean isEmpty()
      Returns:
      true is there are registered entities, false otherwise
    • clear

      public void clear()
      Removes all registered entities.
    • entityByTypes

      public Stream<Map.Entry<Class<?>,Comparator<?>>> entityByTypes()
      Returns a sequence of all type-entity pairs which the current holder supplies.
      Returns:
      sequence of field-entity pairs
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object