KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > builder > BufferRun


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.java.source.builder;
20
21 import org.netbeans.modules.java.source.save.SourceBuffer;
22
23 /**
24  * A single run of characters in a buffer designated by a start and an end position. The buffer is
25  * maintained outside of this class. Subclasses exist for specific types of runs.
26  */

27 public abstract class BufferRun {
28
29     int start;
30     int end;
31     Kind kind;
32
33     public enum Kind { TOKEN, COMMENT, WHITESPACE, LINE_ENDING }
34
35     /**
36      * @param start The starting char offset of the run, inclusive
37      * @param end The end char offset of the run, exclusive
38      */

39     protected BufferRun(int start, int end, Kind kind) {
40         this.start = start;
41         this.end = end;
42         this.kind = kind;
43     }
44
45     /**
46      * @return the starting char offset of the run, inclusive
47      */

48     public int getStart() {
49         return start;
50     }
51
52     /**
53      * @return the end char offset of the run, exclusive
54      */

55     public int getEnd() {
56         return end;
57     }
58     
59     public Kind getKind() {
60         return kind;
61     }
62
63     /*
64      * @see java.lang.Object#toString()
65      */

66     public String JavaDoc toString() {
67         return "BR(" + start + "," + end + ")";
68     }
69
70     /**
71      * Returns a new character array from a source buffer for a given BufferRun
72      * @param br
73      */

74     public char[] getChars(CharSequence JavaDoc sb) {
75         return sb.subSequence(getStart(), getEnd()).toString().toCharArray();
76     }
77     
78     /**
79      * Returns a new String from a source buffer for a given BufferRun
80      * @param br
81      */

82     public String JavaDoc getString(CharSequence JavaDoc sb) {
83         return new String JavaDoc(getChars(sb));
84     }
85 }
86
Popular Tags