Apache Lucene open-source search software
LUCENE-6173: Fix deep clone
Uwe Schindlerdf660ce6c4d7
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex d8a7c2df25c..c2f5b245188 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -433,6 +433,9 @@ Bug Fixesbeen returning the offsets of just the matching tokens in the group whenthere's a distinction. (David Smiley)+* LUCENE-6173: NumericTermAttribute and spatial/CellTokenStream do not clone+ their BytesRef(Builder)s. Also equals/hashCode was missing. (Uwe Schindler)+Documentation* LUCENE-5392: Add/improve analysis package documentation to reflectdiff --git a/lucene/core/src/java/org/apache/lucene/analysis/NumericTokenStream.java b/lucene/core/src/java/org/apache/lucene/analysis/NumericTokenStream.javaindex 4f3225c42eb..db224ba485c 100644--- a/lucene/core/src/java/org/apache/lucene/analysis/NumericTokenStream.java+++ b/lucene/core/src/java/org/apache/lucene/analysis/NumericTokenStream.java@@ -212,6 +212,39 @@ public final class NumericTokenStream extends TokenStream {final NumericTermAttribute a = (NumericTermAttribute) target;a.init(value, valueSize, precisionStep, shift);}++ @Override+ public NumericTermAttributeImpl clone() {+ NumericTermAttributeImpl t = (NumericTermAttributeImpl)super.clone();+ // Do a deep clone+ t.bytes = new BytesRefBuilder();+ t.bytes.copyBytes(bytes.get());+ return t;+ }++ @Override+ public int hashCode() {+ final int prime = 31;+ int result = 1;+ result = prime * result + precisionStep;+ result = prime * result + shift;+ result = prime * result + Long.hashCode(value);+ result = prime * result + valueSize;+ return result;+ }++ @Override+ public boolean equals(Object obj) {+ if (this == obj) return true;+ if (obj == null) return false;+ if (getClass() != obj.getClass()) return false;+ NumericTermAttributeImpl other = (NumericTermAttributeImpl) obj;+ if (precisionStep != other.precisionStep) return false;+ if (shift != other.shift) return false;+ if (value != other.value) return false;+ if (valueSize != other.valueSize) return false;+ return true;+ }}/**diff --git a/lucene/core/src/test/org/apache/lucene/analysis/TestNumericTokenStream.java b/lucene/core/src/test/org/apache/lucene/analysis/TestNumericTokenStream.javaindex 28ce77a8872..5424eb397dd 100644--- a/lucene/core/src/test/org/apache/lucene/analysis/TestNumericTokenStream.java+++ b/lucene/core/src/test/org/apache/lucene/analysis/TestNumericTokenStream.java@@ -19,7 +19,9 @@ package org.apache.lucene.analysis;import org.apache.lucene.util.BytesRef;import org.apache.lucene.util.NumericUtils;+import org.apache.lucene.analysis.NumericTokenStream.NumericTermAttributeImpl;import org.apache.lucene.analysis.tokenattributes.TermToBytesRefAttribute;+import org.apache.lucene.analysis.tokenattributes.TestCharTermAttributeImpl;import org.apache.lucene.analysis.tokenattributes.TypeAttribute;import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;import org.apache.lucene.analysis.tokenattributes.CharTermAttributeImpl;@@ -30,6 +32,7 @@ public class TestNumericTokenStream extends BaseTokenStreamTestCase {static final int ivalue = 123456;public void testLongStream() throws Exception {+ @SuppressWarnings("resource")final NumericTokenStream stream=new NumericTokenStream().setLongValue(lvalue);final TermToBytesRefAttribute bytesAtt = stream.getAttribute(TermToBytesRefAttribute.class);assertNotNull(bytesAtt);@@ -54,6 +57,7 @@ public class TestNumericTokenStream extends BaseTokenStreamTestCase {}public void testIntStream() throws Exception {+ @SuppressWarnings("resource")final NumericTokenStream stream=new NumericTokenStream().setIntValue(ivalue);final TermToBytesRefAttribute bytesAtt = stream.getAttribute(TermToBytesRefAttribute.class);assertNotNull(bytesAtt);@@ -93,6 +97,8 @@ public class TestNumericTokenStream extends BaseTokenStreamTestCase {} catch (IllegalStateException e) {// pass}++ stream.close();}public static interface TestAttribute extends CharTermAttribute {}@@ -112,6 +118,15 @@ public class TestNumericTokenStream extends BaseTokenStreamTestCase {} catch (IllegalArgumentException iae) {assertTrue(iae.getMessage().startsWith("NumericTokenStream does not support"));}+ stream.close();+ }++ public void testAttributeClone() throws Exception {+ NumericTermAttributeImpl att = new NumericTermAttributeImpl();+ NumericTermAttributeImpl copy = TestCharTermAttributeImpl.assertCloneIsEqual(att);+ assertNotSame(att.getBytesRef(), copy.getBytesRef());+ NumericTermAttributeImpl copy2 = TestCharTermAttributeImpl.assertCopyIsEqual(att);+ assertNotSame(att.getBytesRef(), copy2.getBytesRef());}}diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/CellTokenStream.java b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/CellTokenStream.javaindex 5a3d6c839ed..4784090bb5b 100644--- a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/CellTokenStream.java+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/CellTokenStream.java@@ -124,6 +124,35 @@ class CellTokenStream extends TokenStream {fillBytesRef();reflector.reflect(TermToBytesRefAttribute.class, "bytes", BytesRef.deepCopyOf(bytes));}++ @Override+ public CellTermAttributeImpl clone() {+ final CellTermAttributeImpl clone = (CellTermAttributeImpl) super.clone();+ clone.bytes = BytesRef.deepCopyOf(bytes);+ return clone;+ }++ @Override+ public int hashCode() {+ final int prime = 31;+ int result = 1;+ result = prime * result + ((cell == null) ? 0 : cell.hashCode());+ result = prime * result + Boolean.hashCode(omitLeafByte);+ return result;+ }++ @Override+ public boolean equals(Object obj) {+ if (this == obj) return true;+ if (obj == null) return false;+ if (getClass() != obj.getClass()) return false;+ CellTermAttributeImpl other = (CellTermAttributeImpl) obj;+ if (cell == null) {+ if (other.cell != null) return false;+ } else if (!cell.equals(other.cell)) return false;+ if (omitLeafByte != other.omitLeafByte) return false;+ return true;+ }}public CellTokenStream() {