KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > diff > Difference


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.diff;
21
22 import java.io.Serializable JavaDoc;
23
24 /**
25  * This class represents a single difference between two files.
26  *
27  * @author Martin Entlicher
28  */

29 public class Difference extends Object JavaDoc implements Serializable JavaDoc {
30
31     /** Delete type of difference - a portion of a file was removed in the other */
32     public static final int DELETE = 0;
33
34     /** Add type of difference - a portion of a file was added in the other */
35     public static final int ADD = 1;
36
37     /** Change type of difference - a portion of a file was changed in the other */
38     public static final int CHANGE = 2;
39     
40     private int type = 0;
41     private int firstStart = 0;
42     private int firstEnd = 0;
43     private int secondStart = 0;
44     private int secondEnd = 0;
45     private Difference.Part[] firstLineDiffs;
46     private Difference.Part[] secondLineDiffs;
47     
48     /** The text of the difference in the first file. */
49     private String JavaDoc firstText;
50     /** The text of the difference in the second file. */
51     private String JavaDoc secondText;
52     
53     private static final long serialVersionUID = 7638201981188907148L;
54     
55     /**
56      * Creates a new instance of Difference
57      * @param type The type of the difference. Must be one of the <a HREF="#DELETE">DELETE</a>,
58      * <a HREF="#ADD">ADD</a> or <a HREF="#CHANGE">CHANGE</a>
59      * @param firstStart The line number on which the difference starts in the first file.
60      * @param firstEnd The line number on which the difference ends in the first file.
61      * @param secondStart The line number on which the difference starts in the second file.
62      * @param secondEnd The line number on which the difference ends in the second file.
63      */

64     public Difference(int type, int firstStart, int firstEnd, int secondStart, int secondEnd) {
65         this(type, firstStart, firstEnd, secondStart, secondEnd, null, null, null, null);
66     }
67     
68     /**
69      * Creates a new instance of Difference
70      * @param type The type of the difference. Must be one of the <a HREF="#DELETE">DELETE</a>,
71      * <a HREF="#ADD">ADD</a> or <a HREF="#CHANGE">CHANGE</a>
72      * @param firstStart The line number on which the difference starts in the first file.
73      * @param firstEnd The line number on which the difference ends in the first file.
74      * @param secondStart The line number on which the difference starts in the second file.
75      * @param secondEnd The line number on which the difference ends in the second file.
76      * @param firstText The text content of the difference in the first file.
77      * @param secondText The text content of the difference in the second file.
78      */

79     public Difference(int type, int firstStart, int firstEnd, int secondStart, int secondEnd,
80                       String JavaDoc firstText, String JavaDoc secondText) {
81         this(type, firstStart, firstEnd, secondStart, secondEnd, firstText, secondText, null, null);
82     }
83     
84     /**
85      * Creates a new instance of Difference
86      * @param type The type of the difference. Must be one of the <a HREF="#DELETE">DELETE</a>,
87      * <a HREF="#ADD">ADD</a> or <a HREF="#CHANGE">CHANGE</a>
88      * @param firstStart The line number on which the difference starts in the first file.
89      * @param firstEnd The line number on which the difference ends in the first file.
90      * @param secondStart The line number on which the difference starts in the second file.
91      * @param secondEnd The line number on which the difference ends in the second file.
92      * @param firstText The text content of the difference in the first file.
93      * @param secondText The text content of the difference in the second file.
94      * @param firstLineDiffs The list of differences on lines in the first file.
95      * The list contains instances of {@link Difference.Part}.
96      * Can be <code>null</code> when there are no line differences.
97      * @param secondLineDiffs The list of differences on lines in the second file.
98      * The list contains instances of {@link Difference.Part}.
99      * Can be <code>null</code> when there are no line differences.
100      */

101     public Difference(int type, int firstStart, int firstEnd, int secondStart, int secondEnd,
102                       String JavaDoc firstText, String JavaDoc secondText, Difference.Part[] firstLineDiffs, Difference.Part[] secondLineDiffs) {
103         if (type > 2 || type < 0) {
104             throw new IllegalArgumentException JavaDoc("Bad Difference type = "+type);
105         }
106         this.type = type;
107         this.firstStart = firstStart;
108         this.firstEnd = firstEnd;
109         this.secondStart = secondStart;
110         this.secondEnd = secondEnd;
111         this.firstText = firstText;
112         this.secondText = secondText;
113         this.firstLineDiffs = firstLineDiffs;
114         this.secondLineDiffs = secondLineDiffs;
115     }
116
117     /**
118      * Get the difference type. It's one of <a HREF="#DELETE">DELETE</a>,
119      * <a HREF="#ADD">ADD</a> or <a HREF="#CHANGE">CHANGE</a> meaning
120      * respective change in second source.
121      */

122     public int getType() {
123         return this.type;
124     }
125     
126     /**
127      * Get the line number on which the difference starts in the first file.
128      *
129      * <p>For ADD changes it returns previous line number e.g. 0 for add
130      * file start.
131      */

132     public int getFirstStart() {
133         return this.firstStart;
134     }
135
136     /**
137      * Get the line number on which the difference ends in the first file.
138      * <p>
139      * Does not have any meaning for ADD changes.
140      */

141     public int getFirstEnd() {
142         return this.firstEnd;
143     }
144     
145     /**
146      * Get the line number on which the difference starts in the second file.
147      */

148     public int getSecondStart() {
149         return this.secondStart;
150     }
151     
152     /**
153      * Get the line number on which the difference ends in the second file.
154      * <p>
155      * Does not have any meaning for DELETE changes.
156      */

157     public int getSecondEnd() {
158         return this.secondEnd;
159     }
160     
161     /**
162      * The list of differences on lines in the first file.
163      * The list contains instances of {@link Difference.Part}.
164      * Can be <code>null</code> when there are no line differences.
165      */

166     public Difference.Part[] getFirstLineDiffs() {
167         return firstLineDiffs;
168     }
169     
170     /**
171      * The list of differences on lines in the second file.
172      * The list contains instances of {@link Difference.Part}.
173      * Can be <code>null</code> when there are no line differences.
174      */

175     public Difference.Part[] getSecondLineDiffs() {
176         return secondLineDiffs;
177     }
178     
179     /**
180      * Get the text content of the difference in the first file.
181      */

182     public String JavaDoc getFirstText() {
183         return firstText;
184     }
185     
186     /**
187      * Get the text content of the difference in the second file.
188      */

189     public String JavaDoc getSecondText() {
190         return secondText;
191     }
192     
193     public String JavaDoc toString() {
194         return "Difference("+((type == ADD) ? "ADD" : (type == DELETE) ? "DELETE" : "CHANGE")+", "+
195                firstStart+", "+firstEnd+", "+secondStart+", "+secondEnd+")";
196     }
197     
198     /**
199      * This class represents a difference on a single line.
200      */

201     public static final class Part extends Object JavaDoc implements Serializable JavaDoc {
202         
203         private int type;
204         private int line;
205         private int pos1;
206         private int pos2;
207         private String JavaDoc text;
208         
209         private static final long serialVersionUID = 7638201981188907149L;
210     
211         /**
212           * Creates a new instance of LineDiff
213           * @param type The type of the difference. Must be one of the {<a HREF="#DELETE">DELETE</a>,
214           * <a HREF="#ADD">ADD</a> or <a HREF="#CHANGE">CHANGE</a>
215           * @param line The line number
216           * @param pos1 The position on which the difference starts on this line.
217           * @param pos2 The position on which the difference ends on this line.
218           */

219         public Part(int type, int line, int pos1, int pos2) {
220             if (type > 2 || type < 0) {
221                 throw new IllegalArgumentException JavaDoc("Bad Difference type = "+type);
222             }
223             this.type = type;
224             this.line = line;
225             this.pos1 = pos1;
226             this.pos2 = pos2;
227         }
228         
229         /**
230           * Get the difference type. It's one of <a HREF="#DELETE">DELETE</a>,
231           * <a HREF="#ADD">ADD</a> or <a HREF="#CHANGE">CHANGE</a>.
232           */

233         public int getType() {
234             return this.type;
235         }
236     
237         /**
238           * Get the line number.
239           */

240         public int getLine() {
241             return this.line;
242         }
243         
244         /**
245           * Get the position on which the difference starts on this line.
246           */

247         public int getStartPosition() {
248             return this.pos1;
249         }
250         
251         /**
252           * Get the position on which the difference ends on this line.
253           */

254         public int getEndPosition() {
255             return this.pos2;
256         }
257         
258         /**
259          * Set the text content of the difference.
260          *
261         public void setText(String text) {
262             this.text = text;
263         }
264         
265         /**
266          * Get the text content of the difference.
267          *
268         public String getText() {
269             return text;
270         }
271          */

272         
273     }
274     
275 }
276
Popular Tags