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.collections4.sequence; 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 SequencesComparator SequencesComparator} 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 SequencesComparator 039 * @see EditCommand 040 * @see CommandVisitor 041 * @see ReplacementsHandler 042 * 043 * @since 4.0 044 */ 045public class EditScript<T> { 046 047 /** Container for the commands. */ 048 private final List<EditCommand<T>> commands; 049 050 /** Length of the longest common subsequence. */ 051 private int lcsLength; 052 053 /** Number of modifications. */ 054 private int modifications; 055 056 /** 057 * Simple constructor. Creates a new empty script. 058 */ 059 public EditScript() { 060 commands = new ArrayList<>(); 061 lcsLength = 0; 062 modifications = 0; 063 } 064 065 /** 066 * Add a keep command to the script. 067 * 068 * @param command command to add 069 */ 070 public void append(final KeepCommand<T> command) { 071 commands.add(command); 072 ++lcsLength; 073 } 074 075 /** 076 * Add an insert command to the script. 077 * 078 * @param command command to add 079 */ 080 public void append(final InsertCommand<T> command) { 081 commands.add(command); 082 ++modifications; 083 } 084 085 /** 086 * Add a delete command to the script. 087 * 088 * @param command command to add 089 */ 090 public void append(final DeleteCommand<T> command) { 091 commands.add(command); 092 ++modifications; 093 } 094 095 /** 096 * Visit the script. The script implements the <em>visitor</em> design 097 * pattern, this method is the entry point to which the user supplies its 098 * own visitor, the script will be responsible to drive it through the 099 * commands in order and call the appropriate method as each command is 100 * encountered. 101 * 102 * @param visitor the visitor that will visit all commands in turn 103 */ 104 public void visit(final CommandVisitor<T> visitor) { 105 for (final EditCommand<T> command : commands) { 106 command.accept(visitor); 107 } 108 } 109 110 /** 111 * Get the length of the Longest Common Subsequence (LCS). The length of the 112 * longest common subsequence is the number of {@link KeepCommand keep 113 * commands} in the script. 114 * 115 * @return length of the Longest Common Subsequence 116 */ 117 public int getLCSLength() { 118 return lcsLength; 119 } 120 121 /** 122 * Get the number of effective modifications. The number of effective 123 * modification is the number of {@link DeleteCommand delete} and 124 * {@link InsertCommand insert} commands in the script. 125 * 126 * @return number of effective modifications 127 */ 128 public int getModifications() { 129 return modifications; 130 } 131 132}