001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins.cmp.ejbql;
023:
024: import java.util.Iterator;
025: import java.util.LinkedList;
026:
027: /**
028: * A buffer simmilar to StringBuffer that works on string blocks instead
029: * of individual characters. This eliminates excessive array allocation
030: * and copying at the expense of removal and substring opperations. This
031: * is a greate compromise as usually the only functions called on a
032: * StringBuffer are append, length, and toString.
033: *
034: * @author <a href="mailto:dain@daingroup.com">Dain Sundstrom</a>
035: * @version $Revision: 57209 $
036: */
037: public final class BlockStringBuffer {
038: private LinkedList list = new LinkedList();
039: private int length;
040:
041: public BlockStringBuffer() {
042: }
043:
044: public BlockStringBuffer append(boolean b) {
045: String string = String.valueOf(b);
046: length += string.length();
047: list.addLast(string);
048: return this ;
049: }
050:
051: public BlockStringBuffer append(char c) {
052: String string = String.valueOf(c);
053: length += string.length();
054: list.addLast(string);
055: return this ;
056: }
057:
058: public BlockStringBuffer append(char[] str) {
059: String string = String.valueOf(str);
060: length += string.length();
061: list.addLast(string);
062: return this ;
063: }
064:
065: public BlockStringBuffer append(char[] str, int offset, int len) {
066: String string = String.valueOf(str, offset, len);
067: length += string.length();
068: list.addLast(string);
069: return this ;
070: }
071:
072: public BlockStringBuffer append(double d) {
073: String string = String.valueOf(d);
074: length += string.length();
075: list.addLast(string);
076: return this ;
077: }
078:
079: public BlockStringBuffer append(float f) {
080: String string = String.valueOf(f);
081: length += string.length();
082: list.addLast(string);
083: return this ;
084: }
085:
086: public BlockStringBuffer append(int i) {
087: String string = String.valueOf(i);
088: length += string.length();
089: list.addLast(string);
090: return this ;
091: }
092:
093: public BlockStringBuffer append(long l) {
094: String string = String.valueOf(l);
095: length += string.length();
096: list.addLast(string);
097: return this ;
098: }
099:
100: public BlockStringBuffer append(Object obj) {
101: if (obj instanceof String) {
102: String string = (String) obj;
103: length += string.length();
104: list.addLast(string);
105: } else if (obj instanceof BlockStringBuffer) {
106: BlockStringBuffer buf = (BlockStringBuffer) obj;
107: length += buf.length;
108: list.addAll(buf.list);
109: } else {
110: String string = String.valueOf(obj);
111: length += string.length();
112: list.addLast(string);
113: }
114: return this ;
115: }
116:
117: public BlockStringBuffer prepend(boolean b) {
118: String string = String.valueOf(b);
119: length += string.length();
120: list.addFirst(string);
121: return this ;
122: }
123:
124: public BlockStringBuffer prepend(char c) {
125: String string = String.valueOf(c);
126: length += string.length();
127: list.addFirst(string);
128: return this ;
129: }
130:
131: public BlockStringBuffer prepend(char[] str) {
132: String string = String.valueOf(str);
133: length += string.length();
134: list.addFirst(string);
135: return this ;
136: }
137:
138: public BlockStringBuffer prepend(char[] str, int offset, int len) {
139: String string = String.valueOf(str, offset, len);
140: length += string.length();
141: list.addFirst(string);
142: return this ;
143: }
144:
145: public BlockStringBuffer prepend(double d) {
146: String string = String.valueOf(d);
147: length += string.length();
148: list.addFirst(string);
149: return this ;
150: }
151:
152: public BlockStringBuffer prepend(float f) {
153: String string = String.valueOf(f);
154: length += string.length();
155: list.addFirst(string);
156: return this ;
157: }
158:
159: public BlockStringBuffer prepend(int i) {
160: String string = String.valueOf(i);
161: length += string.length();
162: list.addFirst(string);
163: return this ;
164: }
165:
166: public BlockStringBuffer prepend(long l) {
167: String string = String.valueOf(l);
168: length += string.length();
169: list.addFirst(string);
170: return this ;
171: }
172:
173: public BlockStringBuffer prepend(Object obj) {
174: if (obj instanceof String) {
175: String string = (String) obj;
176: length += string.length();
177: list.addFirst(string);
178: } else if (obj instanceof BlockStringBuffer) {
179: BlockStringBuffer buf = (BlockStringBuffer) obj;
180: length += buf.length;
181: list.addAll(0, buf.list);
182: } else {
183: String string = String.valueOf(obj);
184: length += string.length();
185: list.addFirst(string);
186: }
187: return this ;
188: }
189:
190: public int length() {
191: return length;
192: }
193:
194: public int size() {
195: return length;
196: }
197:
198: public StringBuffer toStringBuffer() {
199: // use a string buffer because it will share the final buffer
200: // with the string object which avoids an allocate and copy
201: StringBuffer buf = new StringBuffer(length);
202:
203: for (int i = 0; i < list.size(); i++) {
204: buf.append((String) list.get(i));
205: }
206: return buf;
207: }
208:
209: public String toString() {
210: return toStringBuffer().toString();
211: }
212: }
|