KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > editor > util > AbstractCharSequence


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.lib.editor.util;
21
22 /**
23  * Abstract implementation of character sequence
24  * with {@link String}-like implementation
25  * of <CODE>hashCode()</CODE> and <CODE>equals()</CODE>.
26  *
27  * @author Miloslav Metelka
28  * @version 1.00
29  */

30
31 public abstract class AbstractCharSequence implements CharSequence JavaDoc {
32
33     /**
34      * Returns the length of this character sequence. The length is the number
35      * of 16-bit Unicode characters in the sequence. </p>
36      *
37      * @return the number of characters in this sequence
38      */

39     public abstract int length();
40
41     /**
42      * Returns the character at the specified index. An index ranges from zero
43      * to <tt>length() - 1</tt>. The first character of the sequence is at
44      * index zero, the next at index one, and so on, as for array
45      * indexing. </p>
46      *
47      * @param index the index of the character to be returned
48      *
49      * @return the specified character
50      *
51      * @throws IndexOutOfBoundsException
52      * if the <tt>index</tt> argument is negative or not less than
53      * <tt>length()</tt>
54      */

55     public abstract char charAt(int index);
56
57
58     private String JavaDoc toString(int start, int end) {
59         return CharSequenceUtilities.toString(this, start, end);
60     }
61
62     /**
63      * Return subsequence of this character sequence.
64      * The returned character sequence is only as stable as is this character
65      * sequence.
66      *
67      * @param start &gt;=0 starting index of the subsequence within this
68      * character sequence.
69      * @param end &gt;=0 ending index of the subsequence within this
70      * character sequence.
71      */

72     public CharSequence JavaDoc subSequence(int start, int end) {
73         return new CharSubSequence(this, start, end);
74     }
75
76     public String JavaDoc toString() {
77         return toString(0, length());
78     }
79     
80     /**
81      * Subclass providing string-like implementation
82      * of <code>hashCode()</code> and <code>equals()</code>
83      * method accepting strings with the same content
84      * like charsequence has.
85      * <br>
86      * This makes the class suitable for matching to strings
87      * e.g. in maps.
88      * <br>
89      * <b>NOTE</b>: Matching is just uni-directional
90      * i.e. charsequence.equals(string) works
91      * but string.equals(charsequence) does not.
92      */

93     public static abstract class StringLike extends AbstractCharSequence {
94
95         public int hashCode() {
96             return CharSequenceUtilities.stringLikeHashCode(this);
97         }
98
99         public boolean equals(Object JavaDoc o) {
100             return CharSequenceUtilities.equals(this, o);
101         }
102         
103     }
104
105 }
106
Popular Tags