add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / analyzers / common / src / java / org / apache / lucene / analysis / util / OpenStringBuilder.java
1 package org.apache.lucene.analysis.util;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 /**
21  * A StringBuilder that allows one to access the array.
22  */
23 public class OpenStringBuilder implements Appendable, CharSequence {
24   protected char[] buf;
25   protected int len;
26
27   public OpenStringBuilder() {
28     this(32);
29   }
30
31   public OpenStringBuilder(int size) {
32     buf = new char[size];
33   }
34
35   public OpenStringBuilder(char[] arr, int len) {
36     set(arr, len);
37   }
38
39   public void setLength(int len) { this.len = len; }
40
41   public void set(char[] arr, int end) {
42     this.buf = arr;
43     this.len = end;
44   }
45
46   public char[] getArray() { return buf; }
47   public int size() { return len; }
48   public int length() { return len; }
49   public int capacity() { return buf.length; }
50
51   public Appendable append(CharSequence csq) {
52     return append(csq, 0, csq.length());
53   }
54
55   public Appendable append(CharSequence csq, int start, int end) {
56     reserve(end-start);
57     for (int i=start; i<end; i++) {
58       unsafeWrite(csq.charAt(i));
59     }
60     return this;
61   }
62
63   public Appendable append(char c) {
64     write(c);
65     return this;
66   }
67
68   public char charAt(int index) {
69     return buf[index];
70   }
71
72   public void setCharAt(int index, char ch) {
73     buf[index] = ch;    
74   }
75
76   public CharSequence subSequence(int start, int end) {
77     throw new UnsupportedOperationException(); // todo
78   }
79
80   public void unsafeWrite(char b) {
81     buf[len++] = b;
82   }
83
84   public void unsafeWrite(int b) { unsafeWrite((char)b); }
85
86   public void unsafeWrite(char b[], int off, int len) {
87     System.arraycopy(b, off, buf, this.len, len);
88     this.len += len;
89   }
90
91   protected void resize(int len) {
92     char newbuf[] = new char[Math.max(buf.length << 1, len)];
93     System.arraycopy(buf, 0, newbuf, 0, size());
94     buf = newbuf;
95   }
96
97   public void reserve(int num) {
98     if (len + num > buf.length) resize(len + num);
99   }
100
101   public void write(char b) {
102     if (len >= buf.length) {
103       resize(len +1);
104     }
105     unsafeWrite(b);
106   }
107
108   public void write(int b) { write((char)b); }
109
110   public final void write(char[] b) {
111     write(b,0,b.length);
112   }
113
114   public void write(char b[], int off, int len) {
115     reserve(len);
116     unsafeWrite(b, off, len);
117   }
118
119   public final void write(OpenStringBuilder arr) {
120     write(arr.buf, 0, len);
121   }
122
123   public void write(String s) {
124     reserve(s.length());
125     s.getChars(0,s.length(),buf, len);
126     len +=s.length();
127   }
128
129   public void flush() {
130   }
131
132   public final void reset() {
133     len =0;
134   }
135
136   public char[] toCharArray() {
137     char newbuf[] = new char[size()];
138     System.arraycopy(buf, 0, newbuf, 0, size());
139     return newbuf;
140   }
141
142   public String toString() {
143     return new String(buf, 0, size());
144   }
145 }