EditCommand.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.text.diff;

  18. /**
  19.  * Abstract base class for all commands used to transform an objects sequence
  20.  * into another one.
  21.  * <p>
  22.  * When two objects sequences are compared through the
  23.  * {@link StringsComparator#getScript StringsComparator.getScript} method,
  24.  * the result is provided has a {@link EditScript script} containing the commands
  25.  * that progressively transform the first sequence into the second one.
  26.  * </p>
  27.  * <p>
  28.  * There are only three types of commands, all of which are subclasses of this
  29.  * abstract class. Each command is associated with one object belonging to at
  30.  * least one of the sequences. These commands are {@link InsertCommand
  31.  * InsertCommand} which correspond to an object of the second sequence being
  32.  * inserted into the first sequence, {@link DeleteCommand DeleteCommand} which
  33.  * correspond to an object of the first sequence being removed and
  34.  * {@link KeepCommand KeepCommand} which correspond to an object of the first
  35.  * sequence which {@code equals} an object in the second sequence. It is
  36.  * guaranteed that comparison is always performed this way (i.e. the
  37.  * {@code equals} method of the object from the first sequence is used and
  38.  * the object passed as an argument comes from the second sequence) ; this can
  39.  * be important if subclassing is used for some elements in the first sequence
  40.  * and the {@code equals} method is specialized.
  41.  * </p>
  42.  *
  43.  * <p>
  44.  * This code has been adapted from Apache Commons Collections 4.0.
  45.  * </p>
  46.  *
  47.  * @see StringsComparator
  48.  * @see EditScript
  49.  * @param <T> object type
  50.  * @since 1.0
  51.  */
  52. public abstract class EditCommand<T> {

  53.     /** Object on which the command should be applied. */
  54.     private final T object;

  55.     /**
  56.      * Constructs a new instance of EditCommand.
  57.      *
  58.      * @param object  reference to the object associated with this command, this
  59.      *   refers to an element of one of the sequences being compared
  60.      */
  61.     protected EditCommand(final T object) {
  62.         this.object = object;
  63.     }

  64.     /**
  65.      * Accepts a visitor.
  66.      * <p>
  67.      * This method is invoked for each commands belonging to
  68.      * an {@link EditScript EditScript}, in order to implement the visitor design pattern
  69.      * </p>
  70.      *
  71.      * @param visitor  the visitor to be accepted
  72.      */
  73.     public abstract void accept(CommandVisitor<T> visitor);

  74.     /**
  75.      * Gets the object associated with this command.
  76.      *
  77.      * @return The object on which the command is applied
  78.      */
  79.     protected T getObject() {
  80.         return object;
  81.     }

  82. }