KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Subsequence of the given character sequence. The backing sequence
24  * is considered to be stable i.e. does not change length or content over time.
25  *
26  * @author Miloslav Metelka
27  * @version 1.00
28  */

29
30 public class CharSubSequence extends AbstractCharSequence {
31
32     /**
33      * Ensure that the given start and end parameters are valid indices
34      * of the given text.
35      * @throws IndexOutOfBoundsException if the start or end are not within bounds
36      * of the given text.
37      * @deprecated use {@link CharSequenceUtilities#checkIndexesValid(CharSequence, int, int)}
38      */

39     public static void checkIndexesValid(CharSequence JavaDoc text, int start, int end) {
40         CharSequenceUtilities.checkIndexesValid(text, start, end);
41     }
42     
43     private int length;
44     
45     private int start;
46
47     private CharSequence JavaDoc backingSequence;
48     
49     /**
50      * Construct character subsequence with the given backing character sequence.
51      *
52      * @param backingSequence non-null backing character sequence. It is considered
53      * to be stable and not to change over time.
54      * @param start >=0 starting index of the subsequence within
55      * the backing character sequence.
56      * @param end >= ending index of the subsequence within
57      * the backing character sequence.
58      * @throws IndexOutOfBoundsException if the start or end are not within bounds
59      * of backingSequence.
60      */

61     public CharSubSequence(CharSequence JavaDoc backingSequence, int start, int end) {
62         checkIndexesValid(backingSequence, start, end);
63         this.backingSequence = backingSequence;
64         this.start = start;
65         this.length = end - start;
66     }
67     
68     protected CharSequence JavaDoc backingSequence() {
69         return backingSequence;
70     }
71     
72     protected int start() {
73         return start;
74     }
75
76     public int length() {
77         return length;
78     }
79
80     public char charAt(int index) {
81         CharSequenceUtilities.checkIndexValid(index, length);
82         return backingSequence.charAt(start() + index);
83     }
84
85     /**
86      * Subclass providing string-like implementation
87      * of <code>hashCode()</code> and <code>equals()</code>
88      * method accepting strings with the same content
89      * like charsequence has.
90      * <br>
91      * This makes the class suitable for matching to strings
92      * e.g. in maps.
93      * <br>
94      * <b>NOTE</b>: Matching is just uni-directional
95      * i.e. charsequence.equals(string) works
96      * but string.equals(charsequence) does not.
97      */

98     public static class StringLike extends CharSubSequence {
99
100
101         public StringLike(CharSubSequence backingSequence, int start, int end) {
102             super(backingSequence, start, end);
103         }
104     
105         public int hashCode() {
106             return CharSequenceUtilities.stringLikeHashCode(this);
107         }
108
109         public boolean equals(Object JavaDoc o) {
110             return CharSequenceUtilities.equals(this, o);
111         }
112         
113     }
114
115 }
116
Popular Tags