KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > dom > rewrite > LineCommentEndOffsets


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
12 package org.eclipse.jdt.internal.core.dom.rewrite;
13
14 import java.util.Arrays JavaDoc;
15 import java.util.List JavaDoc;
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 JavaDoc commentList;
25     
26     public LineCommentEndOffsets(List JavaDoc commentList) {
27         this.commentList= commentList;
28         this.offsets= null; // create on demand
29
}
30     
31     private int[] getOffsets() {
32         if (this.offsets == null) {
33             if (this.commentList != null) {
34                 int nComments= this.commentList.size();
35                 // count the number of line comments
36
int count= 0;
37                 for (int i= 0; i < nComments; i++) {
38                     Object JavaDoc curr= this.commentList.get(i);
39                     if (curr instanceof LineComment) {
40                         count++;
41                     }
42                 }
43                 // fill the offset table
44
this.offsets= new int[count];
45                 for (int i= 0, k= 0; i < nComments; i++) {
46                     Object JavaDoc 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(); // returns the shared array
72
int index= Arrays.binarySearch(offsetArray, offset);
73         if (index >= 0) {
74             if (index > 0) {
75                 // shift from the beginning and insert -1 (smallest number) at the beginning
76
// 1, 2, 3, x, 4, 5 -> -1, 1, 2, 3, 4, 5
77
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