Author: aroush Date: Fri Nov 13 21:27:16 2009 New Revision: 836000 URL: http://svn.apache.org/viewvc?rev=836000&view=rev Log: Fixed NUnit test-cases for: TestAttributeSource
Modified: incubator/lucene.net/trunk/C#/src/Lucene.Net/Analysis/TeeSinkTokenFilter.cs incubator/lucene.net/trunk/C#/src/Lucene.Net/SupportClass.cs incubator/lucene.net/trunk/C#/src/Lucene.Net/Util/AttributeSource.cs incubator/lucene.net/trunk/C#/src/Test/Util/TestAttributeSource.cs Modified: incubator/lucene.net/trunk/C#/src/Lucene.Net/Analysis/TeeSinkTokenFilter.cs URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Lucene.Net/Analysis/TeeSinkTokenFilter.cs?rev=836000&r1=835999&r2=836000&view=diff ============================================================================== --- incubator/lucene.net/trunk/C#/src/Lucene.Net/Analysis/TeeSinkTokenFilter.cs (original) +++ incubator/lucene.net/trunk/C#/src/Lucene.Net/Analysis/TeeSinkTokenFilter.cs Fri Nov 13 21:27:16 2009 @@ -111,10 +111,10 @@ throw new System.ArgumentException("The supplied sink is not compatible to this tee"); } // add eventually missing attribute impls to the existing sink - for (System.Collections.IEnumerator it = this.CloneAttributes().GetAttributeImplsIterator(); it.MoveNext(); ) - { - sink.AddAttributeImpl((AttributeImpl) it.Current); - } + foreach (AttributeImpl impl in this.CloneAttributes().GetAttributeImplsIterator()) + { + sink.AddAttributeImpl(impl); + } this.sinks.Add(new System.WeakReference(sink)); } Modified: incubator/lucene.net/trunk/C#/src/Lucene.Net/SupportClass.cs URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Lucene.Net/SupportClass.cs?rev=836000&r1=835999&r2=836000&view=diff ============================================================================== --- incubator/lucene.net/trunk/C#/src/Lucene.Net/SupportClass.cs (original) +++ incubator/lucene.net/trunk/C#/src/Lucene.Net/SupportClass.cs Fri Nov 13 21:27:16 2009 @@ -1659,4 +1659,84 @@ return result; } } + + /// <summary>A collection of <typeparamref name="TItem"/> which can be + /// looked up by instances of <typeparamref name="TKey"/>.</summary> + /// <typeparam name="TItem">The type of the items contains in this + /// collection.</typeparam> + /// <typeparam name="TKey">The type of the keys that can be used to look + /// up the items.</typeparam> + internal class GeneralKeyedCollection<TKey, TItem> : System.Collections.ObjectModel.KeyedCollection<TKey, TItem> + { + /// <summary>Creates a new instance of the + /// <see cref="GeneralKeyedCollection"/> class.</summary> + /// <param name="converter">The <see cref="Converter{TInput, TOutput}"/> which will convert + /// instances of <typeparamref name="TItem"/> to <typeparamref name="TKey"/> + /// when the override of <see cref="GetKeyForItem(TItem)"/> is called.</param> + internal GeneralKeyedCollection(Converter<TItem, TKey> converter) : base() + { + // If the converter is null, throw an exception. + if (converter == null) throw new ArgumentNullException("converter"); + + // Store the converter. + this.converter = converter; + + // That's all folks. + return; + } + + /// <summary>The <see cref="Converter{TInput, TOutput}"/> which will convert + /// instances of <typeparamref name="TItem"/> to <typeparamref name="TKey"/> + /// when the override of <see cref="GetKeyForItem(TItem)"/> is called.</summary> + private readonly Converter<TItem, TKey> converter; + + /// <summary>Converts an item that is added to the collection to + /// a key.</summary> + /// <param name="item">The instance of <typeparamref name="TItem"/> + /// to convert into an instance of <typeparamref name="TKey"/>.</param> + /// <returns>The instance of <typeparamref name="TKey"/> which is the + /// key for this item.</returns> + protected override TKey GetKeyForItem(TItem item) + { + // The converter is not null. + System.Diagnostics.Debug.Assert(converter != null); + + // Call the converter. + return converter(item); + } + + /// <summary>Determines if a key for an item exists in this + /// collection.</summary> + /// <param name="key">The instance of <typeparamref name="TKey"/> + /// to see if it exists in this collection.</param> + /// <returns>True if the key exists in the collection, false otherwise.</returns> + public bool ContainsKey(TKey key) + { + // Call the dictionary - it is lazily created when the first item is added + if (Dictionary != null) + { + return Dictionary.ContainsKey(key); + } + else + { + return false; + } + } + } + + /// <summary> + /// A simple wrapper to allow for the use of the GeneralKeyedCollection. The + /// wrapper is required as there can be several keys for an object depending + /// on how many interfaces it implements. + /// </summary> + internal sealed class AttributeImplItem + { + internal AttributeImplItem(Type key, Lucene.Net.Util.AttributeImpl value) + { + this.Key = key; + this.Value = value; + } + internal Type Key; + internal Lucene.Net.Util.AttributeImpl Value; + } } Modified: incubator/lucene.net/trunk/C#/src/Lucene.Net/Util/AttributeSource.cs URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Lucene.Net/Util/AttributeSource.cs?rev=836000&r1=835999&r2=836000&view=diff ============================================================================== --- incubator/lucene.net/trunk/C#/src/Lucene.Net/Util/AttributeSource.cs (original) +++ incubator/lucene.net/trunk/C#/src/Lucene.Net/Util/AttributeSource.cs Fri Nov 13 21:27:16 2009 @@ -32,57 +32,6 @@ /// </summary> public class AttributeSource { - private class AnonymousClassIterator : System.Collections.IEnumerator - { - public AnonymousClassIterator(Lucene.Net.Util.AttributeSource.State initState, AttributeSource enclosingInstance) - { - InitBlock(initState, enclosingInstance); - } - private void InitBlock(Lucene.Net.Util.AttributeSource.State initState, AttributeSource enclosingInstance) - { - this.initState = initState; - this.enclosingInstance = enclosingInstance; - state = initState; - } - private Lucene.Net.Util.AttributeSource.State initState; - private AttributeSource enclosingInstance; - public virtual System.Object Current - { - get - { - if (state == null) - throw new System.ArgumentOutOfRangeException(); - AttributeImpl att = state.attribute; - state = state.next; - return att; - } - - } - public AttributeSource Enclosing_Instance - { - get - { - return enclosingInstance; - } - - } - private State state; - - public virtual void Remove() - { - throw new System.NotSupportedException(); - } - - public virtual bool MoveNext() - { - return state != null; - } - - virtual public void Reset() - { - System.Diagnostics.Debug.Fail("Port issue:", "Lets see if we need this"); // {{Aroush-2.9}} - } - } /// <summary> An AttributeFactory creates instances of {...@link AttributeImpl}s.</summary> public abstract class AttributeFactory { @@ -156,8 +105,8 @@ // These two maps must always be in sync!!! // So they are private, final and read-only from the outside (read-only iterators) - private System.Collections.IDictionary attributes; - private System.Collections.Hashtable attributeImpls; + private SupportClass.GeneralKeyedCollection<Type, SupportClass.AttributeImplItem> attributes; + private SupportClass.GeneralKeyedCollection<Type, SupportClass.AttributeImplItem> attributeImpls; private AttributeFactory factory; @@ -181,8 +130,8 @@ /// <summary> An AttributeSource using the supplied {...@link AttributeFactory} for creating new {...@link Attribute} instances.</summary> public AttributeSource(AttributeFactory factory) { - this.attributes = new System.Collections.Hashtable(); // {{Aroush-2.9}} Port issue; in Java, this is java.util.LinkedHashMap - this.attributeImpls = new System.Collections.Hashtable(); // {{Aroush-2.9}} Port issue; in Java, this is java.util.LinkedHashMap + this.attributes = new SupportClass.GeneralKeyedCollection<Type, SupportClass.AttributeImplItem>(delegate(SupportClass.AttributeImplItem att) { return att.Key; }); + this.attributeImpls = new SupportClass.GeneralKeyedCollection<Type, SupportClass.AttributeImplItem>(delegate(SupportClass.AttributeImplItem att) { return att.Key; }); this.factory = factory; } @@ -195,10 +144,16 @@ /// <summary>Returns a new iterator that iterates the attribute classes /// in the same order they were added in. /// <p>Signature for Java 1.5: <code>public Iterator<Class<? extends Attribute>> getAttributeClassesIterator()</code> + /// + /// Note that this return value is different from Java in that it enumerates over the values + /// and not the keys /// </summary> - public virtual System.Collections.IEnumerator GetAttributeClassesIterator() + public virtual System.Collections.Generic.IEnumerable<Type> GetAttributeClassesIterator() { - return (new System.Collections.Hashtable(attributes)).GetEnumerator(); + foreach (SupportClass.AttributeImplItem item in this.attributes) + { + yield return item.Key; + } } /// <summary>Returns a new iterator that iterates all unique Attribute implementations. @@ -206,7 +161,7 @@ /// if one instance implements more than one Attribute interface. /// <p>Signature for Java 1.5: <code>public Iterator<AttributeImpl> getAttributeImplsIterator()</code> /// </summary> - public virtual System.Collections.IEnumerator GetAttributeImplsIterator() + public virtual System.Collections.Generic.IEnumerable<AttributeImpl> GetAttributeImplsIterator() { if (HasAttributes()) { @@ -214,12 +169,12 @@ { ComputeCurrentState(); } - State initState = currentState; - return new AnonymousClassIterator(initState, this); - } - else - { - return ((System.Collections.Hashtable) new System.Collections.Hashtable()).GetEnumerator(); + while (currentState != null) + { + AttributeImpl att = currentState.attribute; + currentState = currentState.next; + yield return att; + } } } @@ -276,12 +231,15 @@ { System.Type curInterface = (System.Type) it.Current; // Attribute is a superclass of this interface - if (!attributes.Contains(curInterface)) + if (!attributes.ContainsKey(curInterface)) { // invalidate state to force recomputation in captureState() this.currentState = null; - attributes[curInterface] = att; - attributeImpls[clazz] = att; + attributes.Add(new SupportClass.AttributeImplItem(curInterface, att)); + if (!attributeImpls.ContainsKey(clazz)) + { + attributeImpls.Add(new SupportClass.AttributeImplItem(clazz, att)); + } } } } @@ -294,8 +252,7 @@ /// </summary> public virtual Attribute AddAttribute(System.Type attClass) { - Attribute att = (Attribute) attributes[attClass]; - if (att == null) + if (!attributes.ContainsKey(attClass)) { AttributeImpl attImpl = this.factory.CreateAttributeInstance(attClass); AddAttributeImpl(attImpl); @@ -303,7 +260,7 @@ } else { - return att; + return attributes[attClass].Value; } } @@ -336,13 +293,14 @@ /// </summary> public virtual Attribute GetAttribute(System.Type attClass) { - Attribute att = (Attribute) this.attributes[attClass]; - if (att == null) - { - throw new System.ArgumentException("This AttributeSource does not have the attribute '" + attClass.FullName + "'."); - } - - return att; + if (!this.attributes.ContainsKey(attClass)) + { + throw new System.ArgumentException("This AttributeSource does not have the attribute '" + attClass.FullName + "'."); + } + else + { + return this.attributes[attClass].Value; + } } /// <summary> This class holds the state of an AttributeSource.</summary> @@ -375,14 +333,14 @@ { currentState = new State(); State c = currentState; - System.Collections.IEnumerator it = attributeImpls.GetEnumerator(); - if (it.MoveNext()) - c.attribute = (AttributeImpl) ((System.Collections.DictionaryEntry) it.Current).Value; + System.Collections.Generic.IEnumerator<SupportClass.AttributeImplItem> it = attributeImpls.GetEnumerator(); + if (it.MoveNext()) + c.attribute = it.Current.Value; while (it.MoveNext()) { c.next = new State(); c = c.next; - c.attribute = (AttributeImpl) ((System.Collections.DictionaryEntry) it.Current).Value; + c.attribute = it.Current.Value; } } @@ -442,10 +400,11 @@ do { - AttributeImpl targetImpl = (AttributeImpl) attributeImpls[state.attribute.GetType()]; - if (targetImpl == null) + if (!attributeImpls.ContainsKey(state.attribute.GetType())) + { throw new System.ArgumentException("State contains an AttributeImpl that is not in this AttributeSource"); - state.attribute.CopyTo(targetImpl); + } + state.attribute.CopyTo(attributeImpls[state.attribute.GetType()].Value); state = state.next; } while (state != null); @@ -562,16 +521,15 @@ } for (State state = currentState; state != null; state = state.next) { - clone.attributeImpls[state.attribute.GetType()] = state.attribute.Clone(); + AttributeImpl impl = (AttributeImpl) state.attribute.Clone(); + clone.attributeImpls.Add(new SupportClass.AttributeImplItem(impl.GetType(), impl)); } } // now the interfaces - System.Collections.IEnumerator attIt = new System.Collections.Hashtable(this.attributes).GetEnumerator(); - while (attIt.MoveNext()) + foreach (SupportClass.AttributeImplItem att in this.attributes) { - System.Collections.DictionaryEntry entry = (System.Collections.DictionaryEntry) attIt.Current; - clone.attributes[entry.Key] = clone.attributeImpls[entry.Value.GetType()]; + clone.attributes.Add(new SupportClass.AttributeImplItem(att.Key, clone.attributeImpls[att.Value.GetType()].Value)); } return clone; Modified: incubator/lucene.net/trunk/C#/src/Test/Util/TestAttributeSource.cs URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/C%23/src/Test/Util/TestAttributeSource.cs?rev=836000&r1=835999&r2=836000&view=diff ============================================================================== --- incubator/lucene.net/trunk/C#/src/Test/Util/TestAttributeSource.cs (original) +++ incubator/lucene.net/trunk/C#/src/Test/Util/TestAttributeSource.cs Fri Nov 13 21:27:16 2009 @@ -96,15 +96,17 @@ typeAtt.SetType("TestType"); AttributeSource clone = src.CloneAttributes(); - System.Collections.IEnumerator it = clone.GetAttributeClassesIterator(); + System.Collections.IEnumerator it = clone.GetAttributeClassesIterator().GetEnumerator(); + Assert.IsTrue(it.MoveNext()); Assert.AreEqual(typeof(TermAttribute), it.Current, "TermAttribute must be the first attribute"); - Assert.AreEqual(typeof(TypeAttribute), it.Current, "TypeAttribute must be the second attribute"); + Assert.IsTrue(it.MoveNext()); + Assert.AreEqual(typeof(TypeAttribute), it.Current, "TypeAttribute must be the second attribute"); Assert.IsFalse(it.MoveNext(), "No more attributes"); - TermAttribute termAtt2 = (TermAttribute) clone.GetAttribute(typeof(TermAttribute)); - TypeAttribute typeAtt2 = (TypeAttribute) clone.GetAttribute(typeof(TypeAttribute)); - Assert.AreNotEqual(termAtt2, termAtt, "TermAttribute of original and clone must be different instances"); - Assert.AreNotEqual(typeAtt2, typeAtt, "TypeAttribute of original and clone must be different instances"); + TermAttribute termAtt2 = (TermAttribute)clone.GetAttribute(typeof(TermAttribute)); + TypeAttribute typeAtt2 = (TypeAttribute)clone.GetAttribute(typeof(TypeAttribute)); + Assert.IsFalse(ReferenceEquals(termAtt2, termAtt), "TermAttribute of original and clone must be different instances"); + Assert.IsFalse(ReferenceEquals(typeAtt2, typeAtt), "TypeAttribute of original and clone must be different instances"); Assert.AreEqual(termAtt2, termAtt, "TermAttribute of original and clone must be equal"); Assert.AreEqual(typeAtt2, typeAtt, "TypeAttribute of original and clone must be equal"); } @@ -118,7 +120,7 @@ termAtt.SetTermBuffer("TestTerm"); typeAtt.SetType("TestType"); Assert.AreEqual("(" + termAtt.ToString() + "," + typeAtt.ToString() + ")", src.ToString(), "Attributes should appear in original order"); - System.Collections.IEnumerator it = src.GetAttributeImplsIterator(); + System.Collections.Generic.IEnumerator<AttributeImpl> it = src.GetAttributeImplsIterator().GetEnumerator(); Assert.IsTrue(it.MoveNext(), "Iterator should have 2 attributes left"); Assert.AreSame(termAtt, it.Current, "First AttributeImpl from iterator should be termAtt"); Assert.IsTrue(it.MoveNext(), "Iterator should have 1 attributes left"); @@ -131,8 +133,9 @@ termAtt = (TermAttribute) src.AddAttribute(typeof(TermAttribute)); Assert.IsTrue(termAtt is Token, "TermAttribute should be implemented by Token"); // get the Token attribute and check, that it is the only one - it = src.GetAttributeImplsIterator(); - Token tok = (Token) it.Current; + it = src.GetAttributeImplsIterator().GetEnumerator(); + Assert.IsTrue(it.MoveNext()); + Token tok = (Token)it.Current; Assert.IsFalse(it.MoveNext(), "There should be only one attribute implementation instance"); termAtt.SetTermBuffer("TestTerm");