lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From uschind...@apache.org
Subject svn commit: r922797 - in /lucene/java/trunk: CHANGES.txt src/java/org/apache/lucene/util/AttributeSource.java src/test/org/apache/lucene/util/TestAttributeSource.java
Date Sun, 14 Mar 2010 09:09:36 GMT
Author: uschindler
Date: Sun Mar 14 09:09:36 2010
New Revision: 922797

URL: http://svn.apache.org/viewvc?rev=922797&view=rev
Log:
LUCENE-2314: Add AttributeSource.copyTo(AttributeSource)

Modified:
    lucene/java/trunk/CHANGES.txt
    lucene/java/trunk/src/java/org/apache/lucene/util/AttributeSource.java
    lucene/java/trunk/src/test/org/apache/lucene/util/TestAttributeSource.java

Modified: lucene/java/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/java/trunk/CHANGES.txt?rev=922797&r1=922796&r2=922797&view=diff
==============================================================================
--- lucene/java/trunk/CHANGES.txt (original)
+++ lucene/java/trunk/CHANGES.txt Sun Mar 14 09:09:36 2010
@@ -167,6 +167,11 @@ New features
 * LUCENE-2247: Added a CharArrayMap<V> for performance improvements
   in some stemmers and synonym filters. (Uwe Schindler)
 
+* LUCENE-2314: Added AttributeSource.copyTo(AttributeSource) that
+  allows to use cloneAttributes() and this method as a replacement
+  for captureState()/restoreState(), if the state itsself
+  needs to be inspected/modified.  (Uwe Schindler)
+
 Optimizations
 
 * LUCENE-2075: Terms dict cache is now shared across threads instead

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=922797&r1=922796&r2=922797&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 Sun Mar 14 09:09:36
2010
@@ -365,8 +365,10 @@ public class AttributeSource {
     
     do {
       AttributeImpl targetImpl = attributeImpls.get(state.attribute.getClass());
-      if (targetImpl == null)
-        throw new IllegalArgumentException("State contains an AttributeImpl that is not in
this AttributeSource");
+      if (targetImpl == null) {
+        throw new IllegalArgumentException("State contains AttributeImpl of type " +
+          state.attribute.getClass() + " that is not in in this AttributeSource");
+      }
       state.attribute.copyTo(targetImpl);
       state = state.next;
     } while (state != null);
@@ -446,28 +448,54 @@ public class AttributeSource {
   
   /**
    * Performs a clone of all {@link 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 {@link #AttributeSource(AttributeSource)})
+   * {@code AttributeSource} instance. This method can be used to e.g. create another TokenStream
+   * with exactly the same attributes (using {@link #AttributeSource(AttributeSource)}).
+   * You can also use it as a (non-performant) replacement for {@link #captureState}, if
you need to look
+   * into / modify the captured state.
    */
   public AttributeSource cloneAttributes() {
-    AttributeSource clone = new AttributeSource(this.factory);
+    final AttributeSource clone = new AttributeSource(this.factory);
     
-    // first clone the impls
     if (hasAttributes()) {
+      // first clone the impls
       if (currentState == null) {
         computeCurrentState();
       }
       for (State state = currentState; state != null; state = state.next) {
         clone.attributeImpls.put(state.attribute.getClass(), (AttributeImpl) state.attribute.clone());
       }
-    }
-    
-    // now the interfaces
-    for (Entry<Class<? extends Attribute>, AttributeImpl> entry : this.attributes.entrySet())
{
-      clone.attributes.put(entry.getKey(), clone.attributeImpls.get(entry.getValue().getClass()));
+      
+      // now the interfaces
+      for (Entry<Class<? extends Attribute>, AttributeImpl> entry : this.attributes.entrySet())
{
+        clone.attributes.put(entry.getKey(), clone.attributeImpls.get(entry.getValue().getClass()));
+      }
     }
     
     return clone;
   }
+  
+  /**
+   * Copies the contents of this {@code AttributeSource} to the given target {@code AttributeSource}.
+   * The given instance has to provide all {@link Attribute}s this instance contains. 
+   * The actual attribute implementations must be identical in both {@code AttributeSource}
instances;
+   * ideally both AttributeSource instances should use the same {@link AttributeFactory}.
+   * You can use this method as a replacement for {@link #restoreState}, if you use
+   * {@link #cloneAttributes} instead of {@link #captureState}.
+   */
+  public final void copyTo(AttributeSource target) {
+    if (hasAttributes()) {
+      if (currentState == null) {
+        computeCurrentState();
+      }
+      for (State state = currentState; state != null; state = state.next) {
+        final AttributeImpl targetImpl = target.attributeImpls.get(state.attribute.getClass());
+        if (targetImpl == null) {
+          throw new IllegalArgumentException("This AttributeSource contains AttributeImpl
of type " +
+            state.attribute.getClass() + " that is not in the target");
+        }
+        state.attribute.copyTo(targetImpl);
+      }
+    }
+  }
 
 }

Modified: lucene/java/trunk/src/test/org/apache/lucene/util/TestAttributeSource.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/util/TestAttributeSource.java?rev=922797&r1=922796&r2=922797&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/util/TestAttributeSource.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/util/TestAttributeSource.java Sun Mar 14
09:09:36 2010
@@ -95,6 +95,18 @@ public class TestAttributeSource extends
     assertNotSame("TypeAttribute of original and clone must be different instances", typeAtt2,
typeAtt);
     assertEquals("TermAttribute of original and clone must be equal", termAtt2, termAtt);
     assertEquals("TypeAttribute of original and clone must be equal", typeAtt2, typeAtt);
+    
+    // test copy back
+    termAtt2.setTermBuffer("OtherTerm");
+    typeAtt2.setType("OtherType");
+    clone.copyTo(src);
+    assertEquals("TermAttribute of original must now contain updated term", "OtherTerm",
termAtt.term());
+    assertEquals("TypeAttribute of original must now contain updated type", "OtherType",
typeAtt.type());
+    // verify again:
+    assertNotSame("TermAttribute of original and clone must be different instances", termAtt2,
termAtt);
+    assertNotSame("TypeAttribute of original and clone must be different instances", typeAtt2,
typeAtt);
+    assertEquals("TermAttribute of original and clone must be equal", termAtt2, termAtt);
+    assertEquals("TypeAttribute of original and clone must be equal", typeAtt2, typeAtt);
   }
   
   public void testToStringAndMultiAttributeImplementations() {



Mime
View raw message