001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.api.diff;
043:
044: import java.io.Serializable;
045:
046: /**
047: * This class represents a single difference between two files.
048: *
049: * @author Martin Entlicher
050: */
051: public class Difference extends Object implements Serializable {
052:
053: /** Delete type of difference - a portion of a file was removed in the other */
054: public static final int DELETE = 0;
055:
056: /** Add type of difference - a portion of a file was added in the other */
057: public static final int ADD = 1;
058:
059: /** Change type of difference - a portion of a file was changed in the other */
060: public static final int CHANGE = 2;
061:
062: private int type = 0;
063: private int firstStart = 0;
064: private int firstEnd = 0;
065: private int secondStart = 0;
066: private int secondEnd = 0;
067: private Difference.Part[] firstLineDiffs;
068: private Difference.Part[] secondLineDiffs;
069:
070: /** The text of the difference in the first file. */
071: private String firstText;
072: /** The text of the difference in the second file. */
073: private String secondText;
074:
075: private static final long serialVersionUID = 7638201981188907148L;
076:
077: /**
078: * Creates a new instance of Difference
079: * @param type The type of the difference. Must be one of the <a href="#DELETE">DELETE</a>,
080: * <a href="#ADD">ADD</a> or <a href="#CHANGE">CHANGE</a>
081: * @param firstStart The line number on which the difference starts in the first file.
082: * @param firstEnd The line number on which the difference ends in the first file.
083: * @param secondStart The line number on which the difference starts in the second file.
084: * @param secondEnd The line number on which the difference ends in the second file.
085: */
086: public Difference(int type, int firstStart, int firstEnd,
087: int secondStart, int secondEnd) {
088: this (type, firstStart, firstEnd, secondStart, secondEnd, null,
089: null, null, null);
090: }
091:
092: /**
093: * Creates a new instance of Difference
094: * @param type The type of the difference. Must be one of the <a href="#DELETE">DELETE</a>,
095: * <a href="#ADD">ADD</a> or <a href="#CHANGE">CHANGE</a>
096: * @param firstStart The line number on which the difference starts in the first file.
097: * @param firstEnd The line number on which the difference ends in the first file.
098: * @param secondStart The line number on which the difference starts in the second file.
099: * @param secondEnd The line number on which the difference ends in the second file.
100: * @param firstText The text content of the difference in the first file.
101: * @param secondText The text content of the difference in the second file.
102: */
103: public Difference(int type, int firstStart, int firstEnd,
104: int secondStart, int secondEnd, String firstText,
105: String secondText) {
106: this (type, firstStart, firstEnd, secondStart, secondEnd,
107: firstText, secondText, null, null);
108: }
109:
110: /**
111: * Creates a new instance of Difference
112: * @param type The type of the difference. Must be one of the <a href="#DELETE">DELETE</a>,
113: * <a href="#ADD">ADD</a> or <a href="#CHANGE">CHANGE</a>
114: * @param firstStart The line number on which the difference starts in the first file.
115: * @param firstEnd The line number on which the difference ends in the first file.
116: * @param secondStart The line number on which the difference starts in the second file.
117: * @param secondEnd The line number on which the difference ends in the second file.
118: * @param firstText The text content of the difference in the first file.
119: * @param secondText The text content of the difference in the second file.
120: * @param firstLineDiffs The list of differences on lines in the first file.
121: * The list contains instances of {@link Difference.Part}.
122: * Can be <code>null</code> when there are no line differences.
123: * @param secondLineDiffs The list of differences on lines in the second file.
124: * The list contains instances of {@link Difference.Part}.
125: * Can be <code>null</code> when there are no line differences.
126: */
127: public Difference(int type, int firstStart, int firstEnd,
128: int secondStart, int secondEnd, String firstText,
129: String secondText, Difference.Part[] firstLineDiffs,
130: Difference.Part[] secondLineDiffs) {
131: if (type > 2 || type < 0) {
132: throw new IllegalArgumentException("Bad Difference type = "
133: + type);
134: }
135: this .type = type;
136: this .firstStart = firstStart;
137: this .firstEnd = firstEnd;
138: this .secondStart = secondStart;
139: this .secondEnd = secondEnd;
140: this .firstText = firstText;
141: this .secondText = secondText;
142: this .firstLineDiffs = firstLineDiffs;
143: this .secondLineDiffs = secondLineDiffs;
144: }
145:
146: /**
147: * Get the difference type. It's one of <a href="#DELETE">DELETE</a>,
148: * <a href="#ADD">ADD</a> or <a href="#CHANGE">CHANGE</a> meaning
149: * respective change in second source.
150: */
151: public int getType() {
152: return this .type;
153: }
154:
155: /**
156: * Get the line number on which the difference starts in the first file.
157: *
158: * <p>For ADD changes it returns previous line number e.g. 0 for add
159: * file start.
160: */
161: public int getFirstStart() {
162: return this .firstStart;
163: }
164:
165: /**
166: * Get the line number on which the difference ends in the first file.
167: * <p>
168: * Does not have any meaning for ADD changes.
169: */
170: public int getFirstEnd() {
171: return this .firstEnd;
172: }
173:
174: /**
175: * Get the line number on which the difference starts in the second file.
176: */
177: public int getSecondStart() {
178: return this .secondStart;
179: }
180:
181: /**
182: * Get the line number on which the difference ends in the second file.
183: * <p>
184: * Does not have any meaning for DELETE changes.
185: */
186: public int getSecondEnd() {
187: return this .secondEnd;
188: }
189:
190: /**
191: * The list of differences on lines in the first file.
192: * The list contains instances of {@link Difference.Part}.
193: * Can be <code>null</code> when there are no line differences.
194: */
195: public Difference.Part[] getFirstLineDiffs() {
196: return firstLineDiffs;
197: }
198:
199: /**
200: * The list of differences on lines in the second file.
201: * The list contains instances of {@link Difference.Part}.
202: * Can be <code>null</code> when there are no line differences.
203: */
204: public Difference.Part[] getSecondLineDiffs() {
205: return secondLineDiffs;
206: }
207:
208: /**
209: * Get the text content of the difference in the first file.
210: */
211: public String getFirstText() {
212: return firstText;
213: }
214:
215: /**
216: * Get the text content of the difference in the second file.
217: */
218: public String getSecondText() {
219: return secondText;
220: }
221:
222: public String toString() {
223: return "Difference("
224: + ((type == ADD) ? "ADD" : (type == DELETE) ? "DELETE"
225: : "CHANGE") + ", " + firstStart + ", "
226: + firstEnd + ", " + secondStart + ", " + secondEnd
227: + ")";
228: }
229:
230: /**
231: * This class represents a difference on a single line.
232: */
233: public static final class Part extends Object implements
234: Serializable {
235:
236: private int type;
237: private int line;
238: private int pos1;
239: private int pos2;
240: private String text;
241:
242: private static final long serialVersionUID = 7638201981188907149L;
243:
244: /**
245: * Creates a new instance of LineDiff
246: * @param type The type of the difference. Must be one of the {<a href="#DELETE">DELETE</a>,
247: * <a href="#ADD">ADD</a> or <a href="#CHANGE">CHANGE</a>
248: * @param line The line number
249: * @param pos1 The position on which the difference starts on this line.
250: * @param pos2 The position on which the difference ends on this line.
251: */
252: public Part(int type, int line, int pos1, int pos2) {
253: if (type > 2 || type < 0) {
254: throw new IllegalArgumentException(
255: "Bad Difference type = " + type);
256: }
257: this .type = type;
258: this .line = line;
259: this .pos1 = pos1;
260: this .pos2 = pos2;
261: }
262:
263: /**
264: * Get the difference type. It's one of <a href="#DELETE">DELETE</a>,
265: * <a href="#ADD">ADD</a> or <a href="#CHANGE">CHANGE</a>.
266: */
267: public int getType() {
268: return this .type;
269: }
270:
271: /**
272: * Get the line number.
273: */
274: public int getLine() {
275: return this .line;
276: }
277:
278: /**
279: * Get the position on which the difference starts on this line.
280: */
281: public int getStartPosition() {
282: return this .pos1;
283: }
284:
285: /**
286: * Get the position on which the difference ends on this line.
287: */
288: public int getEndPosition() {
289: return this .pos2;
290: }
291:
292: /**
293: * Set the text content of the difference.
294: *
295: public void setText(String text) {
296: this.text = text;
297: }
298:
299: /**
300: * Get the text content of the difference.
301: *
302: public String getText() {
303: return text;
304: }
305: */
306:
307: }
308:
309: }
|