Class ComparatorBasedComparisonStrategy

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean areEqual​(java.lang.Object actual, java.lang.Object other)
      Returns true if actual and other are equal according to comparator, false otherwise.
      Handles the cases where one of the parameter is null so that internal comparator does not have too.
      java.lang.String asText()
      Used in error messages when a custom comparison strategy was used to compare values.
      java.lang.Iterable<?> duplicatesFrom​(java.lang.Iterable<?> iterable)
      Returns any duplicate elements from the given Iterable according to comparator.
      java.util.Comparator<?> getComparator()  
      java.lang.String getComparatorDescription()  
      boolean isGreaterThan​(java.lang.Object actual, java.lang.Object other)
      Returns true if actual is greater than other, false otherwise.
      boolean isStandard()
      Return true if comparison strategy is default/standard, false otherwise
      boolean iterableContains​(java.lang.Iterable<?> iterable, java.lang.Object value)
      Returns true if given Iterable contains given value according to comparator, false otherwise.
      If given Iterable is null or empty, return false.
      void iterableRemoves​(java.lang.Iterable<?> iterable, java.lang.Object value)
      Look for given value in given Iterable according to the Comparator, if value is found it is removed from it.
      Does nothing if given Iterable is null (meaning no exception thrown).
      void iterablesRemoveFirst​(java.lang.Iterable<?> iterable, java.lang.Object value)
      Removes the first value in iterable that matches the value according to the implemented comparison strategy.
      protected java.util.Set<java.lang.Object> newSetUsingComparisonStrategy()
      Returns a Set honoring the comparison strategy used.
      boolean stringContains​(java.lang.String string, java.lang.String sequence)
      Returns true if given string contains given sequence according to the implemented comparison strategy, false otherwise.
      boolean stringEndsWith​(java.lang.String string, java.lang.String suffix)
      Returns true if string ends with suffix according to the implemented comparison strategy, false otherwise.
      boolean stringStartsWith​(java.lang.String string, java.lang.String prefix)
      Returns true if string starts with prefix according to the implemented comparison strategy, false otherwise.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • comparator

        private final java.util.Comparator comparator
      • comparatorDescription

        private final java.lang.String comparatorDescription
    • Constructor Detail

      • ComparatorBasedComparisonStrategy

        public ComparatorBasedComparisonStrategy​(java.util.Comparator comparator)
        Creates a new ComparatorBasedComparisonStrategy specifying the comparison strategy with given comparator.
        Parameters:
        comparator - the comparison strategy to use.
      • ComparatorBasedComparisonStrategy

        public ComparatorBasedComparisonStrategy​(java.util.Comparator comparator,
                                                 java.lang.String comparatorDescription)
        Creates a new ComparatorBasedComparisonStrategy specifying the comparison strategy with given comparator and comparator description
        Parameters:
        comparator - the comparison strategy to use.
        comparatorDescription - the comparator description to use in assertion messages.
    • Method Detail

      • iterableContains

        public boolean iterableContains​(java.lang.Iterable<?> iterable,
                                        java.lang.Object value)
        Returns true if given Iterable contains given value according to comparator, false otherwise.
        If given Iterable is null or empty, return false.
        Parameters:
        iterable - the Iterable to search value in
        value - the object to look for in given Iterable
        Returns:
        true if given Iterable contains given value according to comparator, false otherwise.
      • iterableRemoves

        public void iterableRemoves​(java.lang.Iterable<?> iterable,
                                    java.lang.Object value)
        Look for given value in given Iterable according to the Comparator, if value is found it is removed from it.
        Does nothing if given Iterable is null (meaning no exception thrown).
        Parameters:
        iterable - the Iterable we want remove value from
        value - object to remove from given Iterable
      • iterablesRemoveFirst

        public void iterablesRemoveFirst​(java.lang.Iterable<?> iterable,
                                         java.lang.Object value)
        Description copied from interface: ComparisonStrategy
        Removes the first value in iterable that matches the value according to the implemented comparison strategy. If given Iterable is null, does nothing.
        Parameters:
        iterable - the Iterable we want remove value from
        value - object to remove from given Iterable
      • areEqual

        public boolean areEqual​(java.lang.Object actual,
                                java.lang.Object other)
        Returns true if actual and other are equal according to comparator, false otherwise.
        Handles the cases where one of the parameter is null so that internal comparator does not have too.
        Parameters:
        actual - the object to compare to other
        other - the object to compare to actual
        Returns:
        true if actual and other are equal according to comparator, false otherwise.
      • duplicatesFrom

        public java.lang.Iterable<?> duplicatesFrom​(java.lang.Iterable<?> iterable)
        Returns any duplicate elements from the given Iterable according to comparator.
        Specified by:
        duplicatesFrom in interface ComparisonStrategy
        Overrides:
        duplicatesFrom in class AbstractComparisonStrategy
        Parameters:
        iterable - the given Iterable we want to extract duplicate elements.
        Returns:
        an Iterable containing the duplicate elements of the given one. If no duplicates are found, an empty Iterable is returned.
      • asText

        public java.lang.String asText()
        Description copied from interface: ComparisonStrategy
        Used in error messages when a custom comparison strategy was used to compare values.

        For example ComparatorBasedComparisonStrategy returns:

        "when comparing values using " + toString()
        Returns:
        the comparison strategy description used in error messages.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • getComparator

        public java.util.Comparator<?> getComparator()
      • getComparatorDescription

        public java.lang.String getComparatorDescription()
      • stringStartsWith

        public boolean stringStartsWith​(java.lang.String string,
                                        java.lang.String prefix)
        Description copied from interface: ComparisonStrategy
        Returns true if string starts with prefix according to the implemented comparison strategy, false otherwise.
        Parameters:
        string - the String we want to look starting prefix
        prefix - the prefix String to look for at string's start
        Returns:
        true if string starts with prefix according to the implemented comparison strategy, false otherwise.
      • stringEndsWith

        public boolean stringEndsWith​(java.lang.String string,
                                      java.lang.String suffix)
        Description copied from interface: ComparisonStrategy
        Returns true if string ends with suffix according to the implemented comparison strategy, false otherwise.
        Parameters:
        string - the String we want to look starting suffix
        suffix - the suffix String to look for at string's end
        Returns:
        true if string ends with suffix according to the implemented comparison strategy, false otherwise.
      • stringContains

        public boolean stringContains​(java.lang.String string,
                                      java.lang.String sequence)
        Description copied from interface: ComparisonStrategy
        Returns true if given string contains given sequence according to the implemented comparison strategy, false otherwise.
        Parameters:
        string - the string to search sequence in (must not be null)
        sequence - the String to look for in given string
        Returns:
        true if given string contains given sequence according to the implemented comparison strategy, false otherwise.
      • isGreaterThan

        public boolean isGreaterThan​(java.lang.Object actual,
                                     java.lang.Object other)
        Description copied from interface: ComparisonStrategy
        Returns true if actual is greater than other, false otherwise.
        Parameters:
        actual - the object to compare to other
        other - the object to compare to actual
        Returns:
        true if actual is greater than other, false otherwise.