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
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 SequencesComparator#getScript SequencesComparator.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 * There are only three types of commands, all of which are subclasses of this
029 * abstract class. Each command is associated with one object belonging to at
030 * least one of the sequences. These commands are {@link InsertCommand
031 * InsertCommand} which correspond to an object of the second sequence being
032 * inserted into the first sequence, {@link DeleteCommand DeleteCommand} which
033 * correspond to an object of the first sequence being removed and
034 * {@link KeepCommand KeepCommand} which correspond to an object of the first
035 * sequence which <code>equals</code> an object in the second sequence. It is
036 * guaranteed that comparison is always performed this way (i.e. the
037 * <code>equals</code> method of the object from the first sequence is used and
038 * the object passed as an argument comes from the second sequence) ; this can
039 * be important if subclassing is used for some elements in the first sequence
040 * and the <code>equals</code> method is specialized.
041 *
042 * @see SequencesComparator
043 * @see EditScript
044 *
045 * @since 4.0
046 */
047public abstract class EditCommand<T> {
048
049    /** Object on which the command should be applied. */
050    private final T object;
051
052    /**
053     * Simple constructor. Creates a new instance of EditCommand
054     *
055     * @param object  reference to the object associated with this command, this
056     *   refers to an element of one of the sequences being compared
057     */
058    protected EditCommand(final T object) {
059        this.object = object;
060    }
061
062    /**
063     * Returns the object associated with this command.
064     *
065     * @return the object on which the command is applied
066     */
067    protected T getObject() {
068        return object;
069    }
070
071    /**
072     * Accept a visitor.
073     * <p>
074     * This method is invoked for each commands belonging to
075     * an {@link EditScript EditScript}, in order to implement the visitor design pattern
076     *
077     * @param visitor  the visitor to be accepted
078     */
079    public abstract void accept(CommandVisitor<T> visitor);
080
081}