/
SimpleString.java
452 lines (381 loc) · 10.8 KB
/
SimpleString.java
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
* Copyright 2005-2014 Red Hat, Inc.
* Red Hat licenses this file to you under the Apache License, version
* 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package org.hornetq.api.core;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.hornetq.utils.DataConstants;
/**
* A simple String class that can store all characters, and stores as simple {@code byte[]}, this
* minimises expensive copying between String objects.
* <p>
* This object is used heavily throughout HornetQ for performance reasons.
*
* @author <a href="mailto:tim.fox@jboss.com">Tim Fox</a>
*/
public final class SimpleString implements CharSequence, Serializable, Comparable<SimpleString>
{
private static final long serialVersionUID = 4204223851422244307L;
// Attributes
// ------------------------------------------------------------------------
private final byte[] data;
private transient int hash;
// Cache the string
private transient String str;
// Static
// ----------------------------------------------------------------------
/**
* Returns a SimpleString constructed from the {@code string} parameter.
* <p>
* If {@code string} is {@code null}, the return value will be {@code null} too.
*
* @param string String used to instantiate a SimpleString.
*/
public static SimpleString toSimpleString(final String string)
{
if (string == null)
{
return null;
}
return new SimpleString(string);
}
// Constructors
// ----------------------------------------------------------------------
/**
* creates a SimpleString from a conventional String
*
* @param string the string to transform
*/
public SimpleString(final String string)
{
int len = string.length();
data = new byte[len << 1];
int j = 0;
for (int i = 0; i < len; i++)
{
char c = string.charAt(i);
byte low = (byte) (c & 0xFF); // low byte
data[j++] = low;
byte high = (byte) (c >> 8 & 0xFF); // high byte
data[j++] = high;
}
str = string;
}
/**
* creates a SimpleString from a byte array
*
* @param data the byte array to use
*/
public SimpleString(final byte[] data)
{
this.data = data;
}
// CharSequence implementation
// ---------------------------------------------------------------------------
public int length()
{
return data.length >> 1;
}
public char charAt(int pos)
{
if (pos < 0 || pos >= data.length >> 1)
{
throw new IndexOutOfBoundsException();
}
pos <<= 1;
return (char) ((data[pos] & 0xFF) | (data[pos + 1] << 8) & 0xFF00);
}
public CharSequence subSequence(final int start, final int end)
{
int len = data.length >> 1;
if (end < start || start < 0 || end > len)
{
throw new IndexOutOfBoundsException();
}
else
{
int newlen = end - start << 1;
byte[] bytes = new byte[newlen];
System.arraycopy(data, start << 1, bytes, 0, newlen);
return new SimpleString(bytes);
}
}
// Comparable implementation -------------------------------------
public int compareTo(final SimpleString o)
{
return toString().compareTo(o.toString());
}
// Public
// ---------------------------------------------------------------------------
/**
* returns the underlying byte array of this SimpleString
*
* @return the byte array
*/
public byte[] getData()
{
return data;
}
/**
* returns true if the SimpleString parameter starts with the same data as this one. false if not.
*
* @param other the SimpleString to look for
* @return true if this SimpleString starts with the same data
*/
public boolean startsWith(final SimpleString other)
{
byte[] otherdata = other.data;
if (otherdata.length > data.length)
{
return false;
}
for (int i = 0; i < otherdata.length; i++)
{
if (data[i] != otherdata[i])
{
return false;
}
}
return true;
}
@Override
public String toString()
{
if (str == null)
{
int len = data.length >> 1;
char[] chars = new char[len];
int j = 0;
for (int i = 0; i < len; i++)
{
int low = data[j++] & 0xFF;
int high = data[j++] << 8 & 0xFF00;
chars[i] = (char) (low | high);
}
str = new String(chars);
}
return str;
}
@Override
public boolean equals(final Object other)
{
if (this == other)
{
return true;
}
if (other instanceof SimpleString)
{
SimpleString s = (SimpleString) other;
if (data.length != s.data.length)
{
return false;
}
for (int i = 0; i < data.length; i++)
{
if (data[i] != s.data[i])
{
return false;
}
}
return true;
}
else
{
return false;
}
}
@Override
public int hashCode()
{
if (hash == 0)
{
int tmphash = 0;
for (byte element : data)
{
tmphash = (tmphash << 5) - tmphash + element; // (hash << 5) - hash is same as hash * 31
}
hash = tmphash;
}
return hash;
}
/**
* Splits this SimpleString into an array of SimpleString using the char param as the delimiter.
* i.e. "a.b" would return "a" and "b" if . was the delimiter
*
* @param delim
*/
public SimpleString[] split(final char delim)
{
List<SimpleString> all = null;
byte low = (byte) (delim & 0xFF); // low byte
byte high = (byte) (delim >> 8 & 0xFF); // high byte
int lasPos = 0;
for (int i = 0; i < data.length; i += 2)
{
if (data[i] == low && data[i + 1] == high)
{
byte[] bytes = new byte[i - lasPos];
System.arraycopy(data, lasPos, bytes, 0, bytes.length);
lasPos = i + 2;
// We will create the ArrayList lazily
if (all == null)
{
// There will be at least 2 strings on this case (which is the actual common usecase)
// For that reason I'm allocating the ArrayList with 2 already
// I have thought about using LinkedList here but I think this will be good enough already
// Note by Clebert
all = new ArrayList<SimpleString>(2);
}
all.add(new SimpleString(bytes));
}
}
if (all == null)
{
return new SimpleString[]{this};
}
else
{
// Adding the last one
byte[] bytes = new byte[data.length - lasPos];
System.arraycopy(data, lasPos, bytes, 0, bytes.length);
all.add(new SimpleString(bytes));
// Converting it to arrays
SimpleString[] parts = new SimpleString[all.size()];
return all.toArray(parts);
}
}
/**
* checks to see if this SimpleString contains the char parameter passed in
*
* @param c the char to check for
* @return true if the char is found, false otherwise.
*/
public boolean contains(final char c)
{
final byte low = (byte) (c & 0xFF); // low byte
final byte high = (byte) (c >> 8 & 0xFF); // high byte
for (int i = 0; i < data.length; i += 2)
{
if (data[i] == low && data[i + 1] == high)
{
return true;
}
}
return false;
}
/**
* Concatenates a SimpleString and a String
*
* @param toAdd the String to concatenate with.
* @return the concatenated SimpleString
*/
public SimpleString concat(final String toAdd)
{
return concat(new SimpleString(toAdd));
}
/**
* Concatenates 2 SimpleString's
*
* @param toAdd the SimpleString to concatenate with.
* @return the concatenated SimpleString
*/
public SimpleString concat(final SimpleString toAdd)
{
byte[] bytes = new byte[data.length + toAdd.getData().length];
System.arraycopy(data, 0, bytes, 0, data.length);
System.arraycopy(toAdd.getData(), 0, bytes, data.length, toAdd.getData().length);
return new SimpleString(bytes);
}
/**
* Concatenates a SimpleString and a char
*
* @param c the char to concate with.
* @return the concatenated SimpleString
*/
public SimpleString concat(final char c)
{
byte[] bytes = new byte[data.length + 2];
System.arraycopy(data, 0, bytes, 0, data.length);
bytes[data.length] = (byte) (c & 0xFF);
bytes[data.length + 1] = (byte) (c >> 8 & 0xFF);
return new SimpleString(bytes);
}
/**
* returns the size of this SimpleString
*
* @return the size
*/
public int sizeof()
{
return DataConstants.SIZE_INT + data.length;
}
/**
* returns the size of a SimpleString
*
* @param str the SimpleString to check
* @return the size
*/
public static int sizeofString(final SimpleString str)
{
return str.sizeof();
}
/**
* returns the size of a SimpleString which could be null
*
* @param str the SimpleString to check
* @return the size
*/
public static int sizeofNullableString(final SimpleString str)
{
if (str == null)
{
return 1;
}
else
{
return 1 + str.sizeof();
}
}
/**
* This method performs a similar function to {@link String#getChars(int, int, char[], int)}.
* This is mainly used by the Parsers on Filters
*
* @param srcBegin
* @param srcEnd
* @param dst
* @param dstPos
*/
public void getChars(final int srcBegin, final int srcEnd, final char[] dst, final int dstPos)
{
if (srcBegin < 0)
{
throw new StringIndexOutOfBoundsException(srcBegin);
}
if (srcEnd > length())
{
throw new StringIndexOutOfBoundsException(srcEnd);
}
if (srcBegin > srcEnd)
{
throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
}
int j = srcBegin * 2;
int d = dstPos;
for (int i = srcBegin; i < srcEnd; i++)
{
int low = data[j++] & 0xFF;
int high = data[j++] << 8 & 0xFF00;
dst[d++] = (char) (low | high);
}
}
}