KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > text > revisions > HunkComputer


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.internal.text.revisions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.jface.text.source.ILineDiffInfo;
17 import org.eclipse.jface.text.source.ILineDiffer;
18
19
20 /**
21  * Computes the diff hunks from an {@link ILineDiffer}.
22  *
23  * @since 3.3
24  */

25 public final class HunkComputer {
26     /**
27      * Converts the line-based information of {@link ILineDiffer} into {@link Hunk}s, grouping
28      * contiguous blocks of lines that are changed (added, deleted).
29      *
30      * @param differ the line differ to query
31      * @param lines the number of lines to query
32      * @return the corresponding {@link Hunk} information
33      */

34     public static Hunk[] computeHunks(ILineDiffer differ, int lines) {
35         List JavaDoc hunks= new ArrayList JavaDoc(lines);
36
37         int added= 0;
38         int changed= 0;
39         ILineDiffInfo info= null;
40         for (int line= 0; line < lines; line++) {
41             info= differ.getLineInfo(line);
42             if (info == null)
43                 continue;
44
45             int changeType= info.getChangeType();
46             switch (changeType) {
47                 case ILineDiffInfo.ADDED:
48                     added++;
49                     continue;
50                 case ILineDiffInfo.CHANGED:
51                     changed++;
52                     continue;
53                 case ILineDiffInfo.UNCHANGED:
54                     added -= info.getRemovedLinesAbove();
55                     if (added != 0 || changed != 0) {
56                         hunks.add(new Hunk(line - changed - Math.max(0, added), added, changed));
57                         added= 0;
58                         changed= 0;
59                     }
60             }
61         }
62
63         // last hunk
64
if (info != null) {
65             added -= info.getRemovedLinesBelow();
66             if (added != 0 || changed != 0) {
67                 hunks.add(new Hunk(lines - changed, added, changed));
68                 added= 0;
69                 changed= 0;
70             }
71         }
72         
73         return (Hunk[]) hunks.toArray(new Hunk[hunks.size()]);
74     }
75     private HunkComputer() {
76     }
77 }
Popular Tags