Class AttributeSource
An AttributeSource contains a list of different Attributes, 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 IAttribute to the AddAttribute<T>(), 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.
Inherited Members
Namespace: Lucene.Net.Util
Assembly: Lucene.Net.dll
Syntax
public class AttributeSource
Constructors
AttributeSource()
An AttributeSource using the default attribute factory DEFAULT_ATTRIBUTE_FACTORY.
Declaration
public AttributeSource()
AttributeSource(AttributeSource)
An AttributeSource that uses the same attributes as the supplied one.
Declaration
public AttributeSource(AttributeSource input)
Parameters
Type | Name | Description |
---|---|---|
AttributeSource | input |
AttributeSource(AttributeFactory)
An AttributeSource using the supplied AttributeSource.AttributeFactory for creating new IAttribute instances.
Declaration
public AttributeSource(AttributeSource.AttributeFactory factory)
Parameters
Type | Name | Description |
---|---|---|
AttributeSource.AttributeFactory | factory |
Properties
HasAttributes
Returns true
, if this AttributeSource has any attributes
Declaration
public bool HasAttributes { get; }
Property Value
Type | Description |
---|---|
bool |
Methods
AddAttributeImpl(Attribute)
Expert: Adds a custom Attribute instance with one or more IAttribute interfaces.
Please note: It is not guaranteed, that att
is added to
the AttributeSource, because the provided attributes may already exist.
You should always retrieve the wanted attributes using GetAttribute<T>() after adding
with this method and cast to your Type.
The recommended way to use custom implementations is using an AttributeSource.AttributeFactory.
Declaration
public void AddAttributeImpl(Attribute att)
Parameters
Type | Name | Description |
---|---|---|
Attribute | att |
AddAttribute<T>()
The caller must pass in an interface type that extends IAttribute. This method first checks if an instance of the corresponding class is already in this AttributeSource and returns it. Otherwise a new instance is created, added to this AttributeSource and returned.
Declaration
public T AddAttribute<T>() where T : IAttribute
Returns
Type | Description |
---|---|
T |
Type Parameters
Name | Description |
---|---|
T |
CaptureState()
Captures the state of all Attributes. The return value can be passed to RestoreState(State) to restore the state of this or another AttributeSource.
Declaration
public virtual AttributeSource.State CaptureState()
Returns
Type | Description |
---|---|
AttributeSource.State |
ClearAttributes()
Resets all Attributes in this AttributeSource by calling Clear() on each IAttribute implementation.
Declaration
public void ClearAttributes()
CloneAttributes()
Performs a clone of all Attribute instances returned in a new AttributeSource instance. This method can be used to e.g. create another TokenStream with exactly the same attributes (using AttributeSource(AttributeSource)). You can also use it as a (non-performant) replacement for CaptureState(), if you need to look into / modify the captured state.
Declaration
public AttributeSource CloneAttributes()
Returns
Type | Description |
---|---|
AttributeSource |
CopyTo(AttributeSource)
Copies the contents of this AttributeSource to the given target AttributeSource. The given instance has to provide all IAttributes this instance contains. The actual attribute implementations must be identical in both AttributeSource instances; ideally both AttributeSource instances should use the same AttributeSource.AttributeFactory. You can use this method as a replacement for RestoreState(State), if you use CloneAttributes() instead of CaptureState().
Declaration
public void CopyTo(AttributeSource target)
Parameters
Type | Name | Description |
---|---|---|
AttributeSource | target |
Equals(object)
Determines whether the specified object is equal to the current object.
Declaration
public override bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
object | obj | The object to compare with the current object. |
Returns
Type | Description |
---|---|
bool | true if the specified object is equal to the current object; otherwise, false. |
Overrides
GetAttributeClassesEnumerator()
Returns a new iterator that iterates the attribute classes in the same order they were added in.
Declaration
public IEnumerator<Type> GetAttributeClassesEnumerator()
Returns
Type | Description |
---|---|
IEnumerator<Type> |
GetAttributeFactory()
Returns the used AttributeSource.AttributeFactory.
Declaration
public AttributeSource.AttributeFactory GetAttributeFactory()
Returns
Type | Description |
---|---|
AttributeSource.AttributeFactory |
GetAttributeImplsEnumerator()
Returns a new iterator that iterates all unique IAttribute implementations. This iterator may contain less entries than GetAttributeClassesEnumerator(), if one instance implements more than one IAttribute interface.
Declaration
public IEnumerator<Attribute> GetAttributeImplsEnumerator()
Returns
Type | Description |
---|---|
IEnumerator<Attribute> |
GetAttribute<T>()
The caller must pass in an interface type that extends IAttribute. Returns the instance of the corresponding Attribute contained in this AttributeSource
Declaration
public virtual T GetAttribute<T>() where T : IAttribute
Returns
Type | Description |
---|---|
T |
Type Parameters
Name | Description |
---|---|
T |
Exceptions
Type | Condition |
---|---|
ArgumentException | if this AttributeSource does not contain the Attribute. It is recommended to always use AddAttribute<T>() even in consumers of TokenStreams, because you cannot know if a specific TokenStream really uses a specific Attribute. AddAttribute<T>() will automatically make the attribute available. If you want to only use the attribute, if it is available (to optimize consuming), use HasAttribute<T>(). |
GetHashCode()
Serves as the default hash function.
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
int | A hash code for the current object. |
Overrides
HasAttribute<T>()
The caller must pass in an interface type that extends IAttribute.
Returns true
, if this AttributeSource contains the corrsponding Attribute.
Declaration
public bool HasAttribute<T>() where T : IAttribute
Returns
Type | Description |
---|---|
bool |
Type Parameters
Name | Description |
---|---|
T |
ReflectAsString(bool)
This method returns the current attribute values as a string in the following format by calling the ReflectWith(IAttributeReflector) method:
- if
prependAttClass
=true:"AttributeClass.Key=value,AttributeClass.Key=value"
- if
prependAttClass
=false:"key=value,key=value"
Declaration
public string ReflectAsString(bool prependAttClass)
Parameters
Type | Name | Description |
---|---|---|
bool | prependAttClass |
Returns
Type | Description |
---|---|
string |
See Also
ReflectWith(IAttributeReflector)
This method is for introspection of attributes, it should simply add the key/values this AttributeSource holds to the given IAttributeReflector.
This method iterates over all IAttribute implementations and calls the corresponding ReflectWith(IAttributeReflector) method.
Declaration
public void ReflectWith(IAttributeReflector reflector)
Parameters
Type | Name | Description |
---|---|---|
IAttributeReflector | reflector |
See Also
RestoreState(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 ICharTermAttribute 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 IOffsetAttribute, but this state doesn't, then the value of the IOffsetAttribute remains unchanged. It might be desirable to reset its value to the default, in which case the caller should first call ClearAttributes() (TokenStream.ClearAttributes()
on the targetStream.
Declaration
public void RestoreState(AttributeSource.State state)
Parameters
Type | Name | Description |
---|---|---|
AttributeSource.State | state |
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.
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
string |