0N/A/*
2362N/A * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage javax.swing.text;
0N/A
0N/Aimport java.text.CharacterIterator;
0N/A
0N/A/**
0N/A * A segment of a character array representing a fragment
0N/A * of text. It should be treated as immutable even though
0N/A * the array is directly accessible. This gives fast access
0N/A * to fragments of text without the overhead of copying
0N/A * around characters. This is effectively an unprotected
0N/A * String.
0N/A * <p>
0N/A * The Segment implements the java.text.CharacterIterator
0N/A * interface to support use with the i18n support without
0N/A * copying text into a string.
0N/A *
0N/A * @author Timothy Prinzing
0N/A */
0N/Apublic class Segment implements Cloneable, CharacterIterator, CharSequence {
0N/A
0N/A /**
0N/A * This is the array containing the text of
0N/A * interest. This array should never be modified;
0N/A * it is available only for efficiency.
0N/A */
0N/A public char[] array;
0N/A
0N/A /**
0N/A * This is the offset into the array that
0N/A * the desired text begins.
0N/A */
0N/A public int offset;
0N/A
0N/A /**
0N/A * This is the number of array elements that
0N/A * make up the text of interest.
0N/A */
0N/A public int count;
0N/A
0N/A private boolean partialReturn;
0N/A
0N/A /**
0N/A * Creates a new segment.
0N/A */
0N/A public Segment() {
0N/A this(null, 0, 0);
0N/A }
0N/A
0N/A /**
0N/A * Creates a new segment referring to an existing array.
0N/A *
0N/A * @param array the array to refer to
0N/A * @param offset the offset into the array
0N/A * @param count the number of characters
0N/A */
0N/A public Segment(char[] array, int offset, int count) {
0N/A this.array = array;
0N/A this.offset = offset;
0N/A this.count = count;
0N/A partialReturn = false;
0N/A }
0N/A
0N/A /**
0N/A * Flag to indicate that partial returns are valid. If the flag is true,
0N/A * an implementation of the interface method Document.getText(position,length,Segment)
0N/A * should return as much text as possible without making a copy. The default
0N/A * state of the flag is false which will cause Document.getText(position,length,Segment)
0N/A * to provide the same return behavior it always had, which may or may not
0N/A * make a copy of the text depending upon the request.
0N/A *
0N/A * @param p whether or not partial returns are valid.
0N/A * @since 1.4
0N/A */
0N/A public void setPartialReturn(boolean p) {
0N/A partialReturn = p;
0N/A }
0N/A
0N/A /**
0N/A * Flag to indicate that partial returns are valid.
0N/A *
0N/A * @return whether or not partial returns are valid.
0N/A * @since 1.4
0N/A */
0N/A public boolean isPartialReturn() {
0N/A return partialReturn;
0N/A }
0N/A
0N/A /**
0N/A * Converts a segment into a String.
0N/A *
0N/A * @return the string
0N/A */
0N/A public String toString() {
0N/A if (array != null) {
0N/A return new String(array, offset, count);
0N/A }
215N/A return "";
0N/A }
0N/A
0N/A // --- CharacterIterator methods -------------------------------------
0N/A
0N/A /**
0N/A * Sets the position to getBeginIndex() and returns the character at that
0N/A * position.
0N/A * @return the first character in the text, or DONE if the text is empty
0N/A * @see #getBeginIndex
0N/A * @since 1.3
0N/A */
0N/A public char first() {
0N/A pos = offset;
0N/A if (count != 0) {
0N/A return array[pos];
0N/A }
0N/A return DONE;
0N/A }
0N/A
0N/A /**
0N/A * Sets the position to getEndIndex()-1 (getEndIndex() if the text is empty)
0N/A * and returns the character at that position.
0N/A * @return the last character in the text, or DONE if the text is empty
0N/A * @see #getEndIndex
0N/A * @since 1.3
0N/A */
0N/A public char last() {
0N/A pos = offset + count;
0N/A if (count != 0) {
0N/A pos -= 1;
0N/A return array[pos];
0N/A }
0N/A return DONE;
0N/A }
0N/A
0N/A /**
0N/A * Gets the character at the current position (as returned by getIndex()).
0N/A * @return the character at the current position or DONE if the current
0N/A * position is off the end of the text.
0N/A * @see #getIndex
0N/A * @since 1.3
0N/A */
0N/A public char current() {
0N/A if (count != 0 && pos < offset + count) {
0N/A return array[pos];
0N/A }
0N/A return DONE;
0N/A }
0N/A
0N/A /**
0N/A * Increments the iterator's index by one and returns the character
0N/A * at the new index. If the resulting index is greater or equal
0N/A * to getEndIndex(), the current index is reset to getEndIndex() and
0N/A * a value of DONE is returned.
0N/A * @return the character at the new position or DONE if the new
0N/A * position is off the end of the text range.
0N/A * @since 1.3
0N/A */
0N/A public char next() {
0N/A pos += 1;
0N/A int end = offset + count;
0N/A if (pos >= end) {
0N/A pos = end;
0N/A return DONE;
0N/A }
0N/A return current();
0N/A }
0N/A
0N/A /**
0N/A * Decrements the iterator's index by one and returns the character
0N/A * at the new index. If the current index is getBeginIndex(), the index
0N/A * remains at getBeginIndex() and a value of DONE is returned.
0N/A * @return the character at the new position or DONE if the current
0N/A * position is equal to getBeginIndex().
0N/A * @since 1.3
0N/A */
0N/A public char previous() {
0N/A if (pos == offset) {
0N/A return DONE;
0N/A }
0N/A pos -= 1;
0N/A return current();
0N/A }
0N/A
0N/A /**
0N/A * Sets the position to the specified position in the text and returns that
0N/A * character.
0N/A * @param position the position within the text. Valid values range from
0N/A * getBeginIndex() to getEndIndex(). An IllegalArgumentException is thrown
0N/A * if an invalid value is supplied.
0N/A * @return the character at the specified position or DONE if the specified position is equal to getEndIndex()
0N/A * @since 1.3
0N/A */
0N/A public char setIndex(int position) {
0N/A int end = offset + count;
0N/A if ((position < offset) || (position > end)) {
0N/A throw new IllegalArgumentException("bad position: " + position);
0N/A }
0N/A pos = position;
0N/A if ((pos != end) && (count != 0)) {
0N/A return array[pos];
0N/A }
0N/A return DONE;
0N/A }
0N/A
0N/A /**
0N/A * Returns the start index of the text.
0N/A * @return the index at which the text begins.
0N/A * @since 1.3
0N/A */
0N/A public int getBeginIndex() {
0N/A return offset;
0N/A }
0N/A
0N/A /**
0N/A * Returns the end index of the text. This index is the index of the first
0N/A * character following the end of the text.
0N/A * @return the index after the last character in the text
0N/A * @since 1.3
0N/A */
0N/A public int getEndIndex() {
0N/A return offset + count;
0N/A }
0N/A
0N/A /**
0N/A * Returns the current index.
0N/A * @return the current index.
0N/A * @since 1.3
0N/A */
0N/A public int getIndex() {
0N/A return pos;
0N/A }
0N/A
0N/A // --- CharSequence methods -------------------------------------
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.6
0N/A */
0N/A public char charAt(int index) {
0N/A if (index < 0
0N/A || index >= count) {
0N/A throw new StringIndexOutOfBoundsException(index);
0N/A }
0N/A return array[offset + index];
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.6
0N/A */
0N/A public int length() {
0N/A return count;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * @since 1.6
0N/A */
0N/A public CharSequence subSequence(int start, int end) {
0N/A if (start < 0) {
0N/A throw new StringIndexOutOfBoundsException(start);
0N/A }
0N/A if (end > count) {
0N/A throw new StringIndexOutOfBoundsException(end);
0N/A }
0N/A if (start > end) {
0N/A throw new StringIndexOutOfBoundsException(end - start);
0N/A }
0N/A Segment segment = new Segment();
0N/A segment.array = this.array;
0N/A segment.offset = this.offset + start;
0N/A segment.count = end - start;
0N/A return segment;
0N/A }
0N/A
0N/A /**
0N/A * Creates a shallow copy.
0N/A *
0N/A * @return the copy
0N/A */
0N/A public Object clone() {
0N/A Object o;
0N/A try {
0N/A o = super.clone();
0N/A } catch (CloneNotSupportedException cnse) {
0N/A o = null;
0N/A }
0N/A return o;
0N/A }
0N/A
0N/A private int pos;
0N/A
0N/A
0N/A}