KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > save > 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 package org.netbeans.modules.java.source.save;
20
21 /**
22  * Represents a difference, as used in <code>Diff</code>. A difference consists
23  * of two pairs of starting and ending points, each pair representing either the
24  * "from" or the "to" collection passed to <code>Diff</code>. If an ending point
25  * is -1, then the difference was either a deletion or an addition. For example,
26  * if <code>getDeletedEnd()</code> returns -1, then the difference represents an
27  * addition.
28  */

29 class Difference {
30     public static final int NONE = -1;
31     
32     /**
33      * The point at which the deletion starts.
34      */

35     private int delStart = NONE;
36     
37     /**
38      * The point at which the deletion ends.
39      */

40     private int delEnd = NONE;
41     
42     /**
43      * The point at which the addition starts.
44      */

45     private int addStart = NONE;
46     
47     /**
48      * The point at which the addition ends.
49      */

50     private int addEnd = NONE;
51     
52     /**
53      * Creates the difference for the given start and end points for the
54      * deletion and addition.
55      */

56     public Difference(int delStart, int delEnd, int addStart, int addEnd) {
57         this.delStart = delStart;
58         this.delEnd = delEnd;
59         this.addStart = addStart;
60         this.addEnd = addEnd;
61     }
62     
63     /**
64      * The point at which the deletion starts, if any. A value equal to
65      * <code>NONE</code> means this is an addition.
66      */

67     public int getDeletedStart() {
68         return delStart;
69     }
70     
71     /**
72      * The point at which the deletion ends, if any. A value equal to
73      * <code>NONE</code> means this is an addition.
74      */

75     public int getDeletedEnd() {
76         return delEnd;
77     }
78     
79     /**
80      * The point at which the addition starts, if any. A value equal to
81      * <code>NONE</code> means this must be an addition.
82      */

83     public int getAddedStart() {
84         return addStart;
85     }
86     
87     /**
88      * The point at which the addition ends, if any. A value equal to
89      * <code>NONE</code> means this must be an addition.
90      */

91     public int getAddedEnd() {
92         return addEnd;
93     }
94     
95     /**
96      * Sets the point as deleted. The start and end points will be modified to
97      * include the given line.
98      */

99     public void setDeleted(int line) {
100         delStart = Math.min(line, delStart);
101         delEnd = Math.max(line, delEnd);
102     }
103     
104     /**
105      * Sets the point as added. The start and end points will be modified to
106      * include the given line.
107      */

108     public void setAdded(int line) {
109         addStart = Math.min(line, addStart);
110         addEnd = Math.max(line, addEnd);
111     }
112     
113     /**
114      * Compares this object to the other for equality. Both objects must be of
115      * type Difference, with the same starting and ending points.
116      */

117     public boolean equals(Object JavaDoc obj) {
118         if (obj instanceof Difference) {
119             Difference other = (Difference)obj;
120             
121             return (delStart == other.delStart &&
122                     delEnd == other.delEnd &&
123                     addStart == other.addStart &&
124                     addEnd == other.addEnd);
125         } else {
126             return false;
127         }
128     }
129     
130     /**
131      * Returns a string representation of this difference.
132      */

133     public String JavaDoc toString() {
134         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
135         buf.append("del: [" + delStart + ", " + delEnd + "]");
136         buf.append(" ");
137         buf.append("add: [" + addStart + ", " + addEnd + "]");
138         return buf.toString();
139     }
140     
141 }
142
Popular Tags