KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jrcs > diff > Revision


1 /*
2  * ====================================================================
3  *
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 1999-2003 The Apache Software Foundation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowledgement:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgement may appear in the software itself,
26  * if and wherever such third-party acknowledgements normally appear.
27  *
28  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
29  * Foundation" must not be used to endorse or promote products derived
30  * from this software without prior written permission. For written
31  * permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache"
34  * nor may "Apache" appear in their names without prior written
35  * permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  *
56  */

57
58 package org.apache.commons.jrcs.diff;
59
60 import java.util.ArrayList JavaDoc;
61 import java.util.Arrays JavaDoc;
62 import java.util.Iterator JavaDoc;
63 import java.util.LinkedList JavaDoc;
64 import java.util.List JavaDoc;
65 import java.util.ListIterator JavaDoc;
66
67 import org.apache.commons.jrcs.diff.print.RCSPrint;
68 import org.apache.commons.jrcs.util.ToString;
69
70
71 /**
72  * A Revision holds the series of deltas that describe the differences
73  * between two sequences.
74  *
75  * @version $Revision: 1.2 $ $Date: 2005/06/16 18:11:11 $
76  *
77  * @author <a HREF="mailto:juanco@suigeneris.org">Juanco Anez</a>
78  * @author <a HREF="mailto:bwm@hplb.hpl.hp.com">Brian McBride</a>
79  *
80  * @see Delta
81  * @see Diff
82  * @see Chunk
83  * @see Revision
84  *
85  * modifications
86  * 27 Apr 2003 bwm
87  *
88  * Added visitor pattern Visitor interface and accept() method.
89  */

90
91 public class Revision
92         extends ToString
93 {
94
95     List JavaDoc deltas_ = new LinkedList JavaDoc();
96
97
98     /**
99      * Creates an empty Revision.
100      */

101     public Revision()
102     {
103         // do nothing
104
}
105
106
107     /**
108      * Adds a delta to this revision.
109      * @param delta the {@link Delta Delta} to add.
110      */

111     public synchronized void addDelta(Delta delta)
112     {
113         if (delta == null)
114         {
115             throw new IllegalArgumentException JavaDoc("new delta is null");
116         }
117         deltas_.add(delta);
118     }
119
120
121     /**
122      * Adds a delta to the start of this revision.
123      * @param delta the {@link Delta Delta} to add.
124      */

125     public synchronized void insertDelta(Delta delta)
126     {
127         if (delta == null)
128         {
129             throw new IllegalArgumentException JavaDoc("new delta is null");
130         }
131         deltas_.add(0, delta);
132     }
133
134
135     /**
136      * Retrieves a delta from this revision by position.
137      * @param i the position of the delta to retrieve.
138      * @return the specified delta
139      */

140     public Delta getDelta(int i)
141     {
142         return (Delta) deltas_.get(i);
143     }
144
145     /**
146      * Returns the number of deltas in this revision.
147      * @return the number of deltas.
148      */

149     public int size()
150     {
151         return deltas_.size();
152     }
153
154     /**
155      * Applies the series of deltas in this revision as patches to
156      * the given text.
157      * @param src the text to patch, which the method doesn't change.
158      * @return the resulting text after the patches have been applied.
159      * @throws PatchFailedException if any of the patches cannot be applied.
160      */

161     public Object JavaDoc[] patch(Object JavaDoc[] src) throws PatchFailedException
162     {
163         List JavaDoc target = new ArrayList JavaDoc(Arrays.asList(src));
164         applyTo(target);
165         return target.toArray();
166     }
167
168     /**
169      * Applies the series of deltas in this revision as patches to
170      * the given text.
171      * @param target the text to patch.
172      * @throws PatchFailedException if any of the patches cannot be applied.
173      */

174     public synchronized void applyTo(List JavaDoc target) throws PatchFailedException
175     {
176         ListIterator JavaDoc i = deltas_.listIterator(deltas_.size());
177         while (i.hasPrevious())
178         {
179             Delta delta = (Delta) i.previous();
180             delta.patch(target);
181         }
182     }
183
184     /**
185      * Converts this revision into its Unix diff style string representation.
186      * @param s a {@link StringBuffer StringBuffer} to which the string
187      * representation will be appended.
188      */

189     public synchronized void toString(StringBuffer JavaDoc s)
190     {
191         Iterator JavaDoc i = deltas_.iterator();
192         while (i.hasNext())
193         {
194             ((Delta) i.next()).toString(s);
195         }
196     }
197
198     /**
199      * Converts this revision into its RCS style string representation.
200      * @param s a {@link StringBuffer StringBuffer} to which the string
201      * representation will be appended.
202      * @param EOL the string to use as line separator.
203      */

204     public synchronized void toRCSString(StringBuffer JavaDoc s, String JavaDoc EOL)
205     {
206         RCSPrint printer = new RCSPrint(s);
207         printer.setEOL(EOL);
208         accept(printer);
209     }
210
211     /**
212      * Converts this revision into its RCS style string representation.
213      * @param s a {@link StringBuffer StringBuffer} to which the string
214      * representation will be appended.
215      */

216     public void toRCSString(StringBuffer JavaDoc s)
217     {
218         toRCSString(s, Diff.NL);
219     }
220
221     /**
222      * Converts this delta into its RCS style string representation.
223      * @param EOL the string to use as line separator.
224      */

225     public String JavaDoc toRCSString(String JavaDoc EOL)
226     {
227         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
228         toRCSString(s, EOL);
229         return s.toString();
230     }
231
232     /**
233      * Converts this delta into its RCS style string representation
234      * using the default line separator.
235      */

236     public String JavaDoc toRCSString()
237     {
238         return toRCSString(Diff.NL);
239     }
240
241     /**
242      * Accepts a visitor.
243      * @param visitor the {@link Visitor} visiting this instance
244      */

245     public void accept(RevisionVisitor visitor) {
246         visitor.visit(this);
247         Iterator JavaDoc iter = deltas_.iterator();
248         while (iter.hasNext()) {
249             ((Delta) iter.next()).accept(visitor);
250         }
251     }
252
253 }
254
Popular Tags