public class AttributeSource
extends java.lang.Object
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.Modifier and Type | Class and Description |
---|---|
static class |
AttributeSource.State
This class holds the state of an AttributeSource.
|
Modifier and Type | Field and Description |
---|---|
private java.util.Map<java.lang.Class<? extends AttributeImpl>,AttributeImpl> |
attributeImpls |
private java.util.Map<java.lang.Class<? extends Attribute>,AttributeImpl> |
attributes |
private AttributeSource.State[] |
currentState |
private AttributeFactory |
factory |
private static java.lang.ClassValue<java.lang.Class<? extends Attribute>[]> |
implInterfaces
a cache that stores all interfaces for known implementation classes for performance (slow reflection)
|
Constructor and Description |
---|
AttributeSource()
An AttributeSource using the default attribute factory
AttributeFactory.DEFAULT_ATTRIBUTE_FACTORY . |
AttributeSource(AttributeFactory factory)
An AttributeSource using the supplied
AttributeFactory for creating new Attribute instances. |
AttributeSource(AttributeSource input)
An AttributeSource that uses the same attributes as the supplied one.
|
Modifier and Type | Method and Description |
---|---|
<T extends Attribute> |
addAttribute(java.lang.Class<T> 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 . |
void |
endAttributes()
Resets all Attributes in this AttributeSource by calling
AttributeImpl.end() on each Attribute implementation. |
boolean |
equals(java.lang.Object obj) |
<T extends Attribute> |
getAttribute(java.lang.Class<T> attClass)
Returns the instance of the passed in Attribute contained in this AttributeSource
|
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.
|
AttributeFactory |
getAttributeFactory()
returns the used AttributeFactory.
|
java.util.Iterator<AttributeImpl> |
getAttributeImplsIterator()
Returns a new iterator that iterates all unique Attribute implementations.
|
(package private) static java.lang.Class<? extends Attribute>[] |
getAttributeInterfaces(java.lang.Class<? extends AttributeImpl> clazz) |
private AttributeSource.State |
getCurrentState() |
boolean |
hasAttribute(java.lang.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() |
java.lang.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 |
removeAllAttributes()
Removes all attributes and their implementations from this AttributeSource.
|
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.
|
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.
|
private final java.util.Map<java.lang.Class<? extends Attribute>,AttributeImpl> attributes
private final java.util.Map<java.lang.Class<? extends AttributeImpl>,AttributeImpl> attributeImpls
private final AttributeSource.State[] currentState
private final AttributeFactory factory
private static final java.lang.ClassValue<java.lang.Class<? extends Attribute>[]> implInterfaces
public AttributeSource()
AttributeFactory.DEFAULT_ATTRIBUTE_FACTORY
.public AttributeSource(AttributeSource input)
public AttributeSource(AttributeFactory factory)
AttributeFactory
for creating new Attribute
instances.public final AttributeFactory getAttributeFactory()
public final java.util.Iterator<java.lang.Class<? extends Attribute>> getAttributeClassesIterator()
public final java.util.Iterator<AttributeImpl> getAttributeImplsIterator()
getAttributeClassesIterator()
,
if one instance implements more than one Attribute interface.static java.lang.Class<? extends Attribute>[] getAttributeInterfaces(java.lang.Class<? extends AttributeImpl> clazz)
public final void addAttributeImpl(AttributeImpl att)
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<T>)
after adding
with this method and cast to your class.
The recommended way to use custom implementations is using an AttributeFactory
.
public final <T extends Attribute> T addAttribute(java.lang.Class<T> attClass)
public final boolean hasAttributes()
public final boolean hasAttribute(java.lang.Class<? extends Attribute> attClass)
public final <T extends Attribute> T getAttribute(java.lang.Class<T> attClass)
The caller must pass in a Class<? extends Attribute> value.
null
if this AttributeSource
does not contain the Attribute. It is recommended to always use
addAttribute(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), use hasAttribute(java.lang.Class<? extends org.apache.lucene.util.Attribute>)
.private AttributeSource.State getCurrentState()
public final void clearAttributes()
AttributeImpl.clear()
on each Attribute implementation.public final void endAttributes()
AttributeImpl.end()
on each Attribute implementation.public final void removeAllAttributes()
public final AttributeSource.State captureState()
restoreState(org.apache.lucene.util.AttributeSource.State)
to restore the state of this or another AttributeSource.public final void restoreState(AttributeSource.State state)
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.
public int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public final java.lang.String reflectAsString(boolean prependAttClass)
reflectWith(AttributeReflector)
method:
prependAttClass=true
: "AttributeClass#key=value,AttributeClass#key=value"
prependAttClass=false
: "key=value,key=value"
reflectWith(AttributeReflector)
public final void reflectWith(AttributeReflector reflector)
AttributeReflector
.
This method iterates over all Attribute implementations and calls the
corresponding AttributeImpl.reflectWith(org.apache.lucene.util.AttributeReflector)
method.
public final AttributeSource cloneAttributes()
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.public final void copyTo(AttributeSource target)
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 AttributeFactory
.
You can use this method as a replacement for restoreState(org.apache.lucene.util.AttributeSource.State)
, if you use
cloneAttributes()
instead of captureState()
.public java.lang.String toString()
toString
in class java.lang.Object
reflectAsString(boolean)