KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > typeconstraints > CompilationUnitRange


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.jdt.internal.corext.refactoring.typeconstraints;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.jdt.core.ICompilationUnit;
16 import org.eclipse.jdt.core.ISourceRange;
17 import org.eclipse.jdt.core.dom.ASTNode;
18 import org.eclipse.jdt.core.dom.CompilationUnit;
19
20 import org.eclipse.jdt.internal.corext.SourceRange;
21 import org.eclipse.jdt.internal.corext.dom.NodeFinder;
22
23 public final class CompilationUnitRange {
24
25     private final ICompilationUnit fCompilationUnit;
26     private final ISourceRange fSourceRange;
27
28     public CompilationUnitRange(ICompilationUnit unit, ISourceRange range) {
29         Assert.isNotNull(unit);
30         Assert.isNotNull(range);
31         fCompilationUnit= unit;
32         fSourceRange= range;
33     }
34
35     public CompilationUnitRange(ICompilationUnit unit, ASTNode node) {
36         this(unit, new SourceRange(node));
37     }
38
39     public ICompilationUnit getCompilationUnit() {
40         return fCompilationUnit;
41     }
42
43     public ISourceRange getSourceRange() {
44         return fSourceRange;
45     }
46
47     //rootNode must be the ast root for fCompilationUnit
48
public ASTNode getNode(CompilationUnit rootNode) {
49         NodeFinder finder= new NodeFinder(fSourceRange.getOffset(), fSourceRange.getLength());
50         rootNode.accept(finder);
51         ASTNode result= finder.getCoveringNode();
52         if (result != null)
53             return result;
54         return finder.getCoveredNode();
55     }
56
57     /* (non-Javadoc)
58      * @see java.lang.Object#toString()
59      */

60     public String JavaDoc toString() {
61         return "(" + fSourceRange.toString() + " in " + fCompilationUnit.getElementName() + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
62
}
63
64     public boolean equals(Object JavaDoc obj) {
65         if (this == obj)
66             return true;
67         if (!(obj instanceof CompilationUnitRange))
68             return false;
69         CompilationUnitRange other= (CompilationUnitRange)obj;
70         return fCompilationUnit.equals(other.fCompilationUnit) && fSourceRange.equals(other.fSourceRange);
71     }
72
73     public int hashCode() {
74         return (37 * fCompilationUnit.hashCode()) ^ fSourceRange.hashCode();
75     }
76 }
77
Popular Tags