001/* 002 * ==================================================================== 003 * 004 * The Apache Software License, Version 1.1 005 * 006 * Copyright (c) 1999-2003 The Apache Software Foundation. 007 * All rights reserved. 008 * 009 * Redistribution and use in source and binary forms, with or without 010 * modification, are permitted provided that the following conditions 011 * are met: 012 * 013 * 1. Redistributions of source code must retain the above copyright 014 * notice, this list of conditions and the following disclaimer. 015 * 016 * 2. Redistributions in binary form must reproduce the above copyright 017 * notice, this list of conditions and the following disclaimer in 018 * the documentation and/or other materials provided with the 019 * distribution. 020 * 021 * 3. The end-user documentation included with the redistribution, if 022 * any, must include the following acknowledgement: 023 * "This product includes software developed by the 024 * Apache Software Foundation (http://www.apache.org/)." 025 * Alternately, this acknowledgement may appear in the software itself, 026 * if and wherever such third-party acknowledgements normally appear. 027 * 028 * 4. The names "The Jakarta Project", "Commons", and "Apache Software 029 * Foundation" must not be used to endorse or promote products derived 030 * from this software without prior written permission. For written 031 * permission, please contact [email protected]. 032 * 033 * 5. Products derived from this software may not be called "Apache" 034 * nor may "Apache" appear in their names without prior written 035 * permission of the Apache Software Foundation. 036 * 037 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 038 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 039 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 040 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 041 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 042 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 043 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 044 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 045 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 046 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 047 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 048 * SUCH DAMAGE. 049 * ==================================================================== 050 * 051 * This software consists of voluntary contributions made by many 052 * individuals on behalf of the Apache Software Foundation. For more 053 * information on the Apache Software Foundation, please see 054 * <http://www.apache.org/>. 055 * 056 */ 057 058package org.apache.wicket.util.diff; 059 060import java.util.ArrayList; 061import java.util.Arrays; 062import java.util.LinkedList; 063import java.util.List; 064import java.util.ListIterator; 065 066/** 067 * A Revision holds the series of deltas that describe the differences between two sequences. 068 * 069 * @version $Revision: 1.1 $ $Date: 2006/03/12 00:24:21 $ 070 * 071 * @author <a href="mailto:[email protected]">Juanco Anez</a> 072 * @author <a href="mailto:[email protected]">Brian McBride</a> 073 * 074 * @see Delta 075 * @see Diff 076 * @see Chunk 077 * @see Revision modifications 27 Apr 2003 bwm 078 * 079 * Added visitor pattern Visitor interface and accept() method. 080 */ 081 082public class Revision extends ToString 083{ 084 085 List<Delta> deltas_ = new LinkedList<>(); 086 087 /** 088 * Creates an empty Revision. 089 */ 090 public Revision() 091 { 092 } 093 094 /** 095 * Adds a delta to this revision. 096 * 097 * @param delta 098 * the {@link Delta Delta} to add. 099 */ 100 public synchronized void addDelta(final Delta delta) 101 { 102 if (delta == null) 103 { 104 throw new IllegalArgumentException("new delta is null"); 105 } 106 deltas_.add(delta); 107 } 108 109 /** 110 * Adds a delta to the start of this revision. 111 * 112 * @param delta 113 * the {@link Delta Delta} to add. 114 */ 115 public synchronized void insertDelta(final Delta delta) 116 { 117 if (delta == null) 118 { 119 throw new IllegalArgumentException("new delta is null"); 120 } 121 deltas_.add(0, delta); 122 } 123 124 /** 125 * Retrieves a delta from this revision by position. 126 * 127 * @param i 128 * the position of the delta to retrieve. 129 * @return the specified delta 130 */ 131 public Delta getDelta(final int i) 132 { 133 return deltas_.get(i); 134 } 135 136 /** 137 * Returns the number of deltas in this revision. 138 * 139 * @return the number of deltas. 140 */ 141 public int size() 142 { 143 return deltas_.size(); 144 } 145 146 /** 147 * Applies the series of deltas in this revision as patches to the given text. 148 * 149 * @param src 150 * the text to patch, which the method doesn't change. 151 * @return the resulting text after the patches have been applied. 152 * @throws PatchFailedException 153 * if any of the patches cannot be applied. 154 */ 155 public Object[] patch(final Object[] src) throws PatchFailedException 156 { 157 List<Object> target = new ArrayList<>(Arrays.asList(src)); 158 applyTo(target); 159 return target.toArray(); 160 } 161 162 /** 163 * Applies the series of deltas in this revision as patches to the given text. 164 * 165 * @param target 166 * the text to patch. 167 * @throws PatchFailedException 168 * if any of the patches cannot be applied. 169 */ 170 public synchronized void applyTo(final List<Object> target) throws PatchFailedException 171 { 172 ListIterator<Delta> i = deltas_.listIterator(deltas_.size()); 173 while (i.hasPrevious()) 174 { 175 Delta delta = i.previous(); 176 delta.patch(target); 177 } 178 } 179 180 /** 181 * Converts this revision into its Unix diff style string representation. 182 * 183 * @param s 184 * a {@link StringBuilder StringBuffer} to which the string representation will be 185 * appended. 186 */ 187 @Override 188 public synchronized void toString(final StringBuilder s) 189 { 190 for (Delta delta : deltas_) 191 { 192 delta.toString(s); 193 } 194 } 195 196 /** 197 * Converts this revision into its RCS style string representation. 198 * 199 * @param s 200 * a {@link StringBuilder StringBuffer} to which the string representation will be 201 * appended. 202 * @param EOL 203 * the string to use as line separator. 204 */ 205 public synchronized void toRCSString(final StringBuilder s, final String EOL) 206 { 207 for (Delta deltas : deltas_) 208 { 209 deltas.toRCSString(s, EOL); 210 } 211 } 212 213 /** 214 * Converts this revision into its RCS style string representation. 215 * 216 * @param s 217 * a {@link StringBuilder StringBuffer} to which the string representation will be 218 * appended. 219 */ 220 public void toRCSString(final StringBuilder s) 221 { 222 toRCSString(s, Diff.NL); 223 } 224 225 /** 226 * Converts this delta into its RCS style string representation. 227 * 228 * @param EOL 229 * the string to use as line separator. 230 * @return String 231 */ 232 public String toRCSString(final String EOL) 233 { 234 StringBuilder s = new StringBuilder(); 235 toRCSString(s, EOL); 236 return s.toString(); 237 } 238 239 /** 240 * Converts this delta into its RCS style string representation using the default line 241 * separator. 242 * 243 * @return String 244 */ 245 public String toRCSString() 246 { 247 return toRCSString(Diff.NL); 248 } 249 250 /** 251 * Accepts a visitor. 252 * 253 * @param visitor 254 * the {@link RevisionVisitor} visiting this instance 255 */ 256 public void accept(final RevisionVisitor visitor) 257 { 258 visitor.visit(this); 259 for (Delta delta : deltas_) 260 { 261 delta.accept(visitor); 262 } 263 } 264 265}