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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.corext;
13 import java.util.Arrays;
14 import java.util.Comparator;
16 import org.eclipse.jdt.core.ISourceRange;
17 import org.eclipse.jdt.core.compiler.IProblem;
18 import org.eclipse.jdt.core.dom.ASTNode;
22 * DO NOT REMOVE, used in a product.
23 * @deprecated As of 3.6, replaced by {@link org.eclipse.jdt.core.SourceRange}
25 public class SourceRange implements ISourceRange { // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=88265 (Allow implementation of ISourceRange)
27 private final int fOffset;
28 private final int fLength;
30 public SourceRange(int offset, int length){
35 public SourceRange(ASTNode node) {
36 this(node.getStartPosition(), node.getLength());
39 public SourceRange(IProblem problem) {
40 this(problem.getSourceStart(), problem.getSourceEnd() - problem.getSourceStart() + 1);
44 * @see ISourceRange#getLength()
46 public int getLength() {
51 * @see ISourceRange#getOffset()
53 public int getOffset() {
57 public int getEndExclusive() {
58 return getOffset() + getLength();
61 public int getEndInclusive() {
62 return getEndExclusive() - 1;
69 public String toString(){
70 return "<offset: " + fOffset +" length: " + fLength + "/>"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
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
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();
85 Arrays.sort(ranges, comparator);
90 * @see Object#equals(Object)
93 public boolean equals(Object obj) {
94 if (! (obj instanceof ISourceRange))
96 return ((ISourceRange)obj).getOffset() == fOffset && ((ISourceRange)obj).getLength() == fLength;
100 * @see Object#hashCode()
103 public int hashCode() {
104 return fLength ^ fOffset;
107 public boolean covers(ASTNode node) {
108 return covers(new SourceRange(node));
111 public boolean covers(SourceRange range) {
112 return getOffset() <= range.getOffset()
113 && getEndInclusive() >= range.getEndInclusive();
117 * Workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=130161
118 * (Java Model returns ISourceRanges [-1, 0] if source not available).
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
123 public static boolean isAvailable(ISourceRange range) {
124 return range != null && range.getOffset() != -1;