001: /*
002: * Copyright (C) 2001, 2002 Robert MacGrogan
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: *
019: * $Archive: SourceJammer$
020: * $FileName: Hunk3.java$
021: * $FileID: 4295$
022: *
023: * Last change:
024: * $AuthorName: Rob MacGrogan$
025: * $Date: 4/23/03 5:23 PM$
026: * $Comment: Replaced GPL header with LGPL header.$
027: */
028:
029: package JLibDiff;
030:
031: import java.io.*;
032: import java.util.*;
033:
034: /**
035: * @author $AuthorName: Rob MacGrogan$
036: * @version $VerNum: 2$
037: * $KeyWordsOff: $<br><br>
038: *
039: *
040: * The <code>Hunk3</code> class represents a bloc of difference of three
041: * files or three Buffers.
042: */
043: public class Hunk3 {
044:
045: DiffType diff;
046: int range[][] = new int[3][2];
047: private Hunk3Type type = null;
048: private ArrayList file0Lines = null;
049: private ArrayList file1Lines = null;
050: private ArrayList file2Lines = null;
051:
052: Vector a = new Vector();
053: Vector b = new Vector();
054: Vector c = new Vector();
055:
056: /**
057: * Allocates a new Hunk3.
058: */
059: public Hunk3() {
060: }
061:
062: /**
063: * Set range of difference which consern this bloc in etch file by values
064: * passed in argument.
065: *
066: * @param low0 start position of bloc of difference in the first file.
067: * @param high0 end position of bloc ob difference in thefirst file.
068: * @param low1 start position of bloc of difference in the second file.
069: * @param high1 end position of bloc ob difference in the second file.
070: * @param lowc start position of bloc of difference in the therd file.
071: * @param highc end position of bloc ob difference in the therd file.
072: */
073: public void setRange(int low0, int high0, int low1, int high1,
074: int lowc, int highc) {
075: range[0][0] = low0;
076: range[0][1] = high0;
077: range[1][0] = low1;
078: range[1][1] = high1;
079: range[2][0] = lowc;
080: range[2][1] = highc;
081: }
082:
083: /**
084: * Returns a string representation of the current Hunk with normal format.
085: */
086: public String convert() {
087: int i;
088: int dontprint = 0;
089: int oddoneout;
090: System.out.print("----------" + diff.code() + "\n");
091: String s = new String("====");
092: switch (diff.code()) {
093: case 5:
094: dontprint = 3;
095: oddoneout = 3;
096: s = s.concat("\n");
097: break;
098: case 6:
099: case 7:
100: case 8:
101: oddoneout = (int) diff.code() - 6;
102: if (oddoneout == 0)
103: dontprint = 1;
104: else
105: dontprint = 0;
106: s = s.concat((+oddoneout + 1) + "\n");
107: break;
108: default:
109: break;
110: }
111: for (i = 0; i < 3; i++) {
112: int lowt = this .lowLine(i), hight = this .highLine(i);
113: s = s.concat(+(i + 1) + ":");
114: switch (lowt - hight) {
115: case 1:
116: s = s.concat(+(lowt - 1) + "a\n");
117: break;
118: case 0:
119: s = s.concat(+lowt + "c\n");
120: break;
121: default:
122: s = s.concat(+lowt + "," + hight + "c\n");
123: break;
124: }
125: if (i == dontprint)
126: continue;
127: if (lowt <= hight) {
128:
129: }
130: }
131: return s;
132: }
133:
134: /**
135: * Returns the value of position of bloc start line reliding the
136: * file passed in argument.
137: *
138: * @param filenum file number which can be:
139: * 0 to indicate first file.
140: * 1 to indicate second file.
141: * 2 to indicate therd file.
142: */
143: public int lowLine(int filenum) {
144: return range[filenum][0];
145: }
146:
147: /**
148: * Returns the value of position of bloc end line reliding the
149: * file passed in argument.
150: *
151: * @param filenum file number which can be:
152: * 0 to indicate first file.
153: * 1 to indicate second file.
154: * 2 to indicate therd file.
155: */
156: public int highLine(int filenum) {
157: return range[filenum][1];
158: }
159:
160: /**
161: * Returns the number of lines reliding this bloc of difference and the
162: * file passed in argument.
163: *
164: * @param filenum file number which can be:
165: * 0 to indicate first file.
166: * 1 to indicate second file.
167: * 2 to indicate therd file.
168: */
169: public int numLines(int filenum) {
170: return (range[filenum][1] - range[filenum][0] + 1);
171: }
172:
173: public void accpet(Hunk3Visitor visitor) {
174: visitor.visitHunk3(this );
175: }
176:
177: /**
178: * Returns the maximum range for this hunk3. That is, the greatest
179: * difference between high line and low line in a single file.
180: */
181: public int getMaxRange() {
182: int max = -1;
183:
184: int range0 = numLines(0);
185: int range1 = numLines(1);
186: int range2 = numLines(2);
187:
188: if (range0 > range1) {
189: max = range0;
190: } else {
191: max = range1;
192: }
193: if (range2 > max) {
194: max = range2;
195: }
196: return max;
197: }
198:
199: /**
200: * Returns the file0Lines.
201: * @return ArrayList
202: */
203: public ArrayList getFile0Lines() {
204: return file0Lines;
205: }
206:
207: /**
208: * Returns the file1Lines.
209: * @return ArrayList
210: */
211: public ArrayList getFile1Lines() {
212: return file1Lines;
213: }
214:
215: /**
216: * Returns the file2Lines.
217: * @return ArrayList
218: */
219: public ArrayList getFile2Lines() {
220: return file2Lines;
221: }
222:
223: /**
224: * Sets the file0Lines.
225: * @param file0Lines The file0Lines to set
226: */
227: public void setFile0Lines(ArrayList file0Lines) {
228: this .file0Lines = file0Lines;
229: }
230:
231: /**
232: * Sets the file1Lines.
233: * @param file1Lines The file1Lines to set
234: */
235: public void setFile1Lines(ArrayList file1Lines) {
236: this .file1Lines = file1Lines;
237: }
238:
239: /**
240: * Sets the file2Lines.
241: * @param file2Lines The file2Lines to set
242: */
243: public void setFile2Lines(ArrayList file2Lines) {
244: this .file2Lines = file2Lines;
245: }
246:
247: /**
248: * Returns the type.
249: * @return Hunk3Type
250: */
251: public Hunk3Type getType() {
252: return type;
253: }
254:
255: /**
256: * Sets the type.
257: * @param type The type to set
258: */
259: public void setType(Hunk3Type type) {
260: this.type = type;
261: }
262:
263: }
|