lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jpou...@apache.org
Subject svn commit: r1618625 - in /lucene/dev/trunk/lucene/core/src: java/org/apache/lucene/util/CharsRefBuilder.java test/org/apache/lucene/util/TestCharsRefBuilder.java
Date Mon, 18 Aug 2014 15:01:55 GMT
Author: jpountz
Date: Mon Aug 18 15:01:54 2014
New Revision: 1618625

URL: http://svn.apache.org/r1618625
Log:
LUCENE-5892: CharsRefBuilder implements Accountable.

Added:
    lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/util/TestCharsRefBuilder.java
  (with props)
Modified:
    lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/util/CharsRefBuilder.java

Modified: lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/util/CharsRefBuilder.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/util/CharsRefBuilder.java?rev=1618625&r1=1618624&r2=1618625&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/util/CharsRefBuilder.java (original)
+++ lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/util/CharsRefBuilder.java Mon
Aug 18 15:01:54 2014
@@ -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.
    */

Added: lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/util/TestCharsRefBuilder.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/util/TestCharsRefBuilder.java?rev=1618625&view=auto
==============================================================================
--- lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/util/TestCharsRefBuilder.java
(added)
+++ lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/util/TestCharsRefBuilder.java
Mon Aug 18 15:01:54 2014
@@ -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());
+  }
+
+}



Mime
View raw message