KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
64
65 /**
66  * Holds a information about a parrt of the text involved in
67  * a differencing or patching operation.
68  *
69  * @version $Id: Chunk.java,v 1.1 2004/10/06 09:47:23 ludovicc Exp $
70  * @author <a HREF="mailto:juanco@suigeneris.org">Juanco Anez</a>
71  * @see Diff
72  * @see Delta
73  */

74 public class Chunk
75     extends org.apache.commons.jrcs.util.ToString
76 {
77
78     protected int anchor;
79
80     protected int count;
81
82     protected List JavaDoc chunk;
83
84     /**
85      * Creates a chunk that doesn't copy the original text.
86      * @param pos the start position in the text.
87      * @param count the size of the chunk.
88      */

89     public Chunk(int pos, int count)
90     {
91         this.anchor = pos;
92         this.count = (count >= 0 ? count : 0);
93     }
94
95     /**
96      * Creates a chunk and saves a copy the original chunk's text.
97      * @param iseq the original text.
98      * @param pos the start position in the text.
99      * @param count the size of the chunk.
100      */

101     public Chunk(Object JavaDoc[] iseq, int pos, int count)
102     {
103         this(pos, count);
104         chunk = slice(iseq, pos, count);
105     }
106
107     /**
108      * Creates a chunk that will be displaced in the resulting text,
109      * and saves a copy the original chunk's text.
110      * @param iseq the original text.
111      * @param pos the start position in the text.
112      * @param count the size of the chunk.
113      * @param offset the position the chunk should have in the resulting text.
114      */

115     public Chunk(Object JavaDoc[] iseq, int pos, int count, int offset)
116     {
117         this(offset, count);
118         chunk = slice(iseq, pos, count);
119     }
120
121     /**
122      * Creates a chunk and saves a copy the original chunk's text.
123      * @param iseq the original text.
124      * @param pos the start position in the text.
125      * @param count the size of the chunk.
126      */

127     public Chunk(List JavaDoc iseq, int pos, int count)
128     {
129         this(pos, count);
130         chunk = slice(iseq, pos, count);
131     }
132
133     /**
134      * Creates a chunk that will be displaced in the resulting text,
135      * and saves a copy the original chunk's text.
136      * @param iseq the original text.
137      * @param pos the start position in the text.
138      * @param count the size of the chunk.
139      * @param offset the position the chunk should have in the resulting text.
140      */

141     public Chunk(List JavaDoc iseq, int pos, int count, int offset)
142     {
143         this(offset, count);
144         chunk = slice(iseq, pos, count);
145     }
146
147     /**
148      * Returns the anchor position of the chunk.
149      * @return the anchor position.
150      */

151     public int anchor()
152     {
153         return anchor;
154     }
155
156     /**
157      * Returns the size of the chunk.
158      * @return the size.
159      */

160     public int size()
161     {
162         return count;
163     }
164
165     /**
166      * Returns the index of the first line of the chunk.
167      */

168     public int first()
169     {
170         return anchor();
171     }
172
173     /**
174      * Returns the index of the last line of the chunk.
175      */

176     public int last()
177     {
178         return anchor() + size() - 1;
179     }
180
181     /**
182      * Returns the <i>from</i> index of the chunk in RCS terms.
183      */

184     public int rcsfrom()
185     {
186         return anchor + 1;
187     }
188
189     /**
190      * Returns the <i>to</i> index of the chunk in RCS terms.
191      */

192     public int rcsto()
193     {
194         return anchor + count;
195     }
196
197     /**
198      * Returns the text saved for this chunk.
199      * @return the text.
200      */

201     public List JavaDoc chunk()
202     {
203         return chunk;
204     }
205
206     /**
207      * Verifies that this chunk's saved text matches the corresponding
208      * text in the given sequence.
209      * @param target the sequence to verify against.
210      * @return true if the texts match.
211      */

212     public boolean verify(List JavaDoc target)
213     {
214         if (chunk == null)
215         {
216             return true;
217         }
218         if (last() > target.size())
219         {
220             return false;
221         }
222         for (int i = 0; i < count; i++)
223         {
224             if (!target.get(anchor + i).equals(chunk.get(i)))
225             {
226                 return false;
227             }
228         }
229         return true;
230     }
231
232     /**
233      * Delete this chunk from he given text.
234      * @param target the text to delete from.
235      */

236     public void applyDelete(List JavaDoc target)
237     {
238         for (int i = last(); i >= first(); i--)
239         {
240             target.remove(i);
241         }
242     }
243
244     /**
245      * Add the text of this chunk to the target at the given position.
246      * @param start where to add the text.
247      * @param target the text to add to.
248      */

249     public void applyAdd(int start, List JavaDoc target)
250     {
251         Iterator JavaDoc i = chunk.iterator();
252         while (i.hasNext())
253         {
254             target.add(start++, i.next());
255         }
256     }
257
258     /**
259      * Provide a string image of the chunk using the an empty prefix and
260      * postfix.
261      */

262     public void toString(StringBuffer JavaDoc s)
263     {
264         toString(s, "", "");
265     }
266
267     /**
268      * Provide a string image of the chunk using the given prefix and
269      * postfix.
270      * @param s where the string image should be appended.
271      * @param prefix the text thatshould prefix each line.
272      * @param postfix the text that should end each line.
273      */

274     public StringBuffer JavaDoc toString(StringBuffer JavaDoc s, String JavaDoc prefix, String JavaDoc postfix)
275     {
276         if (chunk != null)
277         {
278             Iterator JavaDoc i = chunk.iterator();
279             while (i.hasNext())
280             {
281                 s.append(prefix);
282                 s.append(i.next());
283                 s.append(postfix);
284             }
285         }
286         return s;
287     }
288
289     /**
290      * Retreives the specified part from a {@link List List}.
291      * @param seq the list to retreive a slice from.
292      * @param pos the start position.
293      * @param count the number of items in the slice.
294      * @return a {@link List List} containing the specified items.
295      */

296     public static List JavaDoc slice(List JavaDoc seq, int pos, int count)
297     {
298         if (count <= 0)
299         {
300             return new ArrayList JavaDoc(seq.subList(pos, pos));
301         }
302         else
303         {
304             return new ArrayList JavaDoc(seq.subList(pos, pos + count));
305         }
306     }
307
308     /**
309      * Retrieves a slice from an {@link Object Object} array.
310      * @param seq the list to retreive a slice from.
311      * @param pos the start position.
312      * @param count the number of items in the slice.
313      * @return a {@link List List} containing the specified items.
314      */

315     public static List JavaDoc slice(Object JavaDoc[] seq, int pos, int count)
316     {
317         return slice(Arrays.asList(seq), pos, count);
318     }
319
320     /**
321      * Provide a string representation of the numeric range of this chunk.
322      */

323     public String JavaDoc rangeString()
324     {
325         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
326         rangeString(result);
327         return result.toString();
328     }
329
330     /**
331      * Provide a string representation of the numeric range of this chunk.
332      * @param s where the string representation should be appended.
333      */

334     public void rangeString(StringBuffer JavaDoc s)
335     {
336         rangeString(s, ",");
337     }
338
339     /**
340      * Provide a string representation of the numeric range of this chunk.
341      * @param s where the string representation should be appended.
342      * @param separ what to use as line separator.
343      */

344     public void rangeString(StringBuffer JavaDoc s, String JavaDoc separ)
345     {
346         if (size() <= 1)
347         {
348             s.append(Integer.toString(rcsfrom()));
349         }
350         else
351         {
352             s.append(Integer.toString(rcsfrom()));
353             s.append(separ);
354             s.append(Integer.toString(rcsto()));
355         }
356     }
357 }
Popular Tags