    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 * @param <T> object type
    051 * @since 1.0
    052 */
    053public abstract class EditCommand<T> {
    054
    055    /** Object on which the command should be applied. */
    056    private final T object;
    057
    058    /**
    059     * Constructs a new instance of EditCommand.
    060     *
    061     * @param object  reference to the object associated with this command, this
    062     *   refers to an element of one of the sequences being compared
    063     */
    064    protected EditCommand(final T object) {
    065        this.object = object;
    066    }
    067
    068    /**
    069     * Accepts a visitor.
    070     * <p>
    071     * This method is invoked for each commands belonging to
    072     * an {@link EditScript EditScript}, in order to implement the visitor design pattern
    073     * </p>
    074     *
    075     * @param visitor  the visitor to be accepted
    076     */
    077    public abstract void accept(CommandVisitor<T> visitor);
    078
    079    /**
    080     * Gets the object associated with this command.
    081     *
    082     * @return The object on which the command is applied
    083     */
    084    protected T getObject() {
    085        return object;
    086    }
    087
    088}