## org.apache.lucene.util
Class AttributeSource

    java.lang.Object
      org.apache.lucene.util.AttributeSource

Direct Known Subclasses:
  TokenStream

    public class AttributeSource

extends Object

An AttributeSource contains a list of different `AttributeImpl`s, 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 the `addAttribute(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
`static class` `AttributeSource.AttributeFactory` An AttributeFactory creates instances of `AttributeImpl`s.
`static class` `AttributeSource.State` This class holds the state of an AttributeSource.

Constructor Summary
`AttributeSource()` An AttributeSource using the default attribute factory `AttributeSource.AttributeFactory.DEFAULT_ATTRIBUTE_FACTORY`.
`AttributeSource(AttributeSource.AttributeFactory factory)` An AttributeSource using the supplied `AttributeSource.AttributeFactory` for creating new `Attribute` instances.
`AttributeSource(AttributeSource input)` An AttributeSource that uses the same attributes as the supplied one.

Method Summary

`

`<A extends Attribute> A`

`

`addAttribute(Class<A> attClass)`
The caller must pass in a Class<? extends Attribute> value.

`void` `addAttributeImpl(AttributeImpl att)` Expert: Adds a custom AttributeImpl instance with one or more Attribute interfaces.
`AttributeSource.State` `captureState()` Captures the state of all Attributes.
`void` `clearAttributes()` Resets all Attributes in this AttributeSource by calling `AttributeImpl.clear()` on each Attribute implementation.
`AttributeSource` `cloneAttributes()` Performs a clone of all `AttributeImpl` instances returned in a new `AttributeSource` instance.
`void` `copyTo(AttributeSource target)` Copies the contents of this `AttributeSource` to the given target `AttributeSource`.
`boolean` `equals(Object obj)`

`

`<A extends Attribute> A`

`

`getAttribute(Class<A> attClass)`
The caller must pass in a Class<? extends Attribute> value.

`Iterator<Class<? extends Attribute>>` `getAttributeClassesIterator()` Returns a new iterator that iterates the attribute classes in the same order they were added in.
`AttributeSource.AttributeFactory` `getAttributeFactory()` returns the used AttributeFactory.
`Iterator<AttributeImpl>` `getAttributeImplsIterator()` Returns a new iterator that iterates all unique Attribute implementations.
`boolean` `hasAttribute(Class<? extends Attribute> attClass)` The caller must pass in a Class<? extends Attribute> value.
`boolean` `hasAttributes()` Returns true, iff this AttributeSource has any attributes
`int` `hashCode()`
`String` `reflectAsString(boolean prependAttClass)` This method returns the current attribute values as a string in the following format by calling the `reflectWith(AttributeReflector)` method: iff `prependAttClass=true`: `"AttributeClass#key=value,AttributeClass#key=value"` iff `prependAttClass=false`: `"key=value,key=value"`
`void` `reflectWith(AttributeReflector reflector)` This method is for introspection of attributes, it should simply add the key/values this AttributeSource holds to the given `AttributeReflector`.
`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.
`String` `toString()` Returns a string representation of the object.

Methods inherited from class java.lang.Object
`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

Constructor Detail

### AttributeSource

    public AttributeSource()

  An AttributeSource using the default attribute factory `AttributeSource.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 supplied `AttributeSource.AttributeFactory` for creating new `Attribute` instances.

Method Detail

### getAttributeFactory

    public AttributeSource.AttributeFactory getAttributeFactory()

  returns the used AttributeFactory.

### getAttributeClassesIterator

    public Iterator<Class<? extends Attribute>> getAttributeClassesIterator()

  Returns a new iterator that iterates the attribute classes in the same order they were added in.

### getAttributeImplsIterator

    public Iterator<AttributeImpl> getAttributeImplsIterator()

  Returns a new iterator that iterates all unique Attribute implementations. This iterator may contain less entries that `getAttributeClassesIterator()`, if one instance implements more than one Attribute interface.

### addAttributeImpl

    public void addAttributeImpl(AttributeImpl att)

  Expert: Adds a custom AttributeImpl instance with one or more Attribute 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(java.lang.Class)` after adding with this method and cast to your class. The recommended way to use custom implementations is using an `AttributeSource.AttributeFactory`.

### addAttribute

    public <A extends Attribute> A addAttribute(Class<A> 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 boolean hasAttributes()

  Returns true, iff this AttributeSource has any attributes

### hasAttribute

    public boolean hasAttribute(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 <A extends Attribute> A getAttribute(Class<A> attClass)

  The caller must pass in a Class<? extends Attribute> value. Returns the instance of the passed in Attribute contained in this AttributeSource

  Throws:
    `IllegalArgumentException` - if this AttributeSource does not contain the Attribute. It is recommended to always use `addAttribute(java.lang.Class)` even in consumers of TokenStreams, because you cannot know if a specific TokenStream really uses a specific Attribute. `addAttribute(java.lang.Class)` will automatically make the attribute available. If you want to only use the attribute, if it is available (to optimize consuming), use `hasAttribute(java.lang.Class<? extends org.apache.lucene.util.Attribute>)`.

### clearAttributes

    public void clearAttributes()

  Resets all Attributes in this AttributeSource by calling `AttributeImpl.clear()` on each Attribute implementation.

### captureState

    public AttributeSource.State captureState()

  Captures the state of all Attributes. The return value can be passed to `restoreState(org.apache.lucene.util.AttributeSource.State)` to restore the state of this or another AttributeSource.

### restoreState

    public 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:
    `hashCode` in class `Object`

### equals

    public boolean equals(Object obj)

  Overrides:
    `equals` in class `Object`

### toString

    public String toString()

  Returns a string representation of the object. In general, the `toString` method returns a string that "textually represents" this object.

  WARNING: For backwards compatibility this method is implemented as in Lucene 2.9/3.0. In Lucene 4.0 this default implementation will be removed.

  It is recommeneded to use `reflectAsString(boolean)` or `reflectWith(org.apache.lucene.util.AttributeReflector)` to get a well-defined output of AttributeSource's internals.

  Overrides:
    `toString` in class `Object`

### reflectAsString

    public final String reflectAsString(boolean prependAttClass)

  This method returns the current attribute values as a string in the following format by calling the `reflectWith(AttributeReflector)` method:
  - iff `prependAttClass=true`: `"AttributeClass#key=value,AttributeClass#key=value"`
  - iff `prependAttClass=false`: `"key=value,key=value"`

  See Also:
    `reflectWith(AttributeReflector)`

### 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 given `AttributeReflector`.

  This method iterates over all Attribute implementations and calls the corresponding `AttributeImpl.reflectWith(org.apache.lucene.util.AttributeReflector)` method.

  See Also:
    `AttributeImpl.reflectWith(org.apache.lucene.util.AttributeReflector)`

### cloneAttributes

    public AttributeSource cloneAttributes()

  Performs a clone of all `AttributeImpl` 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.

### copyTo

    public final void copyTo(AttributeSource target)

  Copies the contents of this `AttributeSource` to the given target `AttributeSource`. The given instance has to provide all `Attribute`s 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(org.apache.lucene.util.AttributeSource.State)`, if you use `cloneAttributes()` instead of `captureState()`.