Class CharTermAttributeImpl
- java.lang.Object
-
- org.apache.lucene.util.AttributeImpl
-
- org.apache.lucene.analysis.tokenattributes.CharTermAttributeImpl
-
- All Implemented Interfaces:
java.lang.Appendable
,java.lang.CharSequence
,java.lang.Cloneable
,CharTermAttribute
,TermToBytesRefAttribute
,Attribute
- Direct Known Subclasses:
CollatedTermAttributeImpl
,Token
public class CharTermAttributeImpl extends AttributeImpl implements CharTermAttribute, TermToBytesRefAttribute, java.lang.Cloneable
Default implementation ofCharTermAttribute
.
-
-
Constructor Summary
Constructors Constructor Description CharTermAttributeImpl()
Initialize this attribute with empty term text
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description CharTermAttribute
append(char c)
CharTermAttribute
append(java.lang.CharSequence csq)
CharTermAttribute
append(java.lang.CharSequence csq, int start, int end)
CharTermAttribute
append(java.lang.String s)
Appends the specifiedString
to this character sequence.CharTermAttribute
append(java.lang.StringBuilder s)
Appends the specifiedStringBuilder
to this character sequence.CharTermAttribute
append(CharTermAttribute ta)
Appends the contents of the otherCharTermAttribute
to this character sequence.char[]
buffer()
Returns the internal termBuffer character array which you can then directly alter.char
charAt(int index)
void
clear()
Clears the values in this AttributeImpl and resets it to its default value.CharTermAttributeImpl
clone()
Shallow clone.void
copyBuffer(char[] buffer, int offset, int length)
Copies the contents of buffer, starting at offset for length characters, into the termBuffer array.void
copyTo(AttributeImpl target)
Copies the values from this Attribute into the passed-in target attribute.boolean
equals(java.lang.Object other)
int
fillBytesRef()
Updates the bytesTermToBytesRefAttribute.getBytesRef()
to contain this term's final encoding, and returns its hashcode.BytesRef
getBytesRef()
Retrieve this attribute's BytesRef.int
hashCode()
int
length()
void
reflectWith(AttributeReflector reflector)
This method is for introspection of attributes, it should simply add the key/values this attribute holds to the givenAttributeReflector
.char[]
resizeBuffer(int newSize)
Grows the termBuffer to at least size newSize, preserving the existing content.CharTermAttribute
setEmpty()
Sets the length of the termBuffer to zero.CharTermAttribute
setLength(int length)
Set number of valid characters (length of the term) in the termBuffer array.java.lang.CharSequence
subSequence(int start, int end)
java.lang.String
toString()
Returns solely the term text as specified by theCharSequence
interface.-
Methods inherited from class org.apache.lucene.util.AttributeImpl
reflectAsString
-
-
-
-
Method Detail
-
copyBuffer
public final void copyBuffer(char[] buffer, int offset, int length)
Description copied from interface:CharTermAttribute
Copies the contents of buffer, starting at offset for length characters, into the termBuffer array.- Specified by:
copyBuffer
in interfaceCharTermAttribute
- Parameters:
buffer
- the buffer to copyoffset
- the index in the buffer of the first character to copylength
- the number of characters to copy
-
buffer
public final char[] buffer()
Description copied from interface:CharTermAttribute
Returns the internal termBuffer character array which you can then directly alter. If the array is too small for your token, useCharTermAttribute.resizeBuffer(int)
to increase it. After altering the buffer be sure to callCharTermAttribute.setLength(int)
to record the number of valid characters that were placed into the termBuffer.NOTE: The returned buffer may be larger than the valid
CharSequence.length()
.- Specified by:
buffer
in interfaceCharTermAttribute
-
resizeBuffer
public final char[] resizeBuffer(int newSize)
Description copied from interface:CharTermAttribute
Grows the termBuffer to at least size newSize, preserving the existing content.- Specified by:
resizeBuffer
in interfaceCharTermAttribute
- Parameters:
newSize
- minimum size of the new termBuffer- Returns:
- newly created termBuffer with length >= newSize
-
setLength
public final CharTermAttribute setLength(int length)
Description copied from interface:CharTermAttribute
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, useCharTermAttribute.resizeBuffer(int)
first.- Specified by:
setLength
in interfaceCharTermAttribute
- Parameters:
length
- the truncated length
-
setEmpty
public final CharTermAttribute setEmpty()
Description copied from interface:CharTermAttribute
Sets the length of the termBuffer to zero. Use this method before appending contents using theAppendable
interface.- Specified by:
setEmpty
in interfaceCharTermAttribute
-
fillBytesRef
public int fillBytesRef()
Description copied from interface:TermToBytesRefAttribute
Updates the bytesTermToBytesRefAttribute.getBytesRef()
to contain this term's final encoding, and returns its hashcode.- Specified by:
fillBytesRef
in interfaceTermToBytesRefAttribute
- Returns:
- the hashcode as defined by
BytesRef.hashCode()
:int hash = 0; for (int i = termBytes.offset; i < termBytes.offset+termBytes.length; i++) { hash = 31*hash + termBytes.bytes[i]; }
Implement this for performance reasons, if your code can calculate the hash on-the-fly. If this is not the case, just returntermBytes.hashCode()
.
-
getBytesRef
public BytesRef getBytesRef()
Description copied from interface:TermToBytesRefAttribute
Retrieve this attribute's BytesRef. The bytes are updated from the current term when the consumer callsTermToBytesRefAttribute.fillBytesRef()
.- Specified by:
getBytesRef
in interfaceTermToBytesRefAttribute
- Returns:
- this Attributes internal BytesRef.
-
length
public final int length()
- Specified by:
length
in interfacejava.lang.CharSequence
-
charAt
public final char charAt(int index)
- Specified by:
charAt
in interfacejava.lang.CharSequence
-
subSequence
public final java.lang.CharSequence subSequence(int start, int end)
- Specified by:
subSequence
in interfacejava.lang.CharSequence
-
append
public final CharTermAttribute append(java.lang.CharSequence csq)
- Specified by:
append
in interfacejava.lang.Appendable
- Specified by:
append
in interfaceCharTermAttribute
-
append
public final CharTermAttribute append(java.lang.CharSequence csq, int start, int end)
- Specified by:
append
in interfacejava.lang.Appendable
- Specified by:
append
in interfaceCharTermAttribute
-
append
public final CharTermAttribute append(char c)
- Specified by:
append
in interfacejava.lang.Appendable
- Specified by:
append
in interfaceCharTermAttribute
-
append
public final CharTermAttribute append(java.lang.String s)
Description copied from interface:CharTermAttribute
Appends the specifiedString
to this character sequence.The characters of the
String
argument are appended, in order, increasing the length of this sequence by the length of the argument. If argument isnull
, then the four characters"null"
are appended.- Specified by:
append
in interfaceCharTermAttribute
-
append
public final CharTermAttribute append(java.lang.StringBuilder s)
Description copied from interface:CharTermAttribute
Appends the specifiedStringBuilder
to this character sequence.The characters of the
StringBuilder
argument are appended, in order, increasing the length of this sequence by the length of the argument. If argument isnull
, then the four characters"null"
are appended.- Specified by:
append
in interfaceCharTermAttribute
-
append
public final CharTermAttribute append(CharTermAttribute ta)
Description copied from interface:CharTermAttribute
Appends the contents of the otherCharTermAttribute
to this character sequence.The characters of the
CharTermAttribute
argument are appended, in order, increasing the length of this sequence by the length of the argument. If argument isnull
, then the four characters"null"
are appended.- Specified by:
append
in interfaceCharTermAttribute
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
clear
public void clear()
Description copied from class:AttributeImpl
Clears the values in this AttributeImpl and resets it to its default value. If this implementation implements more than one Attribute interface it clears all.- Specified by:
clear
in classAttributeImpl
-
clone
public CharTermAttributeImpl clone()
Description copied from class:AttributeImpl
Shallow clone. Subclasses must override this if they need to clone any members deeply,- Overrides:
clone
in classAttributeImpl
-
equals
public boolean equals(java.lang.Object other)
- Overrides:
equals
in classjava.lang.Object
-
toString
public java.lang.String toString()
Returns solely the term text as specified by theCharSequence
interface.This method changed the behavior with Lucene 3.1, before it returned a String representation of the whole term with all attributes. This affects especially the
Token
subclass.- Specified by:
toString
in interfacejava.lang.CharSequence
- Overrides:
toString
in classjava.lang.Object
-
reflectWith
public void reflectWith(AttributeReflector reflector)
Description copied from class:AttributeImpl
This method is for introspection of attributes, it should simply add the key/values this attribute holds to the givenAttributeReflector
.The default implementation calls
AttributeReflector.reflect(java.lang.Class<? extends org.apache.lucene.util.Attribute>, java.lang.String, java.lang.Object)
for all non-static fields from the implementing class, using the field name as key and the field value as value. The Attribute class is also determined by reflection. Please note that the default implementation can only handle single-Attribute implementations.Custom implementations look like this (e.g. for a combined attribute implementation):
public void reflectWith(AttributeReflector reflector) { reflector.reflect(CharTermAttribute.class, "term", term()); reflector.reflect(PositionIncrementAttribute.class, "positionIncrement", getPositionIncrement()); }
If you implement this method, make sure that for each invocation, the same set of
Attribute
interfaces and keys are passed toAttributeReflector.reflect(java.lang.Class<? extends org.apache.lucene.util.Attribute>, java.lang.String, java.lang.Object)
in the same order, but possibly different values. So don't automatically exclude e.g.null
properties!- Overrides:
reflectWith
in classAttributeImpl
- See Also:
AttributeImpl.reflectAsString(boolean)
-
copyTo
public void copyTo(AttributeImpl target)
Description copied from class:AttributeImpl
Copies the values from this Attribute into the passed-in target attribute. The target implementation must support all the Attributes this implementation supports.- Specified by:
copyTo
in classAttributeImpl
-
-