mirror of https://github.com/apache/lucene.git
LUCENE-1933: Provide a convenience AttributeFactory that creates a Token instance for all basic attributes
git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@820658 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
009c1d4ca5
commit
c1f5e753d7
|
@ -22,6 +22,9 @@ Bug fixes
|
|||
|
||||
New features
|
||||
|
||||
* LUCENE-1933: Provide a convenience AttributeFactory that creates a
|
||||
Token instance for all basic attributes. (Uwe Schindler)
|
||||
|
||||
Optimizations
|
||||
|
||||
Documentation
|
||||
|
|
|
@ -35,15 +35,8 @@ public class SingleTokenTokenStream extends TokenStream {
|
|||
private Token singleToken;
|
||||
private final AttributeImpl tokenAtt;
|
||||
|
||||
private static final AttributeFactory TOKEN_ATTRIBUTE_FACTORY = new AttributeFactory() {
|
||||
public AttributeImpl createAttributeInstance(Class attClass) {
|
||||
return attClass.isAssignableFrom(Token.class)
|
||||
? new Token() : DEFAULT_ATTRIBUTE_FACTORY.createAttributeInstance(attClass);
|
||||
}
|
||||
};
|
||||
|
||||
public SingleTokenTokenStream(Token token) {
|
||||
super(TOKEN_ATTRIBUTE_FACTORY);
|
||||
super(Token.TOKEN_ATTRIBUTE_FACTORY);
|
||||
|
||||
assert token != null;
|
||||
this.singleToken = (Token) token.clone();
|
||||
|
|
|
@ -25,8 +25,10 @@ import java.util.Set;
|
|||
import org.apache.lucene.analysis.Analyzer;
|
||||
import org.apache.lucene.analysis.Token;
|
||||
import org.apache.lucene.analysis.TokenStream;
|
||||
import org.apache.lucene.analysis.tokenattributes.TermAttribute;
|
||||
import org.apache.lucene.search.BooleanQuery;
|
||||
import org.apache.lucene.search.BooleanClause.Occur;
|
||||
import org.apache.lucene.util.AttributeImpl;
|
||||
|
||||
public class IndexTimeSynonymTest extends AbstractTestCase {
|
||||
|
||||
|
@ -296,19 +298,18 @@ public class IndexTimeSynonymTest extends AbstractTestCase {
|
|||
this.tokens = tokens;
|
||||
}
|
||||
|
||||
public TokenStream tokenStream(String fieldName, Reader reader) {
|
||||
final Token reusableToken = new Token();
|
||||
|
||||
TokenStream.setOnlyUseNewAPI(true);
|
||||
TokenStream ts = new TokenStream(){
|
||||
public TokenStream tokenStream(String fieldName, Reader reader) {
|
||||
TokenStream ts = new TokenStream(Token.TOKEN_ATTRIBUTE_FACTORY) {
|
||||
final AttributeImpl reusableToken = (AttributeImpl) addAttribute(TermAttribute.class);
|
||||
int p = 0;
|
||||
|
||||
public boolean incrementToken() throws IOException {
|
||||
if( p >= tokens.length ) return false;
|
||||
clearAttributes();
|
||||
tokens[p++].copyTo(reusableToken);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
ts.addAttributeImpl(reusableToken);
|
||||
return ts;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,7 +45,7 @@ import org.apache.lucene.search.IndexSearcher;
|
|||
import org.apache.lucene.search.TermQuery;
|
||||
import org.apache.lucene.search.TopDocCollector;
|
||||
import org.apache.lucene.search.TopScoreDocCollector;
|
||||
import org.apache.lucene.util.AttributeSource;
|
||||
import org.apache.lucene.util.AttributeImpl;
|
||||
|
||||
/**
|
||||
* Asserts equality of content and behaviour of two index readers.
|
||||
|
@ -177,16 +177,16 @@ public class TestIndicesEquals extends TestCase {
|
|||
t.setPayload(new Payload(new byte[]{2}));
|
||||
tokens.add(t);
|
||||
tokens.add(createToken("fin", 7, 9));
|
||||
final Token reusableToken = new Token();
|
||||
TokenStream ts = new TokenStream() {
|
||||
TokenStream ts = new TokenStream(Token.TOKEN_ATTRIBUTE_FACTORY) {
|
||||
final AttributeImpl reusableToken = (AttributeImpl) addAttribute(TermAttribute.class);
|
||||
Iterator<Token> it = tokens.iterator();
|
||||
|
||||
public final boolean incrementToken() throws IOException {
|
||||
if (!it.hasNext()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
reusableToken.reinit(it.next());
|
||||
clearAttributes();
|
||||
it.next().copyTo(reusableToken);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -194,7 +194,6 @@ public class TestIndicesEquals extends TestCase {
|
|||
it = tokens.iterator();
|
||||
}
|
||||
};
|
||||
ts.addAttributeImpl(reusableToken);
|
||||
|
||||
document.add(new Field("f", ts));
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@ import org.apache.lucene.index.Payload;
|
|||
import org.apache.lucene.index.TermPositions; // for javadoc
|
||||
import org.apache.lucene.util.ArrayUtil;
|
||||
import org.apache.lucene.util.Attribute;
|
||||
import org.apache.lucene.util.AttributeSource;
|
||||
import org.apache.lucene.util.AttributeImpl;
|
||||
|
||||
/**
|
||||
|
@ -879,4 +880,46 @@ public class Token extends AttributeImpl
|
|||
((TypeAttribute) target).setType(type);
|
||||
}
|
||||
}
|
||||
|
||||
/** Convenience factory that returns <code>Token</code> as implementation for the basic
|
||||
* attributes and return the default impl (with "Impl" appended) for all other
|
||||
* attributes.
|
||||
* @since 3.0
|
||||
*/
|
||||
public static final AttributeSource.AttributeFactory TOKEN_ATTRIBUTE_FACTORY =
|
||||
new TokenAttributeFactory(AttributeSource.AttributeFactory.DEFAULT_ATTRIBUTE_FACTORY);
|
||||
|
||||
/** <b>Expert:</b> Creates a TokenAttributeFactory returning {@link Token} as instance for the basic attributes
|
||||
* and for all other attributes calls the given delegate factory.
|
||||
* @since 3.0
|
||||
*/
|
||||
public static final class TokenAttributeFactory extends AttributeSource.AttributeFactory {
|
||||
|
||||
private final AttributeSource.AttributeFactory delegate;
|
||||
|
||||
/** <b>Expert</b>: Creates an AttributeFactory returning {@link Token} as instance for the basic attributes
|
||||
* and for all other attributes calls the given delegate factory. */
|
||||
public TokenAttributeFactory(AttributeSource.AttributeFactory delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
public AttributeImpl createAttributeInstance(Class<? extends Attribute> attClass) {
|
||||
return attClass.isAssignableFrom(Token.class)
|
||||
? new Token() : delegate.createAttributeInstance(attClass);
|
||||
}
|
||||
|
||||
public boolean equals(Object other) {
|
||||
if (this == other) return true;
|
||||
if (other instanceof TokenAttributeFactory) {
|
||||
final TokenAttributeFactory af = (TokenAttributeFactory) other;
|
||||
return this.delegate.equals(af.delegate);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return delegate.hashCode() ^ 0x0a45aa31;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -172,7 +172,14 @@ public class AttributeSource {
|
|||
private static final IdentityHashMap<Class<? extends AttributeImpl>,LinkedList<Class<? extends Attribute>>> knownImplClasses =
|
||||
new IdentityHashMap<Class<? extends AttributeImpl>,LinkedList<Class<? extends Attribute>>>();
|
||||
|
||||
/** Adds a custom AttributeImpl instance with one or more Attribute interfaces. */
|
||||
/** <b>Expert:</b> Adds a custom AttributeImpl instance with one or more Attribute interfaces.
|
||||
* <p><font color="red"><b>Please note:</b> It is not guaranteed, that <code>att</code> is added to
|
||||
* the <code>AttributeSource</code>, because the provided attributes may already exist.
|
||||
* You should always retrieve the wanted attributes using {@link #getAttribute} after adding
|
||||
* with this method and cast to your class.
|
||||
* The recommended way to use custom implementations is using an {@link AttributeFactory}.
|
||||
* </font></p>
|
||||
*/
|
||||
public void addAttributeImpl(final AttributeImpl att) {
|
||||
final Class<? extends AttributeImpl> clazz = att.getClass();
|
||||
if (attributeImpls.containsKey(clazz)) return;
|
||||
|
|
Loading…
Reference in New Issue