KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > texteditor > quickdiff > compare > equivalence > DocEquivalenceComparator


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.ui.internal.texteditor.quickdiff.compare.equivalence;
12
13 import java.util.ConcurrentModificationException JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16
17 import org.eclipse.jface.text.source.ILineRange;
18
19 import org.eclipse.ui.internal.texteditor.quickdiff.compare.rangedifferencer.IRangeComparator;
20
21 /**
22  * Implements the <code>IRangeComparator</code> interface for lines in
23  * a document. A <code>DocEquivalenceComparator</code> is used as the
24  * input for the <code>RangeDifferencer</code> engine to perform a
25  * line oriented compare on documents.
26  * <p>
27  * A <code>DocEquivalenceComparator</code> uses a
28  * <code>DocumentEquivalenceClass</code> to compare ranges.
29  * </p>
30  */

31 public final class DocEquivalenceComparator implements IRangeComparator {
32
33     private final DocumentEquivalenceClass fEquivalenceClass;
34     private final int fLineOffset;
35     private final int fLines;
36
37     private boolean fSkip= false;
38     
39     public DocEquivalenceComparator(DocumentEquivalenceClass equivalenceClass, ILineRange range) {
40         fEquivalenceClass= equivalenceClass;
41         if (range == null) {
42             fLineOffset= 0;
43             fLines= fEquivalenceClass.getCount();
44         } else {
45             fLineOffset= range.getStartLine();
46             fLines= range.getNumberOfLines();
47             Assert.isTrue(fLineOffset >= 0);
48             Assert.isTrue(fLineOffset + fLines <= fEquivalenceClass.getCount());
49         }
50     }
51
52     /**
53      * Returns the number of lines in the document.
54      *
55      * @return number of lines
56      */

57     public int getRangeCount() {
58         return fLines;
59     }
60
61     /**
62      * Returns <code>true</code> if a line given by the first index
63      * matches a line specified by the other <code>IRangeComparator</code> and index.
64      *
65      * @param thisIndex the number of the line within this range comparator
66      * @param other the range comparator to compare this with
67      * @param otherIndex the number of the line within the other comparator
68      * @return <code>true</code> if the lines are equal
69      */

70     public boolean rangesEqual(int thisIndex, IRangeComparator other, int otherIndex) {
71         if (other instanceof DocEquivalenceComparator) {
72             DocEquivalenceComparator dec= (DocEquivalenceComparator) other;
73             try {
74                 Hash ourHash= getHash(thisIndex);
75                 Hash otherHash= dec.getHash(otherIndex);
76                 return ourHash.equals(otherHash);
77             } catch (ConcurrentModificationException JavaDoc e) {
78                 fSkip= true;
79             } catch (IndexOutOfBoundsException JavaDoc e) {
80                 fSkip= true;
81             }
82         }
83         return false;
84     }
85     
86     Hash getHash(int index) {
87         return fEquivalenceClass.getHash(fLineOffset + index);
88     }
89
90     /**
91      * Aborts the comparison if the number of tokens is too large.
92      *
93      * @param length the current edit distance
94      * @param max the maximal edit distance
95      * @param other the comparator with which to compare
96      * @return <code>true</code> to abort a token comparison
97      */

98     public boolean skipRangeComparison(int length, int max, IRangeComparator other) {
99         return fSkip;
100     }
101 }
102
103
Popular Tags