Index: src/java/org/apache/lucene/util/AttributeSource.java =================================================================== --- src/java/org/apache/lucene/util/AttributeSource.java (revision 922531) +++ src/java/org/apache/lucene/util/AttributeSource.java (working copy) @@ -469,5 +469,24 @@ return clone; } + + /** + * Copies the contents of this AttributeSource to the given target. This only works + * if the target uses the same {@link AttributeFactory} and contains at least the + * same {@link AttributeImpl}s this AttributeSource provides. + */ + public final void copyTo(AttributeSource target) { + if (hasAttributes()) { + if (currentState == null) { + computeCurrentState(); + } + for (State state = currentState; state != null; state = state.next) { + AttributeImpl targetImpl = target.attributeImpls.get(state.attribute.getClass()); + if (targetImpl == null) + throw new IllegalArgumentException("This AttributeSource contains an AttributeImpl that is not in the target"); + state.attribute.copyTo(targetImpl); + }; + } + } } Index: src/test/org/apache/lucene/util/TestAttributeSource.java =================================================================== --- src/test/org/apache/lucene/util/TestAttributeSource.java (revision 922531) +++ src/test/org/apache/lucene/util/TestAttributeSource.java (working copy) @@ -95,6 +95,18 @@ 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() {