View Javadoc
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  /**
20   * Abstract base class for all commands used to transform an objects sequence
21   * into another one.
22   * <p>
23   * When two objects sequences are compared through the
24   * {@link StringsComparator#getScript StringsComparator.getScript} method,
25   * the result is provided has a {@link EditScript script} containing the commands
26   * that progressively transform the first sequence into the second one.
27   * </p>
28   * <p>
29   * There are only three types of commands, all of which are subclasses of this
30   * abstract class. Each command is associated with one object belonging to at
31   * least one of the sequences. These commands are {@link InsertCommand
32   * InsertCommand} which correspond to an object of the second sequence being
33   * inserted into the first sequence, {@link DeleteCommand DeleteCommand} which
34   * correspond to an object of the first sequence being removed and
35   * {@link KeepCommand KeepCommand} which correspond to an object of the first
36   * sequence which {@code equals} an object in the second sequence. It is
37   * guaranteed that comparison is always performed this way (i.e. the
38   * {@code equals} method of the object from the first sequence is used and
39   * the object passed as an argument comes from the second sequence) ; this can
40   * be important if subclassing is used for some elements in the first sequence
41   * and the {@code equals} method is specialized.
42   * </p>
43   *
44   * <p>
45   * This code has been adapted from Apache Commons Collections 4.0.
46   * </p>
47   *
48   * @see StringsComparator
49   * @see EditScript
50   * @param <T> object type
51   * @since 1.0
52   */
53  public abstract class EditCommand<T> {
54  
55      /** Object on which the command should be applied. */
56      private final T object;
57  
58      /**
59       * Constructs a new instance of EditCommand.
60       *
61       * @param object  reference to the object associated with this command, this
62       *   refers to an element of one of the sequences being compared
63       */
64      protected EditCommand(final T object) {
65          this.object = object;
66      }
67  
68      /**
69       * Accepts a visitor.
70       * <p>
71       * This method is invoked for each commands belonging to
72       * an {@link EditScript EditScript}, in order to implement the visitor design pattern
73       * </p>
74       *
75       * @param visitor  the visitor to be accepted
76       */
77      public abstract void accept(CommandVisitor<T> visitor);
78  
79      /**
80       * Gets the object associated with this command.
81       *
82       * @return The object on which the command is applied
83       */
84      protected T getObject() {
85          return object;
86      }
87  
88  }