1 11 12 package org.eclipse.jdt.internal.core.dom.rewrite; 13 14 import java.util.Arrays ; 15 import java.util.List ; 16 17 import org.eclipse.jdt.core.dom.LineComment; 18 import org.eclipse.jdt.core.formatter.IndentManipulation; 19 import org.eclipse.jdt.internal.compiler.util.Util; 20 21 public class LineCommentEndOffsets { 22 23 private int[] offsets; 24 private final List commentList; 25 26 public LineCommentEndOffsets(List commentList) { 27 this.commentList= commentList; 28 this.offsets= null; } 30 31 private int[] getOffsets() { 32 if (this.offsets == null) { 33 if (this.commentList != null) { 34 int nComments= this.commentList.size(); 35 int count= 0; 37 for (int i= 0; i < nComments; i++) { 38 Object curr= this.commentList.get(i); 39 if (curr instanceof LineComment) { 40 count++; 41 } 42 } 43 this.offsets= new int[count]; 45 for (int i= 0, k= 0; i < nComments; i++) { 46 Object curr= this.commentList.get(i); 47 if (curr instanceof LineComment) { 48 LineComment comment= (LineComment) curr; 49 this.offsets[k++]= comment.getStartPosition() + comment.getLength(); 50 } 51 } 52 } else { 53 this.offsets= Util.EMPTY_INT_ARRAY; 54 } 55 } 56 return this.offsets; 57 } 58 59 public boolean isEndOfLineComment(int offset) { 60 return offset >= 0 && Arrays.binarySearch(getOffsets(), offset) >= 0; 61 } 62 63 public boolean isEndOfLineComment(int offset, char[] content) { 64 if (offset < 0 || (offset < content.length && !IndentManipulation.isLineDelimiterChar(content[offset]))) { 65 return false; 66 } 67 return Arrays.binarySearch(getOffsets(), offset) >= 0; 68 } 69 70 public boolean remove(int offset) { 71 int[] offsetArray= getOffsets(); int index= Arrays.binarySearch(offsetArray, offset); 73 if (index >= 0) { 74 if (index > 0) { 75 System.arraycopy(offsetArray, 0, offsetArray, 1, index); 78 } 79 offsetArray[0]= -1; 80 return true; 81 } 82 return false; 83 } 84 85 } 86 | Popular Tags |