1 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 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 41 public int getLength() { 42 return fLength; 43 } 44 45 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 63 public String toString(){ 64 return "<offset: " + fOffset +" length: " + fLength + "/>"; } 66 67 73 public static ISourceRange[] reverseSortByOffset(ISourceRange[] ranges){ 74 Comparator comparator= new Comparator (){ 75 public int compare(Object o1, Object o2){ 76 return ((ISourceRange)o2).getOffset() - ((ISourceRange)o1).getOffset(); 77 } 78 }; 79 Arrays.sort(ranges, comparator); 80 return ranges; 81 } 82 83 86 public boolean equals(Object obj) { 87 if (! (obj instanceof ISourceRange)) 88 return false; 89 return ((ISourceRange)obj).getOffset() == fOffset && ((ISourceRange)obj).getLength() == fLength; 90 } 91 92 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 115 public static boolean isAvailable(ISourceRange range) { 116 return range != null && range.getOffset() != -1; 117 } 118 } 119 120 | Popular Tags |