EditScript.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.text.diff;

  18. import java.util.ArrayList;
  19. import java.util.List;

  20. /**
  21.  * This class gathers all the {@link EditCommand commands} needed to transform
  22.  * one objects sequence into another objects sequence.
  23.  * <p>
  24.  * An edit script is the most general view of the differences between two
  25.  * sequences. It is built as the result of the comparison between two sequences
  26.  * by the {@link StringsComparator StringsComparator} class. The user can
  27.  * walk through it using the <em>visitor</em> design pattern.
  28.  * </p>
  29.  * <p>
  30.  * It is guaranteed that the objects embedded in the {@link InsertCommand insert
  31.  * commands} come from the second sequence and that the objects embedded in
  32.  * either the {@link DeleteCommand delete commands} or {@link KeepCommand keep
  33.  * commands} come from the first sequence. This can be important if subclassing
  34.  * is used for some elements in the first sequence and the {@code equals}
  35.  * method is specialized.
  36.  * </p>
  37.  *
  38.  * @see StringsComparator
  39.  * @see EditCommand
  40.  * @see CommandVisitor
  41.  * @see ReplacementsHandler
  42.  * @param <T> object type
  43.  * @since 1.0
  44.  */
  45. public class EditScript<T> {

  46.     /** Container for the commands. */
  47.     private final List<EditCommand<T>> commands;

  48.     /** Length of the longest common subsequence. */
  49.     private int lcsLength;

  50.     /** Number of modifications. */
  51.     private int modifications;

  52.     /**
  53.      * Constructs a new empty script.
  54.      */
  55.     public EditScript() {
  56.         commands = new ArrayList<>();
  57.         lcsLength = 0;
  58.         modifications = 0;
  59.     }

  60.     /**
  61.      * Appends a delete command to the script.
  62.      *
  63.      * @param command  command to add
  64.      */
  65.     public void append(final DeleteCommand<T> command) {
  66.         commands.add(command);
  67.         ++modifications;
  68.     }

  69.     /**
  70.      * Appends an insert command to the script.
  71.      *
  72.      * @param command  command to add
  73.      */
  74.     public void append(final InsertCommand<T> command) {
  75.         commands.add(command);
  76.         ++modifications;
  77.     }

  78.     /**
  79.      * Appends a keep command to the script.
  80.      *
  81.      * @param command  command to add
  82.      */
  83.     public void append(final KeepCommand<T> command) {
  84.         commands.add(command);
  85.         ++lcsLength;
  86.     }

  87.     /**
  88.      * Gets the length of the Longest Common Subsequence (LCS). The length of the
  89.      * longest common subsequence is the number of {@link KeepCommand keep
  90.      * commands} in the script.
  91.      *
  92.      * @return length of the Longest Common Subsequence
  93.      */
  94.     public int getLCSLength() {
  95.         return lcsLength;
  96.     }

  97.     /**
  98.      * Gets the number of effective modifications. The number of effective
  99.      * modification is the number of {@link DeleteCommand delete} and
  100.      * {@link InsertCommand insert} commands in the script.
  101.      *
  102.      * @return number of effective modifications
  103.      */
  104.     public int getModifications() {
  105.         return modifications;
  106.     }

  107.     /**
  108.      * Visits the script. The script implements the <em>visitor</em> design
  109.      * pattern, this method is the entry point to which the user supplies its
  110.      * own visitor, the script will be responsible to drive it through the
  111.      * commands in order and call the appropriate method as each command is
  112.      * encountered.
  113.      *
  114.      * @param visitor  the visitor that will visit all commands in turn
  115.      */
  116.     public void visit(final CommandVisitor<T> visitor) {
  117.         commands.forEach(command -> command.accept(visitor));
  118.     }

  119. }