Class Difference


  • public class Difference
    extends java.lang.Object
    A Difference should give me a sequence of elements I should find (to indicate the context) followed by a list of elements to remove or to add and follow by another sequence of elements. I should later be able to apply such difference to a nodeText.
    • Field Detail

      • STANDARD_INDENTATION_SIZE

        public static final int STANDARD_INDENTATION_SIZE
        See Also:
        Constant Field Values
      • nodeText

        private final NodeText nodeText
      • node

        private final Node node
      • originalElements

        private final java.util.List<TextElement> originalElements
      • originalIndex

        private int originalIndex
      • diffIndex

        private int diffIndex
      • indentation

        private final java.util.List<TextElement> indentation
      • addedIndentation

        private boolean addedIndentation
    • Method Detail

      • lastIndexOfEol

        int lastIndexOfEol​(java.util.List<TextElement> source)
      • posOfNextComment

        private int posOfNextComment​(int fromIndex,
                                     java.util.List<TextElement> elements)
      • isFollowedByComment

        private boolean isFollowedByComment​(int fromIndex,
                                            java.util.List<TextElement> elements)
      • removeElements

        private void removeElements​(int fromIndex,
                                    int toIndex,
                                    java.util.List<TextElement> elements)
      • isValidIndex

        private boolean isValidIndex​(int index,
                                     java.util.List<?> elements)
      • lastIndexOfEolWithoutGPT

        int lastIndexOfEolWithoutGPT​(java.util.List<TextElement> source)
      • indentationBlock

        private java.util.List<TextElement> indentationBlock()
      • isAfterLBrace

        private boolean isAfterLBrace​(NodeText nodeText,
                                      int nodeTextIndex)
      • considerEnforcingIndentation

        int considerEnforcingIndentation​(NodeText nodeText,
                                         int nodeTextIndex)
        If we are at the beginning of a line, with just spaces or tabs before/after the position of the deleted element we should force the space to be the same as the current indentation. This method handles the following case if we remove the modifier public ([ ] is an indent character) [ ][ ]public[ ][ ][ ]void[ ]m{} <-1--> <---2---> 1/ current indentation 2/ these whitespaces must be removed should produce [ ][ ]void[ ]m{}
      • considerRemovingIndentation

        private int considerRemovingIndentation​(NodeText nodeText,
                                                int nodeTextIndex)
      • considerIndentation

        private int considerIndentation​(NodeText nodeText,
                                        int nodeTextIndex,
                                        int numberOfCharactersToPreserve)
      • isEnforcingIndentationActivable

        private boolean isEnforcingIndentationActivable​(RemovedGroup removedGroup)
      • isRemovingIndentationActivable

        private boolean isRemovingIndentationActivable​(RemovedGroup removedGroup)
      • isLastElement

        private boolean isLastElement​(java.util.List<?> list,
                                      int index)
      • removeExtraCharacters

        private int removeExtraCharacters​(NodeText nodeText,
                                          int nodeTextIndex,
                                          int extraCharacters)
        Remove excess white space after deleting element.
        Parameters:
        nodeText - Contains a list of elements to analyze
        nodeTextIndex - Starting position in the input list
        Returns:
        The current position in the list of the elements
      • defineEnforcingIndentationContext

        private Difference.EnforcingIndentationContext defineEnforcingIndentationContext​(NodeText nodeText,
                                                                                         int startIndex)
        Starting at nodeTextIndex this method tries to determine how many contiguous spaces there are between the previous end of line and the next non whitespace (or tab) character
        Parameters:
        nodeText - List of elements to analyze
        nodeTextIndex - Starting position in the input list
        Returns:
        EnforcingIndentationContext Data structure that hold the starting position of the first whitespace char and The number of consecutive whitespace (or tab) characters
      • isSpaceOrTabElement

        private boolean isSpaceOrTabElement​(NodeText nodeText,
                                            int i)
      • apply

        void apply()
        Node that we have calculate the Difference we can apply to a concrete NodeText, modifying it according to the difference (adding and removing the elements provided).
      • applyLeftOverOriginalElements

        private boolean applyLeftOverOriginalElements()
      • applyLeftOverDiffElements

        private boolean applyLeftOverDiffElements()
      • combineRemovedElementsToRemovedGroups

        private java.util.Map<Removed,​RemovedGroup> combineRemovedElementsToRemovedGroups()
        Maps all Removed elements as keys to their corresponding RemovedGroup. A RemovedGroup contains all consecutive Removed elements.
        Example:
         Elements: Kept|Removed1|Removed2|Kept|Removed3|Added|Removed4
         Groups:        <----Group1---->       Group2         Group3
         Keys:          Removed1+Removed2      Removed3       Removed4
         
        Returns:
        Map with all Removed elements as keys to their corresponding RemovedGroup
      • groupConsecutiveRemovedElements

        private java.util.Map<java.lang.Integer,​java.util.List<Removed>> groupConsecutiveRemovedElements()
      • applyRemovedDiffElement

        private void applyRemovedDiffElement​(RemovedGroup removedGroup,
                                             Removed removed,
                                             TextElement originalElement,
                                             boolean originalElementIsChild,
                                             boolean originalElementIsToken)
      • cleanTheLineOfLeftOverSpace

        private void cleanTheLineOfLeftOverSpace​(RemovedGroup removedGroup,
                                                 Removed removed)
        Cleans the line of left over space if there is unnecessary indentation and the element will not be replaced
      • applyKeptDiffElement

        private void applyKeptDiffElement​(Kept kept,
                                          TextElement originalElement,
                                          boolean originalElementIsChild,
                                          boolean originalElementIsToken)
      • isTypeWithFullyQualifiedName

        private boolean isTypeWithFullyQualifiedName​(DifferenceElement element)
      • isNodeWithTypeArguments

        private boolean isNodeWithTypeArguments​(DifferenceElement element)
      • getIndexToNextTokenElement

        private int getIndexToNextTokenElement​(TokenTextElement element,
                                               int nestedDiamondOperator)
      • getIndexToNextTokenElementInArrayType

        private int getIndexToNextTokenElementInArrayType​(TokenTextElement element,
                                                          int arrayLevel)
      • isDiamondOperator

        private boolean isDiamondOperator​(JavaToken.Kind kind)
      • nextIsRightBrace

        private boolean nextIsRightBrace​(int index)
      • applyAddedDiffElement

        private void applyAddedDiffElement​(Added added)
      • isFollowedByUnindent

        private boolean isFollowedByUnindent​(java.util.List<DifferenceElement> diffElements,
                                             int diffIndex)
      • adjustIndentation

        private int adjustIndentation​(java.util.List<TextElement> indentation,
                                      NodeText nodeText,
                                      int nodeTextIndex,
                                      boolean followedByUnindent)
      • isAReplacement

        private boolean isAReplacement​(int diffIndex)
      • isReplaced

        private boolean isReplaced​(int diffIndex)
      • toString

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