001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.text.diff; 018 019import java.util.ArrayList; 020import java.util.List; 021 022/** 023 * This class gathers all the {@link EditCommand commands} needed to transform 024 * one objects sequence into another objects sequence. 025 * <p> 026 * An edit script is the most general view of the differences between two 027 * sequences. It is built as the result of the comparison between two sequences 028 * by the {@link StringsComparator StringsComparator} class. The user can 029 * walk through it using the <em>visitor</em> design pattern. 030 * <p> 031 * It is guaranteed that the objects embedded in the {@link InsertCommand insert 032 * commands} come from the second sequence and that the objects embedded in 033 * either the {@link DeleteCommand delete commands} or {@link KeepCommand keep 034 * commands} come from the first sequence. This can be important if subclassing 035 * is used for some elements in the first sequence and the <code>equals</code> 036 * method is specialized. 037 * 038 * @see StringsComparator 039 * @see EditCommand 040 * @see CommandVisitor 041 * @see ReplacementsHandler 042 * 043 * @param <T> object type 044 * @since 1.0 045 */ 046public class EditScript<T> { 047 048 /** Container for the commands. */ 049 private final List<EditCommand<T>> commands; 050 051 /** Length of the longest common subsequence. */ 052 private int lcsLength; 053 054 /** Number of modifications. */ 055 private int modifications; 056 057 /** 058 * Simple constructor. Creates a new empty script. 059 */ 060 public EditScript() { 061 commands = new ArrayList<>(); 062 lcsLength = 0; 063 modifications = 0; 064 } 065 066 /** 067 * Add a keep command to the script. 068 * 069 * @param command command to add 070 */ 071 public void append(final KeepCommand<T> command) { 072 commands.add(command); 073 ++lcsLength; 074 } 075 076 /** 077 * Add an insert command to the script. 078 * 079 * @param command command to add 080 */ 081 public void append(final InsertCommand<T> command) { 082 commands.add(command); 083 ++modifications; 084 } 085 086 /** 087 * Add a delete command to the script. 088 * 089 * @param command command to add 090 */ 091 public void append(final DeleteCommand<T> command) { 092 commands.add(command); 093 ++modifications; 094 } 095 096 /** 097 * Visit the script. The script implements the <em>visitor</em> design 098 * pattern, this method is the entry point to which the user supplies its 099 * own visitor, the script will be responsible to drive it through the 100 * commands in order and call the appropriate method as each command is 101 * encountered. 102 * 103 * @param visitor the visitor that will visit all commands in turn 104 */ 105 public void visit(final CommandVisitor<T> visitor) { 106 for (final EditCommand<T> command : commands) { 107 command.accept(visitor); 108 } 109 } 110 111 /** 112 * Get the length of the Longest Common Subsequence (LCS). The length of the 113 * longest common subsequence is the number of {@link KeepCommand keep 114 * commands} in the script. 115 * 116 * @return length of the Longest Common Subsequence 117 */ 118 public int getLCSLength() { 119 return lcsLength; 120 } 121 122 /** 123 * Get the number of effective modifications. The number of effective 124 * modification is the number of {@link DeleteCommand delete} and 125 * {@link InsertCommand insert} commands in the script. 126 * 127 * @return number of effective modifications 128 */ 129 public int getModifications() { 130 return modifications; 131 } 132 133}