-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathStringBuilder.java
More file actions
210 lines (175 loc) · 5.2 KB
/
StringBuilder.java
File metadata and controls
210 lines (175 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package ssj.algorithm.string;
import com.google.common.base.Preconditions;
import ssj.algorithm.Collection;
import ssj.algorithm.math.MathUtil;
import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.Objects;
import java.util.function.Predicate;
/**
* Created by shenshijun on 15/2/1.
*/
public class StringBuilder implements Collection<Character>, CharSequence, Comparable<StringBuilder> {
private Character[] _interval_string;
private int _cur_point;
public StringBuilder(int capacity) {
Preconditions.checkArgument(capacity >= 0, "capacity can not be less than 0");
_cur_point = -1;
_interval_string = new Character[capacity];
}
public StringBuilder(String s) {
this(s.length());
append(s);
}
@Override
public void add(Character ele) {
Preconditions.checkNotNull(ele);
//必须加一,以防止容量是0
ensureCapacity((capacity() + 1) * 2);
_interval_string[++_cur_point] = ele;
}
public StringBuilder append(String ele) {
Preconditions.checkNotNull(ele);
for (int i = 0; i < ele.length(); i++) {
add(ele.charAt(i));
}
return this;
}
public Character get(int i) {
return _interval_string[i];
}
public StringBuilder append(Object ele) {
return append(String.valueOf(ele));
}
public void remove(int index) {
Preconditions.checkPositionIndex(index, size());
System.arraycopy(_interval_string, index + 1, _interval_string, index, size() - 1 - index);
_cur_point--;
}
@Override
public void delete(Character ele) {
Preconditions.checkNotNull(ele);
int i = 0;
for (; i < size(); i++) {
if (ele.equals(_interval_string[i])) {
remove(i);
break;
}
}
}
@Override
public Collection<Character> filter(Predicate<? super Character> is_func) {
StringBuilder new_builder = new StringBuilder(size());
for (Character ele : this) {
if (is_func.test(ele)) {
new_builder.add(ele);
}
}
return new_builder;
}
@Override
public <R> Collection<R> newWithCapacity(int size) {
throw new UnsupportedOperationException("string builder");
}
@Override
public int size() {
return _cur_point + 1;
}
@Override
public int capacity() {
return _interval_string.length;
}
@Override
public Iterator<Character> iterator() {
return new CharIterator(size());
}
private void ensureCapacity(int new_size) {
if (_cur_point >= capacity() - 1 && new_size > capacity()) {
_interval_string = Arrays.copyOf(_interval_string, new_size);
}
}
@Override
public int length() {
return size();
}
@Override
public char charAt(int index) {
return _interval_string[index];
}
@Override
public CharSequence subSequence(int start, int end) {
StringBuilder new_builder = new StringBuilder(0);
for (Character c : Arrays.copyOfRange(_interval_string, start, end)) {
new_builder.append(c);
}
return new_builder;
}
@Override
public String toString() {
return String.join("", this);
}
@Override
public boolean equals(Object obj) {
if (obj instanceof StringBuilder) {
StringBuilder other = (StringBuilder) obj;
return Objects.equals(_interval_string, other._interval_string);
}
return false;
}
@Override
public int hashCode() {
return Arrays.hashCode(_interval_string);
}
@Override
public int compareTo(StringBuilder o) {
Preconditions.checkNotNull(o);
int result = 0;
for (int i = 0; i < MathUtil.min(size(), o.size()) && result == 0; i++) {
result = get(i).compareTo(o.get(i));
}
if (result == 0) {
result = Integer.valueOf(size()).compareTo(o.size());
}
return result;
}
public int intValue() {
return MathUtil.strToInt(toString());
}
public long longValue() {
return MathUtil.strToLong(toString());
}
public double doubleValue() {
//TODO 完成String to double
return -1;
}
private class CharIterator implements Iterator<Character> {
private int _cur;
private int _size;
public CharIterator(int size) {
_size = size;
_cur = -1;
}
@Override
public boolean hasNext() {
checkCurrencyModify();
return ++_cur < _size;
}
@Override
public Character next() {
checkCurrencyModify();
return StringBuilder.this.get(_cur);
}
@Override
public void remove() {
checkCurrencyModify();
StringBuilder.this.remove(_cur--);
_size--;
}
private void checkCurrencyModify() {
if (_size != StringBuilder.this.size()) {
throw new ConcurrentModificationException();
}
}
}
}