mirror of https://github.com/apache/lucene.git
LUCENE-5892: CharsRefBuilder implements Accountable.
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1618625 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
2e6c8e7bd8
commit
394baa3b6e
|
@ -17,13 +17,16 @@ package org.apache.lucene.util;
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* A builder for {@link CharsRef} instances.
|
||||
* @lucene.internal
|
||||
*/
|
||||
public class CharsRefBuilder {
|
||||
public class CharsRefBuilder implements Appendable {
|
||||
|
||||
private static final String NULL_STRING = "null";
|
||||
|
||||
private final CharsRef ref;
|
||||
|
||||
|
@ -64,6 +67,33 @@ public class CharsRefBuilder {
|
|||
ref.length = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Appendable append(CharSequence csq) {
|
||||
if (csq == null) {
|
||||
return append(NULL_STRING);
|
||||
}
|
||||
return append(csq, 0, csq.length());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Appendable append(CharSequence csq, int start, int end) {
|
||||
if (csq == null) {
|
||||
return append(NULL_STRING);
|
||||
}
|
||||
grow(ref.length + end - start);
|
||||
for (int i = start; i < end; ++i) {
|
||||
setCharAt(ref.length++, csq.charAt(i));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Appendable append(char c) {
|
||||
grow(ref.length + 1);
|
||||
setCharAt(ref.length++, c);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the given {@link CharsRef} referenced content into this instance.
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
package org.apache.lucene.util;
|
||||
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF 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.
|
||||
*/
|
||||
|
||||
public class TestCharsRefBuilder extends LuceneTestCase {
|
||||
|
||||
public void testAppend() {
|
||||
final String s = TestUtil.randomUnicodeString(random(), 100);
|
||||
CharsRefBuilder builder = new CharsRefBuilder();
|
||||
while (builder.length() < s.length()) {
|
||||
if (random().nextBoolean()) {
|
||||
builder.append(s.charAt(builder.length()));
|
||||
} else {
|
||||
final int start = builder.length();
|
||||
final int end = TestUtil.nextInt(random(), start, s.length());
|
||||
if (random().nextBoolean()) {
|
||||
builder.append(s.substring(start, end));
|
||||
} else {
|
||||
builder.append(s, start, end);
|
||||
}
|
||||
}
|
||||
}
|
||||
assertEquals(s, builder.toString());
|
||||
}
|
||||
|
||||
public void testAppendNull() {
|
||||
CharsRefBuilder builder = new CharsRefBuilder();
|
||||
builder.append(null);
|
||||
builder.append((CharSequence) null, 1, 3);
|
||||
assertEquals("nullnull", builder.toString());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue