KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > SourceRange


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;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.Comparator JavaDoc;
15
16 import org.eclipse.jdt.core.ISourceRange;
17 import org.eclipse.jdt.core.compiler.IProblem;
18 import org.eclipse.jdt.core.dom.ASTNode;
19
20 public class SourceRange implements ISourceRange{
21     
22     private final int fOffset;
23     private final int fLength;
24
25     public SourceRange(int offset, int length){
26         fLength= length;
27         fOffset= offset;
28     }
29     
30     public SourceRange(ASTNode node) {
31         this(node.getStartPosition(), node.getLength());
32     }
33
34     public SourceRange(IProblem problem) {
35         this(problem.getSourceStart(), problem.getSourceEnd() - problem.getSourceStart() + 1);
36     }
37     
38     /*
39      * @see ISourceRange#getLength()
40      */

41     public int getLength() {
42         return fLength;
43     }
44
45     /*
46      * @see ISourceRange#getOffset()
47      */

48     public int getOffset() {
49         return fOffset;
50     }
51     
52     public int getEndExclusive() {
53         return getOffset() + getLength();
54     }
55     
56     public int getEndInclusive() {
57         return getEndExclusive() - 1;
58     }
59     
60     /*non java doc
61      * for debugging only
62      */

63     public String JavaDoc toString(){
64         return "<offset: " + fOffset +" length: " + fLength + "/>"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
65
}
66     
67     /**
68      * Sorts the given ranges by offset (backwards).
69      * Note: modifies the parameter.
70      * @param ranges the ranges to sort
71      * @return the sorted ranges, which are identical to the parameter ranges
72      */

73     public static ISourceRange[] reverseSortByOffset(ISourceRange[] ranges){
74         Comparator JavaDoc comparator= new Comparator JavaDoc(){
75             public int compare(Object JavaDoc o1, Object JavaDoc o2){
76                 return ((ISourceRange)o2).getOffset() - ((ISourceRange)o1).getOffset();
77             }
78         };
79         Arrays.sort(ranges, comparator);
80         return ranges;
81     }
82
83     /*
84      * @see Object#equals(Object)
85      */

86     public boolean equals(Object JavaDoc obj) {
87         if (! (obj instanceof ISourceRange))
88             return false;
89         return ((ISourceRange)obj).getOffset() == fOffset && ((ISourceRange)obj).getLength() == fLength;
90     }
91
92     /*
93      * @see Object#hashCode()
94      */

95     public int hashCode() {
96         return fLength ^ fOffset;
97     }
98     
99     public boolean covers(ASTNode node) {
100         return covers(new SourceRange(node));
101     }
102     
103     public boolean covers(SourceRange range) {
104         return getOffset() <= range.getOffset()
105                 && getEndInclusive() >= range.getEndInclusive();
106     }
107     
108     /**
109      * Workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=130161
110      * (Java Model returns ISourceRanges [-1, 0] if source not available).
111      *
112      * @param range a source range, can be <code>null</code>
113      * @return <code>true</code> iff range is not null and range.getOffset() is not -1
114      */

115     public static boolean isAvailable(ISourceRange range) {
116             return range != null && range.getOffset() != -1;
117     }
118 }
119
120
Popular Tags