KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > ejbql > BlockStringBuffer


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins.cmp.ejbql;
23
24 import java.util.Iterator JavaDoc;
25 import java.util.LinkedList JavaDoc;
26
27 /**
28  * A buffer simmilar to StringBuffer that works on string blocks instead
29  * of individual characters. This eliminates excessive array allocation
30  * and copying at the expense of removal and substring opperations. This
31  * is a greate compromise as usually the only functions called on a
32  * StringBuffer are append, length, and toString.
33  *
34  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
35  * @version $Revision: 37459 $
36  */

37 public final class BlockStringBuffer {
38    private LinkedList JavaDoc list = new LinkedList JavaDoc();
39    private int length;
40    
41    public BlockStringBuffer() {
42    }
43
44    public BlockStringBuffer append(boolean b) {
45       String JavaDoc string = String.valueOf(b);
46       length += string.length();
47       list.addLast(string);
48       return this;
49    }
50
51    public BlockStringBuffer append(char c) {
52       String JavaDoc string = String.valueOf(c);
53       length += string.length();
54       list.addLast(string);
55       return this;
56    }
57
58    public BlockStringBuffer append(char[] str) {
59       String JavaDoc string = String.valueOf(str);
60       length += string.length();
61       list.addLast(string);
62       return this;
63    }
64
65    public BlockStringBuffer append(char[] str, int offset, int len) {
66       String JavaDoc string = String.valueOf(str, offset, len);
67       length += string.length();
68       list.addLast(string);
69       return this;
70    }
71
72    public BlockStringBuffer append(double d) {
73       String JavaDoc string = String.valueOf(d);
74       length += string.length();
75       list.addLast(string);
76       return this;
77    }
78
79    public BlockStringBuffer append(float f) {
80       String JavaDoc string = String.valueOf(f);
81       length += string.length();
82       list.addLast(string);
83       return this;
84    }
85
86    public BlockStringBuffer append(int i) {
87       String JavaDoc string = String.valueOf(i);
88       length += string.length();
89       list.addLast(string);
90       return this;
91    }
92
93    public BlockStringBuffer append(long l) {
94       String JavaDoc string = String.valueOf(l);
95       length += string.length();
96       list.addLast(string);
97       return this;
98    }
99
100    public BlockStringBuffer append(Object JavaDoc obj) {
101       if(obj instanceof String JavaDoc) {
102          String JavaDoc string = (String JavaDoc)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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc string = String.valueOf(l);
168       length += string.length();
169       list.addFirst(string);
170       return this;
171    }
172
173    public BlockStringBuffer prepend(Object JavaDoc obj) {
174       if(obj instanceof String JavaDoc) {
175          String JavaDoc string = (String JavaDoc)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 JavaDoc 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 JavaDoc 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 JavaDoc buf = new StringBuffer JavaDoc(length);
202
203       for(int i = 0; i < list.size(); i++)
204       {
205          buf.append( (String JavaDoc)list.get(i));
206       }
207       return buf;
208    }
209
210    public String JavaDoc toString() {
211       return toStringBuffer().toString();
212    }
213 }
214
Popular Tags