Class ListMatcher


  • public final class ListMatcher
    extends java.lang.Object
    List matching utilities. Assuming list items might be reordered in yaml files. Also, updating file could contain more (or less) properties.

    It is important to update list items because in complex configs, list items contain entire subtrees which might also change (new properties added, comments changed, etc.).

    Searches for items with the maximum number of similar values. Use values from snakeyaml parser to increase accuracy. If items contains subtrees - apply same matching logic for entire subtree.

    Note that matching should work in both directions: find old node in new file's list or an opposite (no matter how items were changed).

    Since:
    06.06.2021
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T extends YamlLine<T>>
      T
      match​(T node, java.util.List<T> list)
      Searches for matched list item in the items list (assuming item from one file and list from another, no matter what direction).
      static java.lang.String unifyListItemPath​(java.lang.String path)
      Replaces list item positions with asterisk, so list items from different files (different list positions) could be compared.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • unifyListItemPath

        public static java.lang.String unifyListItemPath​(java.lang.String path)
        Replaces list item positions with asterisk, so list items from different files (different list positions) could be compared.
        Parameters:
        path - yaml path
        Returns:
        yaml path with unified list positions
      • match

        public static <T extends YamlLine<T>> T match​(T node,
                                                      java.util.List<T> list)
        Searches for matched list item in the items list (assuming item from one file and list from another, no matter what direction).

        Matches complete subtrees. Searches for items with the maximum amount of the same properties containing same values. If value would differ in any property - item would not be matched,

        Scalar list values are ignored (not comparing such properties). For lists with objects, at least one item must match in target list (with the same semantics as above).

        NOTE: if item matches with multiple nodes in the list, null will be returned because match must be exact! Otherwise, there is a high chance to incorrectly merge file (better not merge part at all).

        IMPORTANT: to avoid matching same items for different targets, remove matched item from candidates list.

        Type Parameters:
        T - structure type (works for both comments and snakeyaml structures)
        Parameters:
        node - list item node to find match for
        list - collection of list items to find matching in
        Returns:
        matched item or null