lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From uschind...@apache.org
Subject svn commit: r820658 - in /lucene/java/trunk: ./ contrib/analyzers/common/src/java/org/apache/lucene/analysis/miscellaneous/ contrib/fast-vector-highlighter/src/test/org/apache/lucene/search/vectorhighlight/ contrib/instantiated/src/test/org/apache/luce...
Date Thu, 01 Oct 2009 13:49:47 GMT
Author: uschindler
Date: Thu Oct  1 13:49:46 2009
New Revision: 820658

URL: http://svn.apache.org/viewvc?rev=820658&view=rev
Log:
LUCENE-1933: Provide a convenience AttributeFactory that creates a Token instance for all
basic attributes

Modified:
    lucene/java/trunk/CHANGES.txt
    lucene/java/trunk/contrib/analyzers/common/src/java/org/apache/lucene/analysis/miscellaneous/SingleTokenTokenStream.java
    lucene/java/trunk/contrib/fast-vector-highlighter/src/test/org/apache/lucene/search/vectorhighlight/IndexTimeSynonymTest.java
    lucene/java/trunk/contrib/instantiated/src/test/org/apache/lucene/store/instantiated/TestIndicesEquals.java
    lucene/java/trunk/src/java/org/apache/lucene/analysis/Token.java
    lucene/java/trunk/src/java/org/apache/lucene/util/AttributeSource.java

Modified: lucene/java/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/java/trunk/CHANGES.txt?rev=820658&r1=820657&r2=820658&view=diff
==============================================================================
--- lucene/java/trunk/CHANGES.txt (original)
+++ lucene/java/trunk/CHANGES.txt Thu Oct  1 13:49:46 2009
@@ -22,6 +22,9 @@
 
 New features
 
+* LUCENE-1933: Provide a convenience AttributeFactory that creates a
+  Token instance for all basic attributes.  (Uwe Schindler)
+
 Optimizations
 
 Documentation

Modified: lucene/java/trunk/contrib/analyzers/common/src/java/org/apache/lucene/analysis/miscellaneous/SingleTokenTokenStream.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/contrib/analyzers/common/src/java/org/apache/lucene/analysis/miscellaneous/SingleTokenTokenStream.java?rev=820658&r1=820657&r2=820658&view=diff
==============================================================================
--- lucene/java/trunk/contrib/analyzers/common/src/java/org/apache/lucene/analysis/miscellaneous/SingleTokenTokenStream.java
(original)
+++ lucene/java/trunk/contrib/analyzers/common/src/java/org/apache/lucene/analysis/miscellaneous/SingleTokenTokenStream.java
Thu Oct  1 13:49:46 2009
@@ -35,15 +35,8 @@
   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();

Modified: lucene/java/trunk/contrib/fast-vector-highlighter/src/test/org/apache/lucene/search/vectorhighlight/IndexTimeSynonymTest.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/contrib/fast-vector-highlighter/src/test/org/apache/lucene/search/vectorhighlight/IndexTimeSynonymTest.java?rev=820658&r1=820657&r2=820658&view=diff
==============================================================================
--- lucene/java/trunk/contrib/fast-vector-highlighter/src/test/org/apache/lucene/search/vectorhighlight/IndexTimeSynonymTest.java
(original)
+++ lucene/java/trunk/contrib/fast-vector-highlighter/src/test/org/apache/lucene/search/vectorhighlight/IndexTimeSynonymTest.java
Thu Oct  1 13:49:46 2009
@@ -25,8 +25,10 @@
 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 @@
       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;
     }
   }

Modified: lucene/java/trunk/contrib/instantiated/src/test/org/apache/lucene/store/instantiated/TestIndicesEquals.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/contrib/instantiated/src/test/org/apache/lucene/store/instantiated/TestIndicesEquals.java?rev=820658&r1=820657&r2=820658&view=diff
==============================================================================
--- lucene/java/trunk/contrib/instantiated/src/test/org/apache/lucene/store/instantiated/TestIndicesEquals.java
(original)
+++ lucene/java/trunk/contrib/instantiated/src/test/org/apache/lucene/store/instantiated/TestIndicesEquals.java
Thu Oct  1 13:49:46 2009
@@ -45,7 +45,7 @@
 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 @@
               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 @@
                   it = tokens.iterator();
                 }
               };
-              ts.addAttributeImpl(reusableToken);
               
               document.add(new Field("f", ts));
             }

Modified: lucene/java/trunk/src/java/org/apache/lucene/analysis/Token.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/analysis/Token.java?rev=820658&r1=820657&r2=820658&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/analysis/Token.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/analysis/Token.java Thu Oct  1 13:49:46 2009
@@ -27,6 +27,7 @@
 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 @@
       ((TypeAttribute) target).setType(type);
     }
   }
+
+  /** Convenience factory that returns <code>Token</code> as implementation for
the basic
+   * attributes and return the default impl (with &quot;Impl&quot; 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;
+    }
+  }
+
 }

Modified: lucene/java/trunk/src/java/org/apache/lucene/util/AttributeSource.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/util/AttributeSource.java?rev=820658&r1=820657&r2=820658&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/util/AttributeSource.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/util/AttributeSource.java Thu Oct  1 13:49:46
2009
@@ -172,7 +172,14 @@
   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;



Mime
View raw message