001: /**
002: * MVEL (The MVFLEX Expression Language)
003: *
004: * Copyright (C) 2007 Christopher Brock, MVFLEX/Valhalla Project and the Codehaus
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: */package org.mvel.util;
019:
020: import static java.lang.System.arraycopy;
021:
022: public class StringAppender implements CharSequence {
023: private static final int DEFAULT_SIZE = 15;
024:
025: private char[] str;
026: private int capacity;
027: private int size = 0;
028:
029: public StringAppender() {
030: str = new char[capacity = DEFAULT_SIZE];
031: }
032:
033: public StringAppender(int capacity) {
034: str = new char[this .capacity = capacity];
035: }
036:
037: public StringAppender(char c) {
038: (str = new char[this .capacity = DEFAULT_SIZE])[0] = c;
039: }
040:
041: public StringAppender(char[] s) {
042: capacity = size = (str = s).length;
043: }
044:
045: public StringAppender(CharSequence s) {
046: str = new char[this .capacity = size = s.length()];
047: for (int i = 0; i < str.length; i++)
048: str[i] = s.charAt(i);
049: }
050:
051: public StringAppender(String s) {
052: capacity = size = (str = s.toCharArray()).length;
053: }
054:
055: public StringAppender append(char[] chars) {
056: if (chars.length > (capacity - size))
057: grow(chars.length);
058: for (int i = 0; i < chars.length; size++) {
059: str[size] = chars[i++];
060: }
061: // size += chars.length;
062: return this ;
063: }
064:
065: public StringAppender append(char[] chars, int start, int length) {
066: if (length > (capacity - size))
067: grow(length);
068: int x = start + length;
069: for (int i = start; i < x; i++) {
070: str[size++] = chars[i];
071: }
072: return this ;
073: }
074:
075: public StringAppender append(Object o) {
076: return append(String.valueOf(o));
077: }
078:
079: public StringAppender append(CharSequence s) {
080: if (s.length() > (capacity - size))
081: grow(s.length());
082: for (int i = 0; size < capacity; size++) {
083: str[size] = s.charAt(i++);
084: }
085: return this ;
086: }
087:
088: public StringAppender append(String s) {
089: if (s == null)
090: return this ;
091:
092: int len = s.length();
093: if (len > (capacity - size)) {
094: grow(len);
095: }
096:
097: s.getChars(0, len, str, size);
098: size += len;
099:
100: return this ;
101: }
102:
103: public StringAppender append(char c) {
104: if (size >= capacity)
105: grow(1);
106: str[size++] = c;
107: return this ;
108: }
109:
110: public int length() {
111: return size;
112: }
113:
114: private void grow(int s) {
115: if (capacity == 0)
116: capacity = DEFAULT_SIZE;
117: final char[] newArray = new char[capacity += s * 2];
118: arraycopy(str, 0, newArray, 0, size);
119: str = newArray;
120: }
121:
122: public char[] getChars(int start, int count) {
123: char[] chars = new char[count];
124: arraycopy(str, start, chars, 0, count);
125: return chars;
126: }
127:
128: public char[] toChars() {
129: char[] chars = new char[size];
130: arraycopy(str, 0, chars, 0, size);
131: return chars;
132: }
133:
134: public String toString() {
135: if (size == capacity)
136: return new String(str);
137: else
138: return new String(str, 0, size);
139: }
140:
141: public void getChars(int start, int count, char[] target, int offset) {
142: int delta = offset;
143: for (int i = start; i < count; i++) {
144: target[delta++] = str[i];
145: }
146: }
147:
148: public void reset() {
149: size = 0;
150: }
151:
152: public char charAt(int index) {
153: return str[index];
154: }
155:
156: public CharSequence subSequence(int start, int end) {
157: return new String(str, start, (end - start));
158: }
159:
160: }
|