]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/internal compatibility/org/eclipse/jdt/internal/corext/SourceRange.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / internal compatibility / org / eclipse / jdt / internal / corext / SourceRange.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 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;
14 import java.util.Comparator;
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
21 /**
22  * DO NOT REMOVE, used in a product.
23  * @deprecated As of 3.6, replaced by {@link org.eclipse.jdt.core.SourceRange}
24  */
25 public class SourceRange implements ISourceRange { // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=88265 (Allow implementation of ISourceRange)
26
27         private final int fOffset;
28         private final int fLength;
29
30         public SourceRange(int offset, int length){
31                 fLength= length;
32                 fOffset= offset;
33         }
34
35         public SourceRange(ASTNode node) {
36                 this(node.getStartPosition(), node.getLength());
37         }
38
39         public SourceRange(IProblem problem) {
40                 this(problem.getSourceStart(), problem.getSourceEnd() - problem.getSourceStart() + 1);
41         }
42
43         /*
44          * @see ISourceRange#getLength()
45          */
46         public int getLength() {
47                 return fLength;
48         }
49
50         /*
51          * @see ISourceRange#getOffset()
52          */
53         public int getOffset() {
54                 return fOffset;
55         }
56
57         public int getEndExclusive() {
58                 return getOffset() + getLength();
59         }
60
61         public int getEndInclusive() {
62                 return getEndExclusive() - 1;
63         }
64
65         /*non java doc
66          * for debugging only
67          */
68         @Override
69         public String toString(){
70                 return "<offset: " + fOffset +" length: " + fLength + "/>"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
71         }
72
73         /**
74          * Sorts the given ranges by offset (backwards).
75          * Note: modifies the parameter.
76          * @param ranges the ranges to sort
77          * @return the sorted ranges, which are identical to the parameter ranges
78          */
79         public static ISourceRange[] reverseSortByOffset(ISourceRange[] ranges){
80                 Comparator<ISourceRange> comparator= new Comparator<ISourceRange>(){
81                         public int compare(ISourceRange o1, ISourceRange o2){
82                                 return o2.getOffset() - o1.getOffset();
83                         }
84                 };
85                 Arrays.sort(ranges, comparator);
86                 return ranges;
87         }
88
89     /*
90      * @see Object#equals(Object)
91      */
92     @Override
93         public boolean equals(Object obj) {
94         if (! (obj instanceof ISourceRange))
95                 return false;
96             return ((ISourceRange)obj).getOffset() == fOffset && ((ISourceRange)obj).getLength() == fLength;
97     }
98
99     /*
100      * @see Object#hashCode()
101      */
102     @Override
103         public int hashCode() {
104         return fLength ^ fOffset;
105     }
106
107     public boolean covers(ASTNode node) {
108         return covers(new SourceRange(node));
109     }
110
111     public boolean covers(SourceRange range) {
112         return    getOffset() <= range.getOffset()
113                 && getEndInclusive() >= range.getEndInclusive();
114     }
115
116     /**
117      * Workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=130161
118      * (Java Model returns ISourceRanges [-1, 0] if source not available).
119      *
120      * @param range a source range, can be <code>null</code>
121      * @return <code>true</code> iff range is not null and range.getOffset() is not -1
122      */
123     public static boolean isAvailable(ISourceRange range) {
124                 return range != null && range.getOffset() != -1;
125     }
126 }
127