55
66public abstract class TextChange <S , Self extends TextChange <S , Self >> {
77
8- public static enum ChangeType {
8+ /**
9+ * Indicates whether this change can be merged with another change if they are both {@link #INSERTION} or
10+ * both {@link #DELETION}.
11+ */
12+ public static enum MergeType {
913 /** Indicates that the change will insert something but not remove anything */
1014 INSERTION ,
1115 /** Indicates that the change will delete something but not insert anything */
1216 DELETION ,
13- /** Indicates that the change will remove something and insert something as its replacement.
14- * For {@link PlainTextChange}s, the replacement will only be content.
15- * For {@link RichTextChange}s, the replacement may only be the style, only the content, or a mix of both.
16- * Style changes may change the style of the paragraph's content or a non-empty {@link Paragraph}
17- * ({@code p.length() > 0}) */
18- REPLACEMENT ,
19- /** Indicates that the paragraph style of an empty {@link Paragraph} ({@code p.length() == 0}) was changed */
20- RESTYLED_PARAGRAPH
17+ /** Indicates that the change is a style change, a replacement or something other than the other two types */
18+ NONE ,
2119 }
2220
2321
2422 protected final int position ;
2523 protected final S removed ;
2624 protected final S inserted ;
25+ protected final MergeType mergeType ;
2726
2827 public TextChange (int position , S removed , S inserted ) {
2928 this .position = position ;
3029 this .removed = removed ;
3130 this .inserted = inserted ;
31+
32+ if (insertedLength () == 0 ) {
33+ mergeType = removedLength () != 0
34+ ? MergeType .DELETION
35+ : MergeType .NONE ;
36+ } else if (removedLength () == 0 ) {
37+ mergeType = MergeType .INSERTION ;
38+ } else {
39+ mergeType = MergeType .NONE ;
40+ }
3241 }
3342
3443 public int getPosition () { return position ; };
@@ -37,25 +46,25 @@ public TextChange(int position, S removed, S inserted) {
3746 public Self invert () { return create (position , inserted , removed ); }
3847 public int getRemovalEnd () { return position + removedLength (); }
3948 public int getInsertionEnd () { return position + insertedLength (); }
49+ public final MergeType getMergeType () { return mergeType ; };
4050
41- public abstract ChangeType getType ();
4251 protected abstract int removedLength ();
4352 protected abstract int insertedLength ();
4453 protected abstract S concat (S a , S b );
4554 protected abstract S sub (S s , int from , int to );
4655 protected abstract Self create (int position , S removed , S inserted );
4756
4857 /**
49- * Merges this change with the given change only if the end of this change's inserted text
50- * equals the latter's position and both are either insertion or deletion changes.
58+ * Merges this change with the given change only if the end of this change's inserted text equals the
59+ * latter's position and both are either {@link MergeType#INSERTION} or {@link MergeType#DELETION} changes.
5160 *
5261 * @param latter change to merge with this change.
5362 * @return a new merged change if changes can be merged,
5463 * {@code null} otherwise.
5564 */
5665 public Optional <Self > mergeWith (Self latter ) {
57- if ((this .getType () == ChangeType .INSERTION || this .getType () == ChangeType .DELETION )
58- && this .getType () == latter .getType ()
66+ if ((this .mergeType == MergeType .INSERTION || this .mergeType == MergeType .DELETION )
67+ && this .mergeType == latter .mergeType
5968 && this .getInsertionEnd () == latter .position ) {
6069 S removedText = concat (this .removed , latter .removed );
6170 S addedText = concat (this .inserted , latter .inserted );
@@ -87,7 +96,7 @@ public final String toString() {
8796 return
8897 this .getClass ().getSimpleName () + "{\n " +
8998 "\t position: " + position + "\n " +
90- "\t type : " + getType () + "\n " +
99+ "\t mergeType : " + mergeType + "\n " +
91100 "\t removed: " + removed + "\n " +
92101 "\t inserted: " + inserted + "\n " +
93102 "}" ;
0 commit comments