JAL-1807 Bob's first commit -- Applet loaded; needs image
[jalview.git] / src / javajs / util / SB.java
1
2 package javajs.util;
3
4 import javajs.J2SIgnoreImport;
5
6 /**
7  * Interesting thing here is that JavaScript is 3x faster than Java in handling strings.
8  * 
9  * Java StringBuilder is final, unfortunately. I guess they weren't thinking about Java2Script!
10  * 
11  * The reason we have to do this that several overloaded append methods is WAY too expensive
12  * 
13  */
14
15 @J2SIgnoreImport({java.lang.StringBuilder.class})
16 public class SB {
17   
18   private java.lang.StringBuilder sb;
19   String s; // used by JavaScript only; no Java references
20   
21   //TODO: JS experiment with using array and .push() here
22
23   public SB() {
24     /**
25      * @j2sNative
26      * 
27      *            this.s = "";
28      * 
29      */
30     {
31       sb = new java.lang.StringBuilder();
32     }
33   }
34
35   public static SB newN(int n) {
36     /**
37      * @j2sNative
38      *            return new javajs.util.SB(); 
39      */
40     {
41       // not perfect, because it requires defining sb twice. 
42       // We can do better...
43       SB sb = new SB();
44       sb.sb = new java.lang.StringBuilder(n);
45       return sb;
46     }
47   }
48
49   public static SB newS(String s) {
50     /**
51      * @j2sNative 
52      * 
53      * var sb = new javajs.util.SB();
54      * sb.s = s;
55      * return sb; 
56      * 
57      */
58     {
59     SB sb = new SB();
60     sb.sb = new java.lang.StringBuilder(s);
61     return sb;
62     }
63   }
64
65   public SB append(String s) {
66     /**
67      * @j2sNative
68      * 
69      *            this.s += s
70      * 
71      */
72     {
73       sb.append(s);
74     }
75     return this;
76   }
77   
78   public SB appendC(char c) {
79     /**
80      * @j2sNative
81      * 
82      *            this.s += c;
83      */
84     {
85       sb.append(c);
86     }
87     return this;
88     
89   }
90
91   public SB appendI(int i) {
92     /**
93      * @j2sNative
94      * 
95      *            this.s += i
96      * 
97      */
98     {
99       sb.append(i);
100     }
101     return this;
102   }
103
104   public SB appendB(boolean b) {
105     /**
106      * @j2sNative
107      * 
108      *            this.s += b
109      * 
110      */
111     {
112       sb.append(b);
113     }
114     return this;
115   }
116
117   /**
118    * note that JavaScript could drop off the ".0" in "1.0"
119    * @param f
120    * @return this
121    */
122   public SB appendF(float f) {
123     /**
124      * @j2sNative
125      * 
126      * var sf = "" + f;
127      * if (sf.indexOf(".") < 0 && sf.indexOf("e") < 0)
128      *   sf += ".0" ;
129      *            this.s += sf;
130      * 
131      */
132     {
133       sb.append(f);
134     }
135     return this;
136   }
137
138   public SB appendD(double d) {
139     /**
140      * @j2sNative
141      * 
142      * var sf = "" + d;
143      * if (sf.indexOf(".") < 0 && sf.indexOf("e") < 0)
144      *   sf += ".0" ;
145      *            this.s += sf;
146      * 
147      */
148     {
149       sb.append(d);
150     }
151     return this;
152   }
153
154   public SB appendSB(SB buf) {
155     /**
156      * @j2sNative
157      * 
158      *            this.s += buf.s;
159      * 
160      */
161     {
162       sb.append(buf.sb);
163     }
164     return this;
165   }
166
167   public SB appendO(Object data) {
168     if (data != null) {
169       /**
170        * @j2sNative
171        * 
172        *            this.s += data.toString();
173        * 
174        */
175       {
176         sb.append(data);
177       }
178     }
179     return this;
180   }
181
182   public void appendCB(char[] cb, int off, int len) {
183     /**
184      * @j2sNative
185      * 
186      * for (var i = len,j=off; --i >= 0;)
187      *            this.s += cb[j++];
188      * 
189      */
190     {
191        sb.append(cb, off, len);
192     }
193   }
194
195   @Override
196   public String toString() {
197     /**
198      * @j2sNative
199      * 
200      *            return this.s;
201      * 
202      */
203     {
204       return sb.toString();
205     }
206   }
207
208   public int length() {
209     /**
210      * @j2sNative
211      * 
212      *            return this.s.length;
213      * 
214      */
215     {
216       return sb.length();
217     }
218   }
219
220   public int indexOf(String s) {
221     /**
222      * @j2sNative
223      * 
224      *            return this.s.indexOf(s);
225      * 
226      */
227     {
228       return sb.indexOf(s);
229     }
230   }
231
232   public char charAt(int i) {
233     /**
234      * @j2sNative
235      * 
236      *            return this.s.charAt(i);
237      * 
238      */
239     {
240       return sb.charAt(i);
241     }
242   }
243
244   public int charCodeAt(int i) {
245     /**
246      * @j2sNative
247      * 
248      *            return this.s.charCodeAt(i);
249      * 
250      */
251     {
252       return sb.codePointAt(i);
253     }
254   }
255
256   public void setLength(int n) {
257     /**
258      * @j2sNative
259      * 
260      *            this.s = this.s.substring(0, n);
261      */
262     {
263       sb.setLength(n);
264     }
265   }
266
267   public int lastIndexOf(String s) {
268     /**
269      * @j2sNative
270      * 
271      *            return this.s.lastIndexOf(s);
272      */
273     {
274       return sb.lastIndexOf(s);
275     }
276   }
277
278   public int indexOf2(String s, int i) {
279     /**
280      * @j2sNative
281      * 
282      *            return this.s.indexOf(s, i);
283      */
284     {
285       return sb.indexOf(s, i);
286     }
287   }
288
289   public String substring(int i) {
290     /**
291      * @j2sNative
292      * 
293      *            return this.s.substring(i);
294      */
295     {
296       return sb.substring(i);
297     }
298   }
299
300   public String substring2(int i, int j) {
301     /**
302      * @j2sNative
303      * 
304      *            return this.s.substring(i, j);
305      */
306     {
307       return sb.substring(i, j);
308     }
309   }
310
311   /**
312    * simple byte conversion not allowing for unicode.
313    * Used for base64 conversion and allows for offset
314    * @param off 
315    * @param len or -1 for full length (then off must = 0)
316    * @return byte[]
317    */
318   public byte[] toBytes(int off, int len) {
319     if (len < 0)
320       len = length() - off;
321     byte[] b = new byte[len];
322     for (int i = off + len, j = i - off; --i >= off;)
323       b[--j] = (byte) charAt(i);
324     return b;
325   }
326
327         public void replace(int start, int end, String str) {
328                 /**
329                  * @j2sNative
330                  * 
331                  * this.s = this.s.substring(0, start) + str + this.s.substring(end);
332                  */
333                 {
334                         sb.replace(start, end, str);
335                 }
336         }
337
338         public void insert(int offset, String str) {
339                 replace(offset, offset, str);
340         }
341
342 }