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 apache@apache.org.
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:
058: package org.apache.commons.jrcs.diff;
059:
060: import java.util.ArrayList;
061: import java.util.Arrays;
062: import java.util.Iterator;
063: import java.util.List;
064:
065: /**
066: * Holds a information about a parrt of the text involved in a differencing or
067: * patching operation.
068: *
069: * @version $Id: Chunk.java 7756 2006-04-13 12:25:49Z ian@caret.cam.ac.uk $
070: * @author <a href="mailto:juanco@suigeneris.org">Juanco Anez</a>
071: * @see Diff
072: * @see Delta
073: */
074: public class Chunk extends org.apache.commons.jrcs.util.ToString {
075:
076: protected int anchor;
077:
078: protected int count;
079:
080: protected List chunk;
081:
082: /**
083: * Creates a chunk that doesn't copy the original text.
084: *
085: * @param pos
086: * the start position in the text.
087: * @param count
088: * the size of the chunk.
089: */
090: public Chunk(int pos, int count) {
091: this .anchor = pos;
092: this .count = (count >= 0 ? count : 0);
093: }
094:
095: /**
096: * Creates a chunk and saves a copy the original chunk's text.
097: *
098: * @param iseq
099: * the original text.
100: * @param pos
101: * the start position in the text.
102: * @param count
103: * the size of the chunk.
104: */
105: public Chunk(Object[] iseq, int pos, int count) {
106: this (pos, count);
107: chunk = slice(iseq, pos, count);
108: }
109:
110: /**
111: * Creates a chunk that will be displaced in the resulting text, and saves a
112: * copy the original chunk's text.
113: *
114: * @param iseq
115: * the original text.
116: * @param pos
117: * the start position in the text.
118: * @param count
119: * the size of the chunk.
120: * @param offset
121: * the position the chunk should have in the resulting text.
122: */
123: public Chunk(Object[] iseq, int pos, int count, int offset) {
124: this (offset, count);
125: chunk = slice(iseq, pos, count);
126: }
127:
128: /**
129: * Creates a chunk and saves a copy the original chunk's text.
130: *
131: * @param iseq
132: * the original text.
133: * @param pos
134: * the start position in the text.
135: * @param count
136: * the size of the chunk.
137: */
138: public Chunk(List iseq, int pos, int count) {
139: this (pos, count);
140: chunk = slice(iseq, pos, count);
141: }
142:
143: /**
144: * Creates a chunk that will be displaced in the resulting text, and saves a
145: * copy the original chunk's text.
146: *
147: * @param iseq
148: * the original text.
149: * @param pos
150: * the start position in the text.
151: * @param count
152: * the size of the chunk.
153: * @param offset
154: * the position the chunk should have in the resulting text.
155: */
156: public Chunk(List iseq, int pos, int count, int offset) {
157: this (offset, count);
158: chunk = slice(iseq, pos, count);
159: }
160:
161: /**
162: * Returns the anchor position of the chunk.
163: *
164: * @return the anchor position.
165: */
166: public int anchor() {
167: return anchor;
168: }
169:
170: /**
171: * Returns the size of the chunk.
172: *
173: * @return the size.
174: */
175: public int size() {
176: return count;
177: }
178:
179: /**
180: * Returns the index of the first line of the chunk.
181: */
182: public int first() {
183: return anchor();
184: }
185:
186: /**
187: * Returns the index of the last line of the chunk.
188: */
189: public int last() {
190: return anchor() + size() - 1;
191: }
192:
193: /**
194: * Returns the <i>from</i> index of the chunk in RCS terms.
195: */
196: public int rcsfrom() {
197: return anchor + 1;
198: }
199:
200: /**
201: * Returns the <i>to</i> index of the chunk in RCS terms.
202: */
203: public int rcsto() {
204: return anchor + count;
205: }
206:
207: /**
208: * Returns the text saved for this chunk.
209: *
210: * @return the text.
211: */
212: public List chunk() {
213: return chunk;
214: }
215:
216: /**
217: * Verifies that this chunk's saved text matches the corresponding text in
218: * the given sequence.
219: *
220: * @param target
221: * the sequence to verify against.
222: * @return true if the texts match.
223: */
224: public boolean verify(List target) {
225: if (chunk == null) {
226: return true;
227: }
228: if (last() > target.size()) {
229: return false;
230: }
231: for (int i = 0; i < count; i++) {
232: if (!target.get(anchor + i).equals(chunk.get(i))) {
233: return false;
234: }
235: }
236: return true;
237: }
238:
239: /**
240: * Delete this chunk from he given text.
241: *
242: * @param target
243: * the text to delete from.
244: */
245: public void applyDelete(List target) {
246: for (int i = last(); i >= first(); i--) {
247: target.remove(i);
248: }
249: }
250:
251: /**
252: * Add the text of this chunk to the target at the given position.
253: *
254: * @param start
255: * where to add the text.
256: * @param target
257: * the text to add to.
258: */
259: public void applyAdd(int start, List target) {
260: Iterator i = chunk.iterator();
261: while (i.hasNext()) {
262: target.add(start++, i.next());
263: }
264: }
265:
266: /**
267: * Provide a string image of the chunk using the an empty prefix and
268: * postfix.
269: */
270: public void toString(StringBuffer s) {
271: toString(s, "", "");
272: }
273:
274: /**
275: * Provide a string image of the chunk using the given prefix and postfix.
276: *
277: * @param s
278: * where the string image should be appended.
279: * @param prefix
280: * the text thatshould prefix each line.
281: * @param postfix
282: * the text that should end each line.
283: */
284: public StringBuffer toString(StringBuffer s, String prefix,
285: String postfix) {
286: if (chunk != null) {
287: Iterator i = chunk.iterator();
288: while (i.hasNext()) {
289: s.append(prefix);
290: s.append(i.next());
291: s.append(postfix);
292: }
293: }
294: return s;
295: }
296:
297: /**
298: * Retreives the specified part from a {@link List List}.
299: *
300: * @param seq
301: * the list to retreive a slice from.
302: * @param pos
303: * the start position.
304: * @param count
305: * the number of items in the slice.
306: * @return a {@link List List} containing the specified items.
307: */
308: public static List slice(List seq, int pos, int count) {
309: if (count <= 0) {
310: return new ArrayList(seq.subList(pos, pos));
311: } else {
312: return new ArrayList(seq.subList(pos, pos + count));
313: }
314: }
315:
316: /**
317: * Retrieves a slice from an {@link Object Object} array.
318: *
319: * @param seq
320: * the list to retreive a slice from.
321: * @param pos
322: * the start position.
323: * @param count
324: * the number of items in the slice.
325: * @return a {@link List List} containing the specified items.
326: */
327: public static List slice(Object[] seq, int pos, int count) {
328: return slice(Arrays.asList(seq), pos, count);
329: }
330:
331: /**
332: * Provide a string representation of the numeric range of this chunk.
333: */
334: public String rangeString() {
335: StringBuffer result = new StringBuffer();
336: rangeString(result);
337: return result.toString();
338: }
339:
340: /**
341: * Provide a string representation of the numeric range of this chunk.
342: *
343: * @param s
344: * where the string representation should be appended.
345: */
346: public void rangeString(StringBuffer s) {
347: rangeString(s, ",");
348: }
349:
350: /**
351: * Provide a string representation of the numeric range of this chunk.
352: *
353: * @param s
354: * where the string representation should be appended.
355: * @param separ
356: * what to use as line separator.
357: */
358: public void rangeString(StringBuffer s, String separ) {
359: if (size() <= 1) {
360: s.append(Integer.toString(rcsfrom()));
361: } else {
362: s.append(Integer.toString(rcsfrom()));
363: s.append(separ);
364: s.append(Integer.toString(rcsto()));
365: }
366: }
367: }
|