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.collections4.sequence;
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 SequencesComparator#getScript SequencesComparator.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 * @param <T> the type of object to apply this command.
45 * @see SequencesComparator
46 * @see EditScript
47 * @since 4.0
48 */
49 public abstract class EditCommand<T> {
50
51 /** Object on which the command should be applied. */
52 private final T object;
53
54 /**
55 * Simple constructor. Creates a new instance of EditCommand
56 *
57 * @param object reference to the object associated with this command, this
58 * refers to an element of one of the sequences being compared
59 */
60 protected EditCommand(final T object) {
61 this.object = object;
62 }
63
64 /**
65 * Accept a visitor.
66 * <p>
67 * This method is invoked for each command belonging to
68 * an {@link EditScript EditScript}, in order to implement the visitor design pattern
69 *
70 * @param visitor the visitor to be accepted
71 */
72 public abstract void accept(CommandVisitor<T> visitor);
73
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
83 }