LUCENE-2484: Remove deprecated TermAttribute. Use CharTermAttribute and TermToBytesRefAttribute instead

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@952616 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2010-06-08 11:27:55 +00:00
parent dec4309ee3
commit 608f816434
11 changed files with 87 additions and 454 deletions

View File

@ -84,6 +84,9 @@ Changes in backwards compatibility policy
* LUCENE-2480: Though not a change in backwards compatibility policy, pre-3.0
indexes are no longer supported. You should upgrade to 3.x first, then run
optimize(), or reindex. (Shai Erera, Earwin Burrfoot)
* LUCENE-2484: Removed deprecated TermAttribute. Use CharTermAttribute
and TermToBytesRefAttribute instead. (Uwe Schindler)
Changes in runtime behavior

View File

@ -117,15 +117,12 @@ public final class NumericTokenStream extends TokenStream {
this.delegate = delegate;
}
@Override @SuppressWarnings("deprecation")
@Override
public AttributeImpl createAttributeInstance(Class<? extends Attribute> attClass) {
if (attClass == NumericTermAttribute.class)
return new NumericTermAttributeImpl(ts);
if (attClass.isAssignableFrom(CharTermAttribute.class) ||
// TODO: remove in 4.0 (deprecated class, also remove the suppress above):
attClass.isAssignableFrom(org.apache.lucene.analysis.tokenattributes.TermAttribute.class)
)
throw new IllegalArgumentException("NumericTokenStream does not support CharTermAttribute/TermAttribute.");
if (CharTermAttribute.class.isAssignableFrom(attClass))
throw new IllegalArgumentException("NumericTokenStream does not support CharTermAttribute.");
return delegate.createAttributeInstance(attClass);
}
}

View File

@ -17,12 +17,11 @@ package org.apache.lucene.analysis;
* limitations under the License.
*/
import org.apache.lucene.analysis.tokenattributes.TermAttributeImpl;
import org.apache.lucene.analysis.tokenattributes.CharTermAttributeImpl;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.apache.lucene.analysis.tokenattributes.FlagsAttribute;
import org.apache.lucene.analysis.tokenattributes.PayloadAttribute;
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
import org.apache.lucene.analysis.tokenattributes.TermAttribute;
import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
import org.apache.lucene.index.Payload;
import org.apache.lucene.index.TermPositions; // for javadoc
@ -119,8 +118,7 @@ import org.apache.lucene.util.AttributeImpl;
</p>
@see org.apache.lucene.index.Payload
*/
// TODO: change superclass to CharTermAttribute in 4.0! Maybe deprecate the whole class?
public class Token extends TermAttributeImpl
public class Token extends CharTermAttributeImpl
implements TypeAttribute, PositionIncrementAttribute,
FlagsAttribute, OffsetAttribute, PayloadAttribute {

View File

@ -29,17 +29,11 @@ import org.apache.lucene.util.UnicodeUtil;
/**
* The term text of a Token.
*/
public class CharTermAttributeImpl extends AttributeImpl implements CharTermAttribute, TermAttribute, TermToBytesRefAttribute, Cloneable, Serializable {
public class CharTermAttributeImpl extends AttributeImpl implements CharTermAttribute, TermToBytesRefAttribute, Cloneable, Serializable {
private static int MIN_BUFFER_SIZE = 10;
private char[] termBuffer = new char[ArrayUtil.oversize(MIN_BUFFER_SIZE, RamUsageEstimator.NUM_BYTES_CHAR)];
private int termLength = 0;
@Deprecated
public String term() {
// don't delegate to toString() here!
return new String(termBuffer, 0, termLength);
}
public final void copyBuffer(char[] buffer, int offset, int length) {
growTermBuffer(length);
@ -47,36 +41,9 @@ public class CharTermAttributeImpl extends AttributeImpl implements CharTermAttr
termLength = length;
}
@Deprecated
public void setTermBuffer(char[] buffer, int offset, int length) {
copyBuffer(buffer, offset, length);
}
@Deprecated
public void setTermBuffer(String buffer) {
int length = buffer.length();
growTermBuffer(length);
buffer.getChars(0, length, termBuffer, 0);
termLength = length;
}
@Deprecated
public void setTermBuffer(String buffer, int offset, int length) {
assert offset <= buffer.length();
assert offset + length <= buffer.length();
growTermBuffer(length);
buffer.getChars(offset, offset + length, termBuffer, 0);
termLength = length;
}
public final char[] buffer() {
return termBuffer;
}
@Deprecated
public char[] termBuffer() {
return termBuffer;
}
public final char[] resizeBuffer(int newSize) {
if(termBuffer.length < newSize){
@ -88,11 +55,6 @@ public class CharTermAttributeImpl extends AttributeImpl implements CharTermAttr
}
return termBuffer;
}
@Deprecated
public char[] resizeTermBuffer(int newSize) {
return resizeBuffer(newSize);
}
private void growTermBuffer(int newSize) {
if(termBuffer.length < newSize){
@ -101,11 +63,6 @@ public class CharTermAttributeImpl extends AttributeImpl implements CharTermAttr
termBuffer = new char[ArrayUtil.oversize(newSize, RamUsageEstimator.NUM_BYTES_CHAR)];
}
}
@Deprecated
public int termLength() {
return termLength;
}
public final CharTermAttribute setLength(int length) {
if (length > termBuffer.length)
@ -119,11 +76,6 @@ public class CharTermAttributeImpl extends AttributeImpl implements CharTermAttr
return this;
}
@Deprecated
public void setTermLength(int length) {
setLength(length);
}
// *** TermToBytesRefAttribute interface ***
public final int toBytesRef(BytesRef target) {
return UnicodeUtil.UTF16toUTF8WithHash(termBuffer, 0, termLength, target);
@ -292,13 +244,8 @@ public class CharTermAttributeImpl extends AttributeImpl implements CharTermAttr
@Override
public void copyTo(AttributeImpl target) {
if (target instanceof CharTermAttribute) {
CharTermAttribute t = (CharTermAttribute) target;
t.copyBuffer(termBuffer, 0, termLength);
} else {
TermAttribute t = (TermAttribute) target;
t.setTermBuffer(termBuffer, 0, termLength);
}
CharTermAttribute t = (CharTermAttribute) target;
t.copyBuffer(termBuffer, 0, termLength);
}
}

View File

@ -1,93 +0,0 @@
package org.apache.lucene.analysis.tokenattributes;
/**
* 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.
*/
import org.apache.lucene.util.Attribute;
/**
* The term text of a Token.
* @deprecated Use {@link CharTermAttribute} instead.
*/
@Deprecated
public interface TermAttribute extends Attribute {
/** Returns the Token's term text.
*
* This method has a performance penalty
* because the text is stored internally in a char[]. If
* possible, use {@link #termBuffer()} and {@link
* #termLength()} directly instead. If you really need a
* String, use this method, which is nothing more than
* a convenience call to <b>new String(token.termBuffer(), 0, token.termLength())</b>
*/
public String term();
/** Copies the contents of buffer, starting at offset for
* length characters, into the termBuffer array.
* @param buffer the buffer to copy
* @param offset the index in the buffer of the first character to copy
* @param length the number of characters to copy
*/
public void setTermBuffer(char[] buffer, int offset, int length);
/** Copies the contents of buffer into the termBuffer array.
* @param buffer the buffer to copy
*/
public void setTermBuffer(String buffer);
/** Copies the contents of buffer, starting at offset and continuing
* for length characters, into the termBuffer array.
* @param buffer the buffer to copy
* @param offset the index in the buffer of the first character to copy
* @param length the number of characters to copy
*/
public void setTermBuffer(String buffer, int offset, int length);
/** Returns the internal termBuffer character array which
* you can then directly alter. If the array is too
* small for your token, use {@link
* #resizeTermBuffer(int)} to increase it. After
* altering the buffer be sure to call {@link
* #setTermLength} to record the number of valid
* characters that were placed into the termBuffer. */
public char[] termBuffer();
/** Grows the termBuffer to at least size newSize, preserving the
* existing content. Note: If the next operation is to change
* the contents of the term buffer use
* {@link #setTermBuffer(char[], int, int)},
* {@link #setTermBuffer(String)}, or
* {@link #setTermBuffer(String, int, int)}
* to optimally combine the resize with the setting of the termBuffer.
* @param newSize minimum size of the new termBuffer
* @return newly created termBuffer with length >= newSize
*/
public char[] resizeTermBuffer(int newSize);
/** Return number of valid characters (length of the term)
* in the termBuffer array. */
public int termLength();
/** Set number of valid characters (length of the term) in
* the termBuffer array. Use this to truncate the termBuffer
* or to synchronize with external manipulation of the termBuffer.
* Note: to grow the size of the array,
* use {@link #resizeTermBuffer(int)} first.
* @param length the truncated length
*/
public void setTermLength(int length);
}

View File

@ -1,27 +0,0 @@
package org.apache.lucene.analysis.tokenattributes;
/**
* 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.
*/
/**
* The term text of a Token.
* @deprecated This class is only available for AttributeSource
* to be able to load an old TermAttribute implementation class.
*/
@Deprecated
public class TermAttributeImpl extends CharTermAttributeImpl {
}

View File

@ -21,7 +21,6 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import org.apache.lucene.analysis.tokenattributes.TermAttribute;
import org.apache.lucene.analysis.tokenattributes.TermToBytesRefAttribute;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.util.BytesRef;
@ -290,14 +289,7 @@ final class TermsHashPerField extends InvertedDocConsumerPerField {
@Override
void start(Fieldable f) {
if (fieldState.attributeSource.hasAttribute(TermToBytesRefAttribute.class)) {
termAtt = fieldState.attributeSource.getAttribute(TermToBytesRefAttribute.class);
} else if (fieldState.attributeSource.hasAttribute(TermAttribute.class)) {
perThread.legacyTermAttributeWrapper.setTermAttribute(fieldState.attributeSource.getAttribute(TermAttribute.class));
termAtt = perThread.legacyTermAttributeWrapper;
} else {
throw new IllegalArgumentException("Could not find a term attribute (that implements TermToBytesRefAttribute) in the TokenStream");
}
termAtt = fieldState.attributeSource.getAttribute(TermToBytesRefAttribute.class);
consumer.start(f);
if (nextPerField != null) {
nextPerField.start(f);

View File

@ -19,8 +19,6 @@ package org.apache.lucene.index;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.UnicodeUtil;
import org.apache.lucene.analysis.tokenattributes.TermAttribute;
import org.apache.lucene.analysis.tokenattributes.TermToBytesRefAttribute;
import java.io.IOException;
@ -42,23 +40,6 @@ final class TermsHashPerThread extends InvertedDocConsumerPerThread {
// Used by perField:
final BytesRef utf8 = new BytesRef(10);
final LegacyTermAttributeWrapper legacyTermAttributeWrapper = new LegacyTermAttributeWrapper();
/** This class is used to wrap a legacy TermAttribute without support for {@link TermToBytesRefAttribute}. */
@Deprecated
static class LegacyTermAttributeWrapper implements TermToBytesRefAttribute {
private TermAttribute termAtt = null;
void setTermAttribute(TermAttribute termAtt) {
this.termAtt = termAtt;
}
public int toBytesRef(BytesRef target) {
assert target.bytes != null : "target byteref must be != null, because utf8 is used here";
return UnicodeUtil.UTF16toUTF8WithHash(termAtt.termBuffer(), 0, termAtt.termLength(), target);
}
}
public TermsHashPerThread(DocInverterPerThread docInverterPerThread, final TermsHash termsHash, final TermsHash nextTermsHash, final TermsHashPerThread primaryPerThread) {
docState = docInverterPerThread.docState;

View File

@ -21,6 +21,8 @@ import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.NumericUtils;
import org.apache.lucene.analysis.tokenattributes.TermToBytesRefAttribute;
import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.CharTermAttributeImpl;
public class TestNumericTokenStream extends BaseTokenStreamTestCase {
@ -91,4 +93,23 @@ public class TestNumericTokenStream extends BaseTokenStreamTestCase {
}
}
public static interface TestAttribute extends CharTermAttribute {}
public static class TestAttributeImpl extends CharTermAttributeImpl implements TestAttribute {}
public void testCTA() throws Exception {
final NumericTokenStream stream=new NumericTokenStream();
try {
stream.addAttribute(CharTermAttribute.class);
fail("Succeeded to add CharTermAttribute.");
} catch (IllegalArgumentException iae) {
assertTrue(iae.getMessage().startsWith("NumericTokenStream does not support"));
}
try {
stream.addAttribute(TestAttribute.class);
fail("Succeeded to add TestAttribute.");
} catch (IllegalArgumentException iae) {
assertTrue(iae.getMessage().startsWith("NumericTokenStream does not support"));
}
}
}

View File

@ -34,17 +34,17 @@ public class TestToken extends LuceneTestCase {
public void testCtor() throws Exception {
Token t = new Token();
char[] content = "hello".toCharArray();
t.setTermBuffer(content, 0, content.length);
assertNotSame(t.termBuffer(), content);
t.copyBuffer(content, 0, content.length);
assertNotSame(t.buffer(), content);
assertEquals(0, t.startOffset());
assertEquals(0, t.endOffset());
assertEquals("hello", t.term());
assertEquals("hello", t.toString());
assertEquals("word", t.type());
assertEquals(0, t.getFlags());
t = new Token(6, 22);
t.setTermBuffer(content, 0, content.length);
assertEquals("hello", t.term());
t.copyBuffer(content, 0, content.length);
assertEquals("hello", t.toString());
assertEquals("hello", t.toString());
assertEquals(6, t.startOffset());
assertEquals(22, t.endOffset());
@ -52,8 +52,8 @@ public class TestToken extends LuceneTestCase {
assertEquals(0, t.getFlags());
t = new Token(6, 22, 7);
t.setTermBuffer(content, 0, content.length);
assertEquals("hello", t.term());
t.copyBuffer(content, 0, content.length);
assertEquals("hello", t.toString());
assertEquals("hello", t.toString());
assertEquals(6, t.startOffset());
assertEquals(22, t.endOffset());
@ -61,8 +61,8 @@ public class TestToken extends LuceneTestCase {
assertEquals(7, t.getFlags());
t = new Token(6, 22, "junk");
t.setTermBuffer(content, 0, content.length);
assertEquals("hello", t.term());
t.copyBuffer(content, 0, content.length);
assertEquals("hello", t.toString());
assertEquals("hello", t.toString());
assertEquals(6, t.startOffset());
assertEquals(22, t.endOffset());
@ -73,12 +73,12 @@ public class TestToken extends LuceneTestCase {
public void testResize() {
Token t = new Token();
char[] content = "hello".toCharArray();
t.setTermBuffer(content, 0, content.length);
t.copyBuffer(content, 0, content.length);
for (int i = 0; i < 2000; i++)
{
t.resizeTermBuffer(i);
assertTrue(i <= t.termBuffer().length);
assertEquals("hello", t.term());
t.resizeBuffer(i);
assertTrue(i <= t.buffer().length);
assertEquals("hello", t.toString());
}
}
@ -88,86 +88,73 @@ public class TestToken extends LuceneTestCase {
for (int i = 0; i < 20; i++)
{
char[] content = buf.toString().toCharArray();
t.setTermBuffer(content, 0, content.length);
assertEquals(buf.length(), t.termLength());
assertEquals(buf.toString(), t.term());
t.copyBuffer(content, 0, content.length);
assertEquals(buf.length(), t.length());
assertEquals(buf.toString(), t.toString());
buf.append(buf.toString());
}
assertEquals(1048576, t.termLength());
// now as a string, first variant
t = new Token();
buf = new StringBuilder("ab");
for (int i = 0; i < 20; i++)
{
String content = buf.toString();
t.setTermBuffer(content, 0, content.length());
assertEquals(content.length(), t.termLength());
assertEquals(content, t.term());
buf.append(content);
}
assertEquals(1048576, t.termLength());
assertEquals(1048576, t.length());
// now as a string, second variant
t = new Token();
buf = new StringBuilder("ab");
for (int i = 0; i < 20; i++)
{
t.setEmpty().append(buf);
String content = buf.toString();
t.setTermBuffer(content);
assertEquals(content.length(), t.termLength());
assertEquals(content, t.term());
assertEquals(content.length(), t.length());
assertEquals(content, t.toString());
buf.append(content);
}
assertEquals(1048576, t.termLength());
assertEquals(1048576, t.length());
// Test for slow growth to a long term
t = new Token();
buf = new StringBuilder("a");
for (int i = 0; i < 20000; i++)
{
t.setEmpty().append(buf);
String content = buf.toString();
t.setTermBuffer(content);
assertEquals(content.length(), t.termLength());
assertEquals(content, t.term());
assertEquals(content.length(), t.length());
assertEquals(content, t.toString());
buf.append("a");
}
assertEquals(20000, t.termLength());
assertEquals(20000, t.length());
// Test for slow growth to a long term
t = new Token();
buf = new StringBuilder("a");
for (int i = 0; i < 20000; i++)
{
t.setEmpty().append(buf);
String content = buf.toString();
t.setTermBuffer(content);
assertEquals(content.length(), t.termLength());
assertEquals(content, t.term());
assertEquals(content.length(), t.length());
assertEquals(content, t.toString());
buf.append("a");
}
assertEquals(20000, t.termLength());
assertEquals(20000, t.length());
}
public void testToString() throws Exception {
char[] b = {'a', 'l', 'o', 'h', 'a'};
Token t = new Token("", 0, 5);
t.setTermBuffer(b, 0, 5);
t.copyBuffer(b, 0, 5);
assertEquals("aloha", t.toString());
t.setTermBuffer("hi there");
t.setEmpty().append("hi there");
assertEquals("hi there", t.toString());
}
public void testTermBufferEquals() throws Exception {
Token t1a = new Token();
char[] content1a = "hello".toCharArray();
t1a.setTermBuffer(content1a, 0, 5);
t1a.copyBuffer(content1a, 0, 5);
Token t1b = new Token();
char[] content1b = "hello".toCharArray();
t1b.setTermBuffer(content1b, 0, 5);
t1b.copyBuffer(content1b, 0, 5);
Token t2 = new Token();
char[] content2 = "hello2".toCharArray();
t2.setTermBuffer(content2, 0, 6);
t2.copyBuffer(content2, 0, 6);
assertTrue(t1a.equals(t1b));
assertFalse(t1a.equals(t2));
assertFalse(t2.equals(t1b));
@ -175,27 +162,27 @@ public class TestToken extends LuceneTestCase {
public void testMixedStringArray() throws Exception {
Token t = new Token("hello", 0, 5);
assertEquals(t.termLength(), 5);
assertEquals(t.term(), "hello");
t.setTermBuffer("hello2");
assertEquals(t.termLength(), 6);
assertEquals(t.term(), "hello2");
t.setTermBuffer("hello3".toCharArray(), 0, 6);
assertEquals(t.term(), "hello3");
assertEquals(t.length(), 5);
assertEquals(t.toString(), "hello");
t.setEmpty().append("hello2");
assertEquals(t.length(), 6);
assertEquals(t.toString(), "hello2");
t.copyBuffer("hello3".toCharArray(), 0, 6);
assertEquals(t.toString(), "hello3");
char[] buffer = t.termBuffer();
char[] buffer = t.buffer();
buffer[1] = 'o';
assertEquals(t.term(), "hollo3");
assertEquals(t.toString(), "hollo3");
}
public void testClone() throws Exception {
Token t = new Token(0, 5);
char[] content = "hello".toCharArray();
t.setTermBuffer(content, 0, 5);
char[] buf = t.termBuffer();
t.copyBuffer(content, 0, 5);
char[] buf = t.buffer();
Token copy = (Token) TestSimpleAttributeImpls.assertCloneIsEqual(t);
assertEquals(t.term(), copy.term());
assertNotSame(buf, copy.termBuffer());
assertEquals(t.toString(), copy.toString());
assertNotSame(buf, copy.buffer());
Payload pl = new Payload(new byte[]{1,2,3,4});
t.setPayload(pl);
@ -207,16 +194,16 @@ public class TestToken extends LuceneTestCase {
public void testCopyTo() throws Exception {
Token t = new Token();
Token copy = (Token) TestSimpleAttributeImpls.assertCopyIsEqual(t);
assertEquals("", t.term());
assertEquals("", copy.term());
assertEquals("", t.toString());
assertEquals("", copy.toString());
t = new Token(0, 5);
char[] content = "hello".toCharArray();
t.setTermBuffer(content, 0, 5);
char[] buf = t.termBuffer();
t.copyBuffer(content, 0, 5);
char[] buf = t.buffer();
copy = (Token) TestSimpleAttributeImpls.assertCopyIsEqual(t);
assertEquals(t.term(), copy.term());
assertNotSame(buf, copy.termBuffer());
assertEquals(t.toString(), copy.toString());
assertNotSame(buf, copy.buffer());
Payload pl = new Payload(new byte[]{1,2,3,4});
t.setPayload(pl);
@ -241,7 +228,7 @@ public class TestToken extends LuceneTestCase {
public void testTokenAttributeFactory() throws Exception {
TokenStream ts = new MockTokenizer(Token.TOKEN_ATTRIBUTE_FACTORY, new StringReader("foo bar"), MockTokenizer.WHITESPACE, false);
assertTrue("TypeAttribute is not implemented by SenselessAttributeImpl",
assertTrue("SenselessAttribute is not implemented by SenselessAttributeImpl",
ts.addAttribute(SenselessAttribute.class) instanceof SenselessAttributeImpl);
assertTrue("CharTermAttribute is not implemented by Token",

View File

@ -1,173 +0,0 @@
package org.apache.lucene.analysis.tokenattributes;
/**
* 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.
*/
import org.apache.lucene.util.LuceneTestCase;
public class TestTermAttributeImpl extends LuceneTestCase {
public TestTermAttributeImpl(String name) {
super(name);
}
public void testResize() {
TermAttributeImpl t = new TermAttributeImpl();
char[] content = "hello".toCharArray();
t.setTermBuffer(content, 0, content.length);
for (int i = 0; i < 2000; i++)
{
t.resizeTermBuffer(i);
assertTrue(i <= t.termBuffer().length);
assertEquals("hello", t.term());
}
}
public void testGrow() {
TermAttributeImpl t = new TermAttributeImpl();
StringBuilder buf = new StringBuilder("ab");
for (int i = 0; i < 20; i++)
{
char[] content = buf.toString().toCharArray();
t.setTermBuffer(content, 0, content.length);
assertEquals(buf.length(), t.termLength());
assertEquals(buf.toString(), t.term());
buf.append(buf.toString());
}
assertEquals(1048576, t.termLength());
// now as a string, first variant
t = new TermAttributeImpl();
buf = new StringBuilder("ab");
for (int i = 0; i < 20; i++)
{
String content = buf.toString();
t.setTermBuffer(content, 0, content.length());
assertEquals(content.length(), t.termLength());
assertEquals(content, t.term());
buf.append(content);
}
assertEquals(1048576, t.termLength());
// now as a string, second variant
t = new TermAttributeImpl();
buf = new StringBuilder("ab");
for (int i = 0; i < 20; i++)
{
String content = buf.toString();
t.setTermBuffer(content);
assertEquals(content.length(), t.termLength());
assertEquals(content, t.term());
buf.append(content);
}
assertEquals(1048576, t.termLength());
// Test for slow growth to a long term
t = new TermAttributeImpl();
buf = new StringBuilder("a");
for (int i = 0; i < 20000; i++)
{
String content = buf.toString();
t.setTermBuffer(content);
assertEquals(content.length(), t.termLength());
assertEquals(content, t.term());
buf.append("a");
}
assertEquals(20000, t.termLength());
// Test for slow growth to a long term
t = new TermAttributeImpl();
buf = new StringBuilder("a");
for (int i = 0; i < 20000; i++)
{
String content = buf.toString();
t.setTermBuffer(content);
assertEquals(content.length(), t.termLength());
assertEquals(content, t.term());
buf.append("a");
}
assertEquals(20000, t.termLength());
}
public void testToString() throws Exception {
char[] b = {'a', 'l', 'o', 'h', 'a'};
TermAttributeImpl t = new TermAttributeImpl();
t.setTermBuffer(b, 0, 5);
assertEquals("aloha", t.toString());
t.setTermBuffer("hi there");
assertEquals("hi there", t.toString());
}
public void testMixedStringArray() throws Exception {
TermAttributeImpl t = new TermAttributeImpl();
t.setTermBuffer("hello");
assertEquals(t.termLength(), 5);
assertEquals(t.term(), "hello");
t.setTermBuffer("hello2");
assertEquals(t.termLength(), 6);
assertEquals(t.term(), "hello2");
t.setTermBuffer("hello3".toCharArray(), 0, 6);
assertEquals(t.term(), "hello3");
// Make sure if we get the buffer and change a character
// that term() reflects the change
char[] buffer = t.termBuffer();
buffer[1] = 'o';
assertEquals(t.term(), "hollo3");
}
public void testClone() throws Exception {
TermAttributeImpl t = new TermAttributeImpl();
char[] content = "hello".toCharArray();
t.setTermBuffer(content, 0, 5);
char[] buf = t.termBuffer();
TermAttributeImpl copy = (TermAttributeImpl) TestSimpleAttributeImpls.assertCloneIsEqual(t);
assertEquals(t.term(), copy.term());
assertNotSame(buf, copy.termBuffer());
}
public void testEquals() throws Exception {
TermAttributeImpl t1a = new TermAttributeImpl();
char[] content1a = "hello".toCharArray();
t1a.setTermBuffer(content1a, 0, 5);
TermAttributeImpl t1b = new TermAttributeImpl();
char[] content1b = "hello".toCharArray();
t1b.setTermBuffer(content1b, 0, 5);
TermAttributeImpl t2 = new TermAttributeImpl();
char[] content2 = "hello2".toCharArray();
t2.setTermBuffer(content2, 0, 6);
assertTrue(t1a.equals(t1b));
assertFalse(t1a.equals(t2));
assertFalse(t2.equals(t1b));
}
public void testCopyTo() throws Exception {
TermAttributeImpl t = new TermAttributeImpl();
TermAttributeImpl copy = (TermAttributeImpl) TestSimpleAttributeImpls.assertCopyIsEqual(t);
assertEquals("", t.term());
assertEquals("", copy.term());
t = new TermAttributeImpl();
char[] content = "hello".toCharArray();
t.setTermBuffer(content, 0, 5);
char[] buf = t.termBuffer();
copy = (TermAttributeImpl) TestSimpleAttributeImpls.assertCopyIsEqual(t);
assertEquals(t.term(), copy.term());
assertNotSame(buf, copy.termBuffer());
}
}