001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package javax.swing.text;
018:
019: import java.text.CharacterIterator;
020:
021: import org.apache.harmony.x.swing.internal.nls.Messages;
022:
023: public class Segment implements Cloneable, CharacterIterator {
024:
025: public char[] array;
026:
027: public int count;
028:
029: public int offset;
030:
031: private boolean isPartial;
032:
033: private int pos;
034:
035: public Segment() {
036: this (null, 0, 0);
037: }
038:
039: public Segment(final char[] array, final int offset, final int count) {
040: this .array = array;
041: this .offset = offset;
042: this .count = count;
043:
044: this .pos = 0;
045: this .isPartial = false;
046: }
047:
048: @Override
049: public Object clone() {
050: Object clone;
051:
052: try {
053: clone = super .clone();
054: } catch (final CloneNotSupportedException e) {
055: clone = null;
056: }
057:
058: return clone;
059: }
060:
061: public char current() {
062: if (pos < 0 || pos >= count + offset) {
063: return DONE;
064: }
065: return array[pos];
066: }
067:
068: public char first() {
069: pos = offset;
070:
071: if (isEmpty()) {
072: return DONE;
073: }
074:
075: return array[pos];
076: }
077:
078: public int getBeginIndex() {
079: return offset;
080: }
081:
082: public int getEndIndex() {
083: return offset + count;
084: }
085:
086: public int getIndex() {
087: return pos;
088: }
089:
090: public boolean isPartialReturn() {
091: return isPartial;
092: }
093:
094: public char last() {
095: if (isEmpty()) {
096: pos = offset + count;
097: return DONE;
098: }
099:
100: pos = offset + count - 1;
101:
102: return array[pos];
103: }
104:
105: public char next() {
106: pos++;
107:
108: if (pos >= offset + count) {
109: pos = offset + count;
110: return DONE;
111: }
112:
113: return array[pos];
114: }
115:
116: public char previous() {
117: if (pos == offset) {
118: return DONE;
119: }
120:
121: return array[--pos];
122: }
123:
124: public char setIndex(final int position) {
125: if (position < 0 || position > offset + count) {
126: throw new IllegalArgumentException(Messages.getString(
127: "swing.89", position)); //$NON-NLS-1$
128: }
129:
130: pos = position;
131:
132: if (position == offset + count) {
133: return DONE;
134: }
135:
136: return array[pos];
137: }
138:
139: public void setPartialReturn(final boolean p) {
140: isPartial = p;
141: return;
142: }
143:
144: @Override
145: public String toString() {
146: return array != null ? new String(array, offset, count) : "";
147: }
148:
149: private boolean isEmpty() {
150: if (count == 0 || array == null || array.length == 0) {
151: return true;
152: }
153: return false;
154: }
155:
156: }
|