001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.lib.editor.util;
043:
044: /**
045: * Subsequence of the given character sequence. The backing sequence
046: * is considered to be stable i.e. does not change length or content over time.
047: *
048: * @author Miloslav Metelka
049: * @version 1.00
050: */
051:
052: public class CharSubSequence extends AbstractCharSequence {
053:
054: /**
055: * Ensure that the given start and end parameters are valid indices
056: * of the given text.
057: * @throws IndexOutOfBoundsException if the start or end are not within bounds
058: * of the given text.
059: * @deprecated use {@link CharSequenceUtilities#checkIndexesValid(CharSequence, int, int)}
060: */
061: public static void checkIndexesValid(CharSequence text, int start,
062: int end) {
063: CharSequenceUtilities.checkIndexesValid(text, start, end);
064: }
065:
066: private int length;
067:
068: private int start;
069:
070: private CharSequence backingSequence;
071:
072: /**
073: * Construct character subsequence with the given backing character sequence.
074: *
075: * @param backingSequence non-null backing character sequence. It is considered
076: * to be stable and not to change over time.
077: * @param start >=0 starting index of the subsequence within
078: * the backing character sequence.
079: * @param end >= ending index of the subsequence within
080: * the backing character sequence.
081: * @throws IndexOutOfBoundsException if the start or end are not within bounds
082: * of backingSequence.
083: */
084: public CharSubSequence(CharSequence backingSequence, int start,
085: int end) {
086: checkIndexesValid(backingSequence, start, end);
087: this .backingSequence = backingSequence;
088: this .start = start;
089: this .length = end - start;
090: }
091:
092: protected CharSequence backingSequence() {
093: return backingSequence;
094: }
095:
096: protected int start() {
097: return start;
098: }
099:
100: public int length() {
101: return length;
102: }
103:
104: public char charAt(int index) {
105: CharSequenceUtilities.checkIndexValid(index, length);
106: return backingSequence.charAt(start() + index);
107: }
108:
109: /**
110: * Subclass providing string-like implementation
111: * of <code>hashCode()</code> and <code>equals()</code>
112: * method accepting strings with the same content
113: * like charsequence has.
114: * <br>
115: * This makes the class suitable for matching to strings
116: * e.g. in maps.
117: * <br>
118: * <b>NOTE</b>: Matching is just uni-directional
119: * i.e. charsequence.equals(string) works
120: * but string.equals(charsequence) does not.
121: */
122: public static class StringLike extends CharSubSequence {
123:
124: public StringLike(CharSequence backingSequence, int start,
125: int end) {
126: super (backingSequence, start, end);
127: }
128:
129: public int hashCode() {
130: return CharSequenceUtilities.stringLikeHashCode(this );
131: }
132:
133: public boolean equals(Object o) {
134: return CharSequenceUtilities.equals(this, o);
135: }
136:
137: }
138:
139: }
|