Class ComparisonUtils


  • public class ComparisonUtils
    extends Object
    Utility methods used for comparison and merge.
    • Method Detail

      • compareStreams

        public static ComparisonFlags compareStreams​(InputStream main,
                                                     InputStream other,
                                                     InputStream ancestor,
                                                     boolean isThreeWay)
                                              throws IOException
        Compares the given triple of streams. Checks whether any stream is null and compares streams byte by byte. Does not close streams at the end.
        Parameters:
        main - the main stream, may be null
        other - the other stream, may be null
        ancestor - the ancestor stream, may be null
        isThreeWay - true if the data source comparison is three-way and false if it is two-way
        Returns:
        the comparison result, never null
        Throws:
        IOException - if some other I/O error occurs while reading the streams
      • compareStreamsContent

        public static ComparisonFlags compareStreamsContent​(InputStream main,
                                                            InputStream other,
                                                            InputStream ancestor)
                                                     throws IOException
        Compares streams content byte by byte.
        Parameters:
        main - the main stream, can be null.
        other - the other stream, can be null.
        ancestor - the ancestor stream, can be null.
        Returns:
        the resulting comparison flags, never null
        Throws:
        IOException - if some other I/O error occurs while reading the streams
      • getScopeProviderForExtension

        public static org.eclipse.xtext.scoping.IScopeProvider getScopeProviderForExtension​(String extension)
      • getLongestCommonSubsequence

        public static <U> List<U> getLongestCommonSubsequence​(List<U> sequence1,
                                                              List<U> sequence2)
        Returns the longest common subsequence of the two specified sequences.
        Type Parameters:
        U - the type of elements in the sequences
        Parameters:
        sequence1 - the first sequence, not null
        sequence2 - the second sequence, not null
        Returns:
        the longest common subsequence, not null
      • getLongestCommonSubsequence

        public static <U> List<U> getLongestCommonSubsequence​(List<U> sequence1,
                                                              List<U> sequence2,
                                                              List<U> sequence3)
        Returns the longest common subsequence of the three specified sequences.
        Type Parameters:
        U - the type of elements in the sequences
        Parameters:
        sequence1 - the first sequence, cannot be null
        sequence2 - the second sequence, cannot be null
        sequence3 - the third sequence, cannot be null
        Returns:
        the longest common subsequence of the three sequences, never null
      • getLongestCommonSubsequenceMemoryOptimized

        public static <U> List<U> getLongestCommonSubsequenceMemoryOptimized​(ComparisonUtils.Sequence<U> sequence1,
                                                                             ComparisonUtils.Sequence<U> sequence2,
                                                                             ComparisonUtils.Sequence<U> sequence3,
                                                                             BiFunction<U,​U,​Boolean> equalityFunction)
        Returns the longest common subsequence of the three specified sequences. Uses the memory optimized Hirschberg algorithm.
        Type Parameters:
        U - the type of elements in the sequences
        Parameters:
        sequence1 - the first sequence, cannot be null
        sequence2 - the second sequence, cannot be null
        sequence3 - the third sequence, cannot be null
        equalityFunction - the function of equality of elements, may be null
        Returns:
        the longest common subsequence of the three sequences, never null
      • isSubsymlinkOf

        public static boolean isSubsymlinkOf​(String originalSymlink,
                                             String testedSymlink)
        Checks whether the tested symlink is subsymlink of the original symlink.
        Parameters:
        originalSymlink - the original symlink, can not be null
        testedSymlink - the symlink to test, can not be null
        Returns:
        true if the tested symlink is subsymlink of the original symlink and false otherwise
      • getNameFromSymlink

        public static String getNameFromSymlink​(String symlink)
        Obtains object's name from the given object's symlink.
        Parameters:
        symlink - the symlink to get name from, cannot be null
        Returns:
        the object's name, never null
      • threeWayCheckDoubleChanges

        public static void threeWayCheckDoubleChanges​(ComparisonFlags flags)
        Detects double changes according to the given comparison flags and updates them. Should be run only in threeway comparison mode. It is generally INCORRECT to call this method for combined comparison flags of parent nodes, because they contain flags from different children, while this method expects flags calculated just for one single node.
        Parameters:
        flags - the comparison flags to check, cannot be null
      • adoptImportedObjectSymlink

        public static String adoptImportedObjectSymlink​(IComparisonSession session,
                                                        String symlink)
        Converts, if possible, the imported object symlink into a new one supported by the main side.
        Parameters:
        session - the comparison session, cannot be null
        symlink - the imported object symlink to convert, cannot be null
        Returns:
        the converted symlink, never null
      • unequalAndNotNull

        public static boolean unequalAndNotNull​(Object first,
                                                Object second)
        Determines whether two objects are unequal and both non-null.
        Parameters:
        first - the first object to check, may be null
        second - the second object to check, may be null
        Returns:
        true if both non-null and they are not equal according to Object.equals(Object), otherwise return false
      • createComparisonFlagsForObjects

        public static ComparisonFlags createComparisonFlagsForObjects​(IComparedObjects<?> comparingObjects,
                                                                      boolean threeWay)
        Creates and returns a comparison flags for the triple of compared objects and fills them with information about object presence on each comparison side.
        Parameters:
        comparingObjects - the comparing objects to check comparison direction change for, cannot be null
        threeWay - tells whether the comparison is three-way or two-way
        Returns:
        the comparison flags with information about object presence on each comparison side, never null
      • createComparisonContext

        public static ComparisonContext createComparisonContext​(IComparisonSession session)
        Creates a comparison context with read-only transactional access to the data sources. The transaction to the comparison model is not opened. Note that the caller is responsible to close the context after its usage.
        Parameters:
        session - the comparison session to open the context for, cannot be null
        Returns:
        the comparison context instance, never null
      • createComparisonContext

        public static ComparisonContext createComparisonContext​(IComparisonSession session,
                                                                IBmTransaction mainDataSourceMergeTransaction)
        Creates a comparison context with transactional access to the data sources. If the merge transaction is given, the context can be used on a merge phase to modify MAIN data source. The transaction to the comparison model is not opened. Note that the caller is responsible to close the context after its usage.
        Parameters:
        session - the comparison session to open the context for, cannot be null
        mainDataSourceMergeTransaction - the active merge transaction to MAIN data source, can be null
        Returns:
        the comparison context instance, never null
      • createComparisonContext

        public static ComparisonContext createComparisonContext​(IComparisonSession session,
                                                                boolean comparisonModelWriteEnabled)
        Creates a comparison context with read-only transactional access to the data sources and to the comparison model. Note that the caller is responsible to close the context after its usage.
        Parameters:
        session - the comparison session to open the context for, cannot be null
        comparisonModelWriteEnabled - the flag indicating possibility to modify the comparison model
        Returns:
        the comparison context instance, never null
      • hasConflictOrderChangesInCollection

        public static boolean hasConflictOrderChangesInCollection​(Set<? extends IComparedObjects<?>> collectionElementsWithChangedOrderInAncestorMain,
                                                                  Set<? extends IComparedObjects<?>> collectionElementsWithChangedOrderInAncestorOther)
        Detects conflict order changes in a collection in three-way comparison mode.
        Parameters:
        collectionElementsWithChangedOrderInAncestorMain - the set of compared objects with order changes between ancestor and main sides, cannot be null
        collectionElementsWithChangedOrderInAncestorOther - the set of compared objects with order changes between ancestor and other sides, cannot be null
        Returns:
        true if the collection has conflict order changes, false otherwise
      • hasDeletionConflict

        public static boolean hasDeletionConflict​(IComparedObjects<?> comparingObjects,
                                                  ComparisonFlags flags)
        Checks case when element was deleted on one side and modified on the other.
        Parameters:
        comparingObjects - the comparing objects, cannot be null
        flags - the comparison flags calculated for the comparing objects, cannot be null
        Returns:
        true if the deletion conflict has been detected, false otherwise
      • isComparisonModelEditable

        public static boolean isComparisonModelEditable​(ComparisonProcessStatus status)
        Tell whether the comparison model is editable for the comparison handle with the given comparison process status.
        Parameters:
        status - the comparison process status, cannot be null
        Returns:
        true if the comparison model is editable, false otherwise