KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > Comment


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
20 package org.netbeans.api.java.source;
21
22 import org.netbeans.api.java.source.query.Query;
23
24 /**
25  * An individual comment, consisting of a style, begin and end source
26  * file position, the indention (column) of its first character, and its text.
27  */

28 public final class Comment {
29     private Style style;
30     private int pos;
31     private int endPos;
32     private int indent;
33     private String JavaDoc text;
34
35     /**
36      * The set of different comment types.
37      */

38     public enum Style {
39         /**
40          * A line (double-slash) comment.
41          */

42         LINE,
43         
44         /**
45          * A block comment.
46          */

47         BLOCK,
48         
49         /**
50          * A JavaDoc comment.
51          */

52         JAVADOC,
53     }
54
55     /**
56      * Define a new block comment from a string. This comment does not
57      * have source file positions.
58      */

59     public static Comment create(String JavaDoc s) {
60         return new Comment(Style.BLOCK, Query.NOPOS, Query.NOPOS, Query.NOPOS, s);
61     }
62     
63     public static Comment create(Style style, int pos, int endPos, int indent, String JavaDoc text) {
64         return new Comment(style, pos, endPos, indent, text);
65     }
66     
67     /**
68      * Define a comment, using source file positions.
69      */

70     private Comment(Style style, int pos, int endPos, int indent, String JavaDoc text) {
71         this.style = style;
72         this.pos = pos;
73         this.endPos = endPos;
74         this.indent = indent;
75         this.text = text;
76     }
77     
78     public Style style() {
79         return style;
80     }
81
82     /**
83      * The start position in the source file, or Query.NOPOS if the
84      * comment was added by a translation operation.
85      */

86     public int pos() {
87         return pos;
88     }
89
90     /**
91      * The end position in the source file, or Query.NOPOS if the
92      * comment was added by a translation operation.
93      */

94     public int endPos() {
95         return endPos;
96     }
97
98     /**
99      * Returns the line indention for this comment, or Query.NOPOS if the
100      * comment was added by a translation operation.
101      */

102     public int indent() {
103         return indent;
104     }
105     
106     /** Returns true if this is a JavaDoc comment. */
107     public boolean isDocComment() {
108         return style == Style.JAVADOC;
109     }
110
111     /**
112      * Returns the comment text.
113      */

114     public String JavaDoc getText() {
115         return text;
116     }
117     
118     public boolean isNew() {
119         return pos == Query.NOPOS;
120     }
121     
122     public String JavaDoc toString() {
123         StringBuilder JavaDoc sb = new StringBuilder JavaDoc(style.toString());
124         sb.append(" pos=");
125         sb.append(pos);
126         sb.append(" endPos=");
127         sb.append(endPos);
128         sb.append(" indent=");
129         sb.append(indent);
130         sb.append(' ');
131         sb.append(text);
132         return sb.toString();
133     }
134
135     public boolean equals(Object JavaDoc obj) {
136         if (!(obj instanceof Comment))
137             return false;
138         Comment c = (Comment)obj;
139         return c.style == style && c.pos == pos && c.endPos == endPos &&
140             c.indent == indent && c.text.equals(text);
141     }
142
143     public int hashCode() {
144         return style.hashCode() + pos + endPos + indent + text.hashCode();
145     }
146 }
147
Popular Tags