001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.collections4.sequence; 018 019/** 020 * This interface should be implemented by user object to walk 021 * through {@link EditScript EditScript} objects. 022 * <p> 023 * Users should implement this interface in order to walk through 024 * the {@link EditScript EditScript} object created by the comparison 025 * of two sequences. This is a direct application of the visitor 026 * design pattern. The {@link EditScript#visit EditScript.visit} 027 * method takes an object implementing this interface as an argument, 028 * it will perform the loop over all commands in the script and the 029 * proper methods of the user class will be called as the commands are 030 * encountered. 031 * <p> 032 * The implementation of the user visitor class will depend on the 033 * need. Here are two examples. 034 * <p> 035 * The first example is a visitor that build the longest common 036 * subsequence: 037 * <pre> 038 * import org.apache.commons.collections4.comparators.sequence.CommandVisitor; 039 * 040 * import java.util.ArrayList; 041 * 042 * public class LongestCommonSubSequence implements CommandVisitor { 043 * 044 * public LongestCommonSubSequence() { 045 * a = new ArrayList(); 046 * } 047 * 048 * public void visitInsertCommand(Object object) { 049 * } 050 * 051 * public void visitKeepCommand(Object object) { 052 * a.add(object); 053 * } 054 * 055 * public void visitDeleteCommand(Object object) { 056 * } 057 * 058 * public Object[] getSubSequence() { 059 * return a.toArray(); 060 * } 061 * 062 * private ArrayList a; 063 * 064 * } 065 * </pre> 066 * <p> 067 * The second example is a visitor that shows the commands and the way 068 * they transform the first sequence into the second one: 069 * <pre> 070 * import org.apache.commons.collections4.comparators.sequence.CommandVisitor; 071 * 072 * import java.util.Arrays; 073 * import java.util.ArrayList; 074 * import java.util.Iterator; 075 * 076 * public class ShowVisitor implements CommandVisitor { 077 * 078 * public ShowVisitor(Object[] sequence1) { 079 * v = new ArrayList(); 080 * v.addAll(Arrays.asList(sequence1)); 081 * index = 0; 082 * } 083 * 084 * public void visitInsertCommand(Object object) { 085 * v.insertElementAt(object, index++); 086 * display("insert", object); 087 * } 088 * 089 * public void visitKeepCommand(Object object) { 090 * ++index; 091 * display("keep ", object); 092 * } 093 * 094 * public void visitDeleteCommand(Object object) { 095 * v.remove(index); 096 * display("delete", object); 097 * } 098 * 099 * private void display(String commandName, Object object) { 100 * System.out.println(commandName + " " + object + " ->" + this); 101 * } 102 * 103 * public String toString() { 104 * StringBuffer buffer = new StringBuffer(); 105 * for (Iterator iter = v.iterator(); iter.hasNext();) { 106 * buffer.append(' ').append(iter.next()); 107 * } 108 * return buffer.toString(); 109 * } 110 * 111 * private ArrayList v; 112 * private int index; 113 * 114 * } 115 * </pre> 116 * 117 * @since 4.0 118 */ 119public interface CommandVisitor<T> { 120 121 /** 122 * Method called when an insert command is encountered. 123 * 124 * @param object object to insert (this object comes from the second sequence) 125 */ 126 void visitInsertCommand(T object); 127 128 /** 129 * Method called when a keep command is encountered. 130 * 131 * @param object object to keep (this object comes from the first sequence) 132 */ 133 void visitKeepCommand(T object); 134 135 /** 136 * Method called when a delete command is encountered. 137 * 138 * @param object object to delete (this object comes from the first sequence) 139 */ 140 void visitDeleteCommand(T object); 141 142}