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.collections4.sequence;

  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 SequencesComparator#getScript SequencesComparator.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.  * @param <T> the type of object to apply this command.
  44.  * @see SequencesComparator
  45.  * @see EditScript
  46.  * @since 4.0
  47.  */
  48. public abstract class EditCommand<T> {

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

  51.     /**
  52.      * Simple constructor. Creates a new instance of EditCommand
  53.      *
  54.      * @param object  reference to the object associated with this command, this
  55.      *   refers to an element of one of the sequences being compared
  56.      */
  57.     protected EditCommand(final T object) {
  58.         this.object = object;
  59.     }

  60.     /**
  61.      * Accept a visitor.
  62.      * <p>
  63.      * This method is invoked for each command belonging to
  64.      * an {@link EditScript EditScript}, in order to implement the visitor design pattern
  65.      *
  66.      * @param visitor  the visitor to be accepted
  67.      */
  68.     public abstract void accept(CommandVisitor<T> visitor);

  69.     /**
  70.      * Gets 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. }