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</code> an object in the second sequence. It is
37 * guaranteed that comparison is always performed this way (i.e. the
38 * <code>equals</code> 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</code> 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 *
51 * @param <T> object type
52 * @since 1.0
53 */
54 public abstract class EditCommand<T> {
55
56 /** Object on which the command should be applied. */
57 private final T object;
58
59 /**
60 * Simple constructor. Creates a new instance of EditCommand
61 *
62 * @param object reference to the object associated with this command, this
63 * refers to an element of one of the sequences being compared
64 */
65 protected EditCommand(final T object) {
66 this.object = object;
67 }
68
69 /**
70 * Returns the object associated with this command.
71 *
72 * @return the object on which the command is applied
73 */
74 protected T getObject() {
75 return object;
76 }
77
78 /**
79 * Accept a visitor.
80 * <p>
81 * This method is invoked for each commands belonging to
82 * an {@link EditScript EditScript}, in order to implement the visitor design pattern
83 *
84 * @param visitor the visitor to be accepted
85 */
86 public abstract void accept(CommandVisitor<T> visitor);
87
88 }