Class AttributeSource
- java.lang.Object
-
- org.apache.lucene.util.AttributeSource
-
- Direct Known Subclasses:
TokenStream
public class AttributeSource extends java.lang.ObjectAn AttributeSource contains a list of differentAttributeImpls, and methods to add and get them. There can only be a single instance of an attribute in the same AttributeSource instance. This is ensured by passing in the actual type of the Attribute (Class<Attribute>) to theaddAttribute(Class), which then checks if an instance of that type is already present. If yes, it returns the instance, otherwise it creates a new instance and returns it.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAttributeSource.AttributeFactoryAn AttributeFactory creates instances ofAttributeImpls.static classAttributeSource.StateThis class holds the state of an AttributeSource.
-
Constructor Summary
Constructors Constructor Description AttributeSource()An AttributeSource using the default attribute factoryAttributeSource.AttributeFactory.DEFAULT_ATTRIBUTE_FACTORY.AttributeSource(AttributeSource input)An AttributeSource that uses the same attributes as the supplied one.AttributeSource(AttributeSource.AttributeFactory factory)An AttributeSource using the suppliedAttributeSource.AttributeFactoryfor creating newAttributeinstances.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T extends Attribute>
TaddAttribute(java.lang.Class<T> attClass)The caller must pass in a Class<? extends Attribute> value.voidaddAttributeImpl(AttributeImpl att)Expert: Adds a custom AttributeImpl instance with one or more Attribute interfaces.AttributeSource.StatecaptureState()Captures the state of all Attributes.voidclearAttributes()Resets all Attributes in this AttributeSource by callingAttributeImpl.clear()on each Attribute implementation.AttributeSourcecloneAttributes()Performs a clone of allAttributeImplinstances returned in a newAttributeSourceinstance.voidcopyTo(AttributeSource target)Copies the contents of thisAttributeSourceto the given targetAttributeSource.booleanequals(java.lang.Object obj)<T extends Attribute>
TgetAttribute(java.lang.Class<T> attClass)The caller must pass in a Class<? extends Attribute> value.java.util.Iterator<java.lang.Class<? extends Attribute>>getAttributeClassesIterator()Returns a new iterator that iterates the attribute classes in the same order they were added in.AttributeSource.AttributeFactorygetAttributeFactory()returns the used AttributeFactory.java.util.Iterator<AttributeImpl>getAttributeImplsIterator()Returns a new iterator that iterates all unique Attribute implementations.booleanhasAttribute(java.lang.Class<? extends Attribute> attClass)The caller must pass in a Class<? extends Attribute> value.booleanhasAttributes()Returns true, iff this AttributeSource has any attributesinthashCode()java.lang.StringreflectAsString(boolean prependAttClass)This method returns the current attribute values as a string in the following format by calling thereflectWith(AttributeReflector)method: iffprependAttClass=true:"AttributeClass#key=value,AttributeClass#key=value"iffprependAttClass=false:"key=value,key=value"voidreflectWith(AttributeReflector reflector)This method is for introspection of attributes, it should simply add the key/values this AttributeSource holds to the givenAttributeReflector.voidrestoreState(AttributeSource.State state)Restores this state by copying the values of all attribute implementations that this state contains into the attributes implementations of the targetStream.java.lang.StringtoString()Returns a string consisting of the class's simple name, the hex representation of the identity hash code, and the current reflection of all attributes.
-
-
-
Constructor Detail
-
AttributeSource
public AttributeSource()
An AttributeSource using the default attribute factoryAttributeSource.AttributeFactory.DEFAULT_ATTRIBUTE_FACTORY.
-
AttributeSource
public AttributeSource(AttributeSource input)
An AttributeSource that uses the same attributes as the supplied one.
-
AttributeSource
public AttributeSource(AttributeSource.AttributeFactory factory)
An AttributeSource using the suppliedAttributeSource.AttributeFactoryfor creating newAttributeinstances.
-
-
Method Detail
-
getAttributeFactory
public final AttributeSource.AttributeFactory getAttributeFactory()
returns the used AttributeFactory.
-
getAttributeClassesIterator
public final java.util.Iterator<java.lang.Class<? extends Attribute>> getAttributeClassesIterator()
Returns a new iterator that iterates the attribute classes in the same order they were added in.
-
getAttributeImplsIterator
public final java.util.Iterator<AttributeImpl> getAttributeImplsIterator()
Returns a new iterator that iterates all unique Attribute implementations. This iterator may contain less entries thatgetAttributeClassesIterator(), if one instance implements more than one Attribute interface.
-
addAttributeImpl
public final void addAttributeImpl(AttributeImpl att)
Expert: Adds a custom AttributeImpl instance with one or more Attribute interfaces.Please note: It is not guaranteed, that
attis added to theAttributeSource, because the provided attributes may already exist. You should always retrieve the wanted attributes usinggetAttribute(java.lang.Class<T>)after adding with this method and cast to your class. The recommended way to use custom implementations is using anAttributeSource.AttributeFactory.
-
addAttribute
public final <T extends Attribute> T addAttribute(java.lang.Class<T> attClass)
The caller must pass in a Class<? extends Attribute> value. This method first checks if an instance of that class is already in this AttributeSource and returns it. Otherwise a new instance is created, added to this AttributeSource and returned.
-
hasAttributes
public final boolean hasAttributes()
Returns true, iff this AttributeSource has any attributes
-
hasAttribute
public final boolean hasAttribute(java.lang.Class<? extends Attribute> attClass)
The caller must pass in a Class<? extends Attribute> value. Returns true, iff this AttributeSource contains the passed-in Attribute.
-
getAttribute
public final <T extends Attribute> T getAttribute(java.lang.Class<T> attClass)
The caller must pass in a Class<? extends Attribute> value. Returns the instance of the passed in Attribute contained in this AttributeSource- Throws:
java.lang.IllegalArgumentException- if this AttributeSource does not contain the Attribute. It is recommended to always useaddAttribute(java.lang.Class<T>)even in consumers of TokenStreams, because you cannot know if a specific TokenStream really uses a specific Attribute.addAttribute(java.lang.Class<T>)will automatically make the attribute available. If you want to only use the attribute, if it is available (to optimize consuming), usehasAttribute(java.lang.Class<? extends org.apache.lucene.util.Attribute>).
-
clearAttributes
public final void clearAttributes()
Resets all Attributes in this AttributeSource by callingAttributeImpl.clear()on each Attribute implementation.
-
captureState
public final AttributeSource.State captureState()
Captures the state of all Attributes. The return value can be passed torestoreState(org.apache.lucene.util.AttributeSource.State)to restore the state of this or another AttributeSource.
-
restoreState
public final void restoreState(AttributeSource.State state)
Restores this state by copying the values of all attribute implementations that this state contains into the attributes implementations of the targetStream. The targetStream must contain a corresponding instance for each argument contained in this state (e.g. it is not possible to restore the state of an AttributeSource containing a TermAttribute into a AttributeSource using a Token instance as implementation).Note that this method does not affect attributes of the targetStream that are not contained in this state. In other words, if for example the targetStream contains an OffsetAttribute, but this state doesn't, then the value of the OffsetAttribute remains unchanged. It might be desirable to reset its value to the default, in which case the caller should first call
clearAttributes()on the targetStream.
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
reflectAsString
public final java.lang.String reflectAsString(boolean prependAttClass)
This method returns the current attribute values as a string in the following format by calling thereflectWith(AttributeReflector)method:- iff
prependAttClass=true:"AttributeClass#key=value,AttributeClass#key=value" - iff
prependAttClass=false:"key=value,key=value"
- See Also:
reflectWith(AttributeReflector)
- iff
-
reflectWith
public final void reflectWith(AttributeReflector reflector)
This method is for introspection of attributes, it should simply add the key/values this AttributeSource holds to the givenAttributeReflector.This method iterates over all Attribute implementations and calls the corresponding
AttributeImpl.reflectWith(org.apache.lucene.util.AttributeReflector)method.
-
cloneAttributes
public final AttributeSource cloneAttributes()
Performs a clone of allAttributeImplinstances returned in a newAttributeSourceinstance. This method can be used to e.g. create another TokenStream with exactly the same attributes (usingAttributeSource(AttributeSource)). You can also use it as a (non-performant) replacement forcaptureState(), if you need to look into / modify the captured state.
-
copyTo
public final void copyTo(AttributeSource target)
Copies the contents of thisAttributeSourceto the given targetAttributeSource. The given instance has to provide allAttributes this instance contains. The actual attribute implementations must be identical in bothAttributeSourceinstances; ideally both AttributeSource instances should use the sameAttributeSource.AttributeFactory. You can use this method as a replacement forrestoreState(org.apache.lucene.util.AttributeSource.State), if you usecloneAttributes()instead ofcaptureState().
-
toString
public java.lang.String toString()
Returns a string consisting of the class's simple name, the hex representation of the identity hash code, and the current reflection of all attributes.- Overrides:
toStringin classjava.lang.Object- See Also:
reflectAsString(boolean)
-
-