~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to editor/util/src/org/netbeans/lib/editor/util/CharSubSequence.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.lib.editor.util;
 
43
 
 
44
/**
 
45
 * Subsequence of the given character sequence. The backing sequence
 
46
 * is considered to be stable i.e. does not change length or content over time.
 
47
 *
 
48
 * @author Miloslav Metelka
 
49
 * @version 1.00
 
50
 */
 
51
 
 
52
public class CharSubSequence extends AbstractCharSequence {
 
53
 
 
54
    /**
 
55
     * Ensure that the given start and end parameters are valid indices
 
56
     * of the given text.
 
57
     * @throws IndexOutOfBoundsException if the start or end are not within bounds
 
58
     *  of the given text.
 
59
     * @deprecated use {@link CharSequenceUtilities#checkIndexesValid(CharSequence, int, int)}
 
60
     */
 
61
    public static void checkIndexesValid(CharSequence text, int start, int end) {
 
62
        CharSequenceUtilities.checkIndexesValid(text, start, end);
 
63
    }
 
64
    
 
65
    private int length;
 
66
    
 
67
    private int start;
 
68
 
 
69
    private CharSequence backingSequence;
 
70
    
 
71
    /**
 
72
     * Construct character subsequence with the given backing character sequence.
 
73
     *
 
74
     * @param backingSequence non-null backing character sequence. It is considered
 
75
     * to be stable and not to change over time.
 
76
     * @param start >=0 starting index of the subsequence within
 
77
     *  the backing character sequence.
 
78
     * @param end >= ending index of the subsequence within
 
79
     *  the backing character sequence.
 
80
     * @throws IndexOutOfBoundsException if the start or end are not within bounds
 
81
     *  of backingSequence.
 
82
     */
 
83
    public CharSubSequence(CharSequence backingSequence, int start, int end) {
 
84
        checkIndexesValid(backingSequence, start, end);
 
85
        this.backingSequence = backingSequence;
 
86
        this.start = start;
 
87
        this.length = end - start;
 
88
    }
 
89
    
 
90
    protected CharSequence backingSequence() {
 
91
        return backingSequence;
 
92
    }
 
93
    
 
94
    protected int start() {
 
95
        return start;
 
96
    }
 
97
 
 
98
    public int length() {
 
99
        return length;
 
100
    }
 
101
 
 
102
    public char charAt(int index) {
 
103
        CharSequenceUtilities.checkIndexValid(index, length);
 
104
        return backingSequence.charAt(start() + index);
 
105
    }
 
106
 
 
107
    /**
 
108
     * Subclass providing string-like implementation
 
109
     * of <code>hashCode()</code> and <code>equals()</code>
 
110
     * method accepting strings with the same content
 
111
     * like charsequence has.
 
112
     * <br>
 
113
     * This makes the class suitable for matching to strings
 
114
     * e.g. in maps.
 
115
     * <br>
 
116
     * <b>NOTE</b>: Matching is just uni-directional
 
117
     * i.e. charsequence.equals(string) works
 
118
     * but string.equals(charsequence) does not.
 
119
     */
 
120
    public static class StringLike extends CharSubSequence {
 
121
 
 
122
        public StringLike(CharSequence backingSequence, int start, int end) {
 
123
            super(backingSequence, start, end);
 
124
        }
 
125
    
 
126
        public int hashCode() {
 
127
            return CharSequenceUtilities.stringLikeHashCode(this);
 
128
        }
 
129
 
 
130
        public boolean equals(Object o) {
 
131
            return CharSequenceUtilities.equals(this, o);
 
132
        }
 
133
        
 
134
    }
 
135
 
 
136
}