Apache Solr open-source search software
LUCENE-5892: CharsRefBuilder implements Accountable.
Adrien Grand394baa3b6ef4
diff --git a/lucene/core/src/java/org/apache/lucene/util/CharsRefBuilder.java b/lucene/core/src/java/org/apache/lucene/util/CharsRefBuilder.javaindex eaebd51ad2d..1c2e102ba86 100644--- a/lucene/core/src/java/org/apache/lucene/util/CharsRefBuilder.java+++ b/lucene/core/src/java/org/apache/lucene/util/CharsRefBuilder.java@@ -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.*/diff --git a/lucene/core/src/test/org/apache/lucene/util/TestCharsRefBuilder.java b/lucene/core/src/test/org/apache/lucene/util/TestCharsRefBuilder.javanew file mode 100644index 00000000000..184a3b031ae--- /dev/null+++ b/lucene/core/src/test/org/apache/lucene/util/TestCharsRefBuilder.java@@ -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());+ }++}