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: * $Header:$
018: */
019: package org.apache.beehive.netui.util.internal;
020:
021: import java.io.Serializable;
022:
023: /**
024: * Unsynchronized alternative to StringBuffer.
025: */
026: public final class InternalStringBuilder implements Serializable {
027: private char _buffer[];
028: private int _length = 0;
029: private boolean _shared;
030:
031: static final long serialVersionUID = 1;
032:
033: public InternalStringBuilder() {
034: this (16);
035: }
036:
037: public InternalStringBuilder(int length) {
038: _buffer = new char[length];
039: _shared = false;
040: }
041:
042: public InternalStringBuilder(String str) {
043: this (str.length() + 16);
044: append(str);
045: }
046:
047: public int length() {
048: return _length;
049: }
050:
051: private final void copyWhenShared() {
052: if (_shared) {
053: char newValue[] = new char[_buffer.length];
054: System.arraycopy(_buffer, 0, newValue, 0, _length);
055: _buffer = newValue;
056: _shared = false;
057: }
058: }
059:
060: public void ensureCapacity(int minCapacity) {
061: int maxCapacity = _buffer.length;
062:
063: if (minCapacity > maxCapacity) {
064: int newCapacity = (maxCapacity + 1) * 2;
065: if (minCapacity > newCapacity)
066: newCapacity = minCapacity;
067: char newValue[] = new char[newCapacity];
068: System.arraycopy(_buffer, 0, newValue, 0, _length);
069: _buffer = newValue;
070: _shared = false;
071: }
072: }
073:
074: public void setLength(int length) {
075: if (length < 0)
076: throw new StringIndexOutOfBoundsException(length);
077: ensureCapacity(length);
078:
079: if (_length < length) {
080: copyWhenShared();
081: while (_length < length) {
082: _buffer[_length++] = '\0';
083: }
084: }
085: _length = length;
086: }
087:
088: public char charAt(int index) {
089: if (index < 0 || index >= _length)
090: throw new StringIndexOutOfBoundsException(index);
091: return _buffer[index];
092: }
093:
094: public InternalStringBuilder append(Object obj) {
095: return append(String.valueOf(obj));
096: }
097:
098: public InternalStringBuilder append(String str) {
099: if (str == null)
100: str = String.valueOf(str);
101: int len = str.length();
102: ensureCapacity(_length + len);
103: copyWhenShared();
104: str.getChars(0, len, _buffer, _length);
105: _length += len;
106: return this ;
107: }
108:
109: public InternalStringBuilder append(char c) {
110: ensureCapacity(_length + 1);
111: copyWhenShared();
112: _buffer[_length++] = c;
113: return this ;
114: }
115:
116: public InternalStringBuilder append(int i) {
117: return append(String.valueOf(i));
118: }
119:
120: public String toString() {
121: _shared = true;
122: return new String(_buffer, 0, _length);
123: }
124:
125: public InternalStringBuilder deleteCharAt(int index) {
126: if (index < 0 || index >= _length)
127: throw new StringIndexOutOfBoundsException(index);
128: System.arraycopy(_buffer, index + 1, _buffer, index, _length
129: - index - 1);
130: _length--;
131: return this;
132: }
133: }
|