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 019/** 020 * Abstract base class for all commands used to transform an objects sequence 021 * into another one. 022 * <p> 023 * When two objects sequences are compared through the 024 * {@link StringsComparator#getScript StringsComparator.getScript} method, 025 * the result is provided has a {@link EditScript script} containing the commands 026 * that progressively transform the first sequence into the second one. 027 * </p> 028 * <p> 029 * There are only three types of commands, all of which are subclasses of this 030 * abstract class. Each command is associated with one object belonging to at 031 * least one of the sequences. These commands are {@link InsertCommand 032 * InsertCommand} which correspond to an object of the second sequence being 033 * inserted into the first sequence, {@link DeleteCommand DeleteCommand} which 034 * correspond to an object of the first sequence being removed and 035 * {@link KeepCommand KeepCommand} which correspond to an object of the first 036 * sequence which {@code equals} an object in the second sequence. It is 037 * guaranteed that comparison is always performed this way (i.e. the 038 * {@code equals} method of the object from the first sequence is used and 039 * the object passed as an argument comes from the second sequence) ; this can 040 * be important if subclassing is used for some elements in the first sequence 041 * and the {@code equals} method is specialized. 042 * </p> 043 * 044 * <p> 045 * This code has been adapted from Apache Commons Collections 4.0. 046 * </p> 047 * 048 * @see StringsComparator 049 * @see EditScript 050 * 051 * @param <T> object type 052 * @since 1.0 053 */ 054public abstract class EditCommand<T> { 055 056 /** Object on which the command should be applied. */ 057 private final T object; 058 059 /** 060 * Constructs a new instance of EditCommand. 061 * 062 * @param object reference to the object associated with this command, this 063 * refers to an element of one of the sequences being compared 064 */ 065 protected EditCommand(final T object) { 066 this.object = object; 067 } 068 069 /** 070 * Accepts a visitor. 071 * <p> 072 * This method is invoked for each commands belonging to 073 * an {@link EditScript EditScript}, in order to implement the visitor design pattern 074 * </p> 075 * 076 * @param visitor the visitor to be accepted 077 */ 078 public abstract void accept(CommandVisitor<T> visitor); 079 080 /** 081 * Gets the object associated with this command. 082 * 083 * @return The object on which the command is applied 084 */ 085 protected T getObject() { 086 return object; 087 } 088 089}