Example #1
0
        /// <summary> 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)})
        /// </summary>
        public virtual AttributeSource CloneAttributes()
        {
            AttributeSource clone = new AttributeSource(this.factory);

            // first clone the impls
            if (HasAttributes())
            {
                if (currentState == null)
                {
                    ComputeCurrentState();
                }
                for (State state = currentState; state != null; state = state.next)
                {
                    AttributeImpl impl = (AttributeImpl)state.attribute.Clone();
                    clone.attributeImpls.Add(new SupportClass.AttributeImplItem(impl.GetType(), impl));
                }
            }

            // now the interfaces
            foreach (SupportClass.AttributeImplItem att in this.attributes)
            {
                clone.attributes.Add(new SupportClass.AttributeImplItem(att.Key, clone.attributeImpls[att.Value.GetType()].Value));
            }

            return(clone);
        }
	  public SingleTokenTokenStream(Token token) : base(Token.TOKEN_ATTRIBUTE_FACTORY)
	  {

		Debug.Assert(token != null);
		this.singleToken = token.Clone();

        tokenAtt = AddAttribute <ICharTermAttribute>();
		Debug.Assert(tokenAtt is Token);
	  }
Example #3
0
        // {{Aroush-2.9 Port issue, need to mimic java's IdentityHashMap

        /*
         * From Java docs:
         * This class implements the Map interface with a hash table, using
         * reference-equality in place of object-equality when comparing keys
         * (and values). In other words, in an IdentityHashMap, two keys k1 and k2
         * are considered equal if and only if (k1==k2). (In normal Map
         * implementations (like HashMap) two keys k1 and k2 are considered
         * equal if and only if (k1==null ? k2==null : k1.equals(k2)).)
         */
        // Aroush-2.9}}

        /// <summary>Adds a custom AttributeImpl instance with one or more Attribute interfaces. </summary>
        public virtual void  AddAttributeImpl(AttributeImpl att)
        {
            System.Type clazz = att.GetType();
            if (attributeImpls.Contains(clazz))
            {
                return;
            }
            System.Collections.ArrayList foundInterfaces;
            lock (knownImplClasses)
            {
                foundInterfaces = (System.Collections.ArrayList)knownImplClasses[clazz];
                if (foundInterfaces == null)
                {
                    // we have a strong reference to the class instance holding all interfaces in the list (parameter "att"),
                    // so all WeakReferences are never evicted by GC
                    knownImplClasses.Add(clazz, foundInterfaces = new System.Collections.ArrayList());
                    // find all interfaces that this attribute instance implements
                    // and that extend the Attribute interface
                    System.Type actClazz = clazz;
                    do
                    {
                        System.Type[] interfaces = actClazz.GetInterfaces();
                        for (int i = 0; i < interfaces.Length; i++)
                        {
                            System.Type curInterface = interfaces[i];
                            if (curInterface != typeof(Attribute) && typeof(Attribute).IsAssignableFrom(curInterface))
                            {
                                foundInterfaces.Add(new WeakReference(curInterface));
                            }
                        }
                        actClazz = actClazz.BaseType;
                    }while (actClazz != null);
                }
            }

            // add all interfaces of this AttributeImpl to the maps
            for (System.Collections.IEnumerator it = foundInterfaces.GetEnumerator(); it.MoveNext();)
            {
                WeakReference curInterfaceRef = (WeakReference)it.Current;
                System.Type   curInterface    = (System.Type)curInterfaceRef.Target;
                System.Diagnostics.Debug.Assert(curInterface != null, "We have a strong reference on the class holding the interfaces, so they should never get evicted");
                // Attribute is a superclass of this interface
                if (!attributes.ContainsKey(curInterface))
                {
                    // invalidate state to force recomputation in captureState()
                    this.currentState = null;
                    attributes.Add(new SupportClass.AttributeImplItem(curInterface, att));
                    if (!attributeImpls.ContainsKey(clazz))
                    {
                        attributeImpls.Add(new SupportClass.AttributeImplItem(clazz, att));
                    }
                }
            }
        }
        public SingleTokenTokenStream(Token token)
        {
            Debug.Assert(token != null, "Token was null!");
            _singleToken = (Token) token.Clone();

            // ReSharper disable DoNotCallOverridableMethodsInConstructor
            _tokenAtt = (AttributeImpl) AddAttribute(typeof (TermAttribute));
            // ReSharper restore DoNotCallOverridableMethodsInConstructor

            Debug.Assert(_tokenAtt is Token || _tokenAtt.GetType().Name.Equals(typeof (TokenWrapper).Name),
                         "Token Attribute is the wrong type! Type was: " + _tokenAtt.GetType().Name + " but expected " +
                         typeof (TokenWrapper).Name);
        }
Example #5
0
        /// <summary>
        /// Shallow clone. Subclasses must override this if they
        /// need to clone any members deeply,
        /// </summary>
        public object Clone()
        {
            AttributeImpl clone = null;

            try
            {
                clone = (AttributeImpl)base.MemberwiseClone();
            }
            catch (Exception)
            {
                throw new Exception("Clone not supported");   // shouldn't happen
            }
            return(clone);
        }
Example #6
0
 /// <summary>Returns a new iterator that iterates all unique Attribute implementations.
 /// This iterator may contain less entries that {@link #getAttributeClassesIterator},
 /// if one instance implements more than one Attribute interface.
 /// Signature for Java 1.5: <code>public Iterator&lt;AttributeImpl&gt; getAttributeImplsIterator()</code>
 /// </summary>
 public virtual System.Collections.Generic.IEnumerable <AttributeImpl> GetAttributeImplsIterator()
 {
     if (HasAttributes())
     {
         if (currentState == null)
         {
             ComputeCurrentState();
         }
         while (currentState != null)
         {
             AttributeImpl att = currentState.attribute;
             currentState = currentState.next;
             yield return(att);
         }
     }
 }
Example #7
0
        /// <summary> The caller must pass in a Class&lt;? extends Attribute&gt; value.
        /// This method first checks if an instance of that class is
        /// already in this AttributeSource and returns it. Otherwise a
        /// new instance is created, added to this AttributeSource and returned.
        /// Signature for Java 1.5: <code>public &lt;T extends Attribute&gt; T addAttribute(Class&lt;T&gt;)</code>
        /// </summary>
        public virtual Attribute AddAttribute(System.Type attClass)
        {
            if (!attributes.ContainsKey(attClass))
            {
                if (!(attClass.IsInterface && typeof(Attribute).IsAssignableFrom(attClass)))
                {
                    throw new ArgumentException(
                              "AddAttribute() only accepts an interface that extends Attribute, but " +
                              attClass.FullName + " does not fulfil this contract."
                              );
                }

                AttributeImpl attImpl = this.factory.CreateAttributeInstance(attClass);
                AddAttributeImpl(attImpl);
                return(attImpl);
            }
            else
            {
                return(attributes[attClass].Value);
            }
        }
Example #8
0
 /// <summary> Copies the values from this Attribute into the passed-in
 /// target attribute. The target implementation must support all the
 /// Attributes this implementation supports.
 /// </summary>
 public abstract void  CopyTo(AttributeImpl target);
Example #9
0
		/// <summary> Copies the values from this Attribute into the passed-in
		/// target attribute. The target implementation must support all the
		/// Attributes this implementation supports.
		/// </summary>
		public abstract void  CopyTo(AttributeImpl target);
 public override void CopyTo(AttributeImpl target) {
     target.Clear();
 }
Example #11
0
        // {{Aroush-2.9 Port issue, need to mimic java's IdentityHashMap
        /*
         * From Java docs:
         * This class implements the Map interface with a hash table, using 
         * reference-equality in place of object-equality when comparing keys 
         * (and values). In other words, in an IdentityHashMap, two keys k1 and k2 
         * are considered equal if and only if (k1==k2). (In normal Map 
         * implementations (like HashMap) two keys k1 and k2 are considered 
         * equal if and only if (k1==null ? k2==null : k1.equals(k2)).) 
         */
        // Aroush-2.9}}
		
		/// <summary>Adds a custom AttributeImpl instance with one or more Attribute interfaces. </summary>
		public virtual void  AddAttributeImpl(AttributeImpl att)
		{
			System.Type clazz = att.GetType();
			if (attributeImpls.Contains(clazz))
				return ;
			System.Collections.ArrayList foundInterfaces;
			lock (knownImplClasses)
			{
				foundInterfaces = (System.Collections.ArrayList) knownImplClasses[clazz];
				if (foundInterfaces == null)
				{
                    // we have a strong reference to the class instance holding all interfaces in the list (parameter "att"),
                    // so all WeakReferences are never evicted by GC
					knownImplClasses.Add(clazz, foundInterfaces = new System.Collections.ArrayList());
					// find all interfaces that this attribute instance implements
					// and that extend the Attribute interface
					System.Type actClazz = clazz;
					do 
					{
						System.Type[] interfaces = actClazz.GetInterfaces();
						for (int i = 0; i < interfaces.Length; i++)
						{
							System.Type curInterface = interfaces[i];
							if (curInterface != typeof(Attribute) && typeof(Attribute).IsAssignableFrom(curInterface))
							{
								foundInterfaces.Add(new WeakReference(curInterface));
							}
						}
						actClazz = actClazz.BaseType;
					}
					while (actClazz != null);
				}
			}
			
			// add all interfaces of this AttributeImpl to the maps
			for (System.Collections.IEnumerator it = foundInterfaces.GetEnumerator(); it.MoveNext(); )
			{
                WeakReference curInterfaceRef = (WeakReference)it.Current;
				System.Type curInterface = (System.Type) curInterfaceRef.Target;
                System.Diagnostics.Debug.Assert(curInterface != null,"We have a strong reference on the class holding the interfaces, so they should never get evicted");
				// Attribute is a superclass of this interface
				if (!attributes.ContainsKey(curInterface))
				{
					// invalidate state to force recomputation in captureState()
					this.currentState = null;
                    attributes.Add(new SupportClass.AttributeImplItem(curInterface, att));
                    if (!attributeImpls.ContainsKey(clazz))
                    {
                        attributeImpls.Add(new SupportClass.AttributeImplItem(clazz, att));
                    }
				}
			}
		}
 public override void CopyTo(AttributeImpl target)
 {
     // this makes no sense for us, because our state is per-docsenum.
     // we don't want to copy any stuff over to another docsenum ever!
 }
Example #13
0
 public AttributeReflectorAnonymousInnerClassHelper(AttributeImpl outerInstance, bool prependAttClass, StringBuilder buffer)
 {
     this.OuterInstance   = outerInstance;
     this.PrependAttClass = prependAttClass;
     this.Buffer          = buffer;
 }
Example #14
0
        // {{Aroush-2.9 Port issue, need to mimic java's IdentityHashMap
        /*
         * From Java docs:
         * This class implements the Map interface with a hash table, using
         * reference-equality in place of object-equality when comparing keys
         * (and values). In other words, in an IdentityHashMap, two keys k1 and k2
         * are considered equal if and only if (k1==k2). (In normal Map
         * implementations (like HashMap) two keys k1 and k2 are considered
         * equal if and only if (k1==null ? k2==null : k1.equals(k2)).)
         */
        // Aroush-2.9}}
        /// <summary>Adds a custom AttributeImpl instance with one or more Attribute interfaces. </summary>
        public virtual void AddAttributeImpl(AttributeImpl att)
        {
            System.Type clazz = att.GetType();
            if (attributeImpls.Contains(clazz))
                return ;
            System.Collections.ArrayList foundInterfaces;
            lock (knownImplClasses)
            {
                foundInterfaces = (System.Collections.ArrayList) knownImplClasses[clazz];
                if (foundInterfaces == null)
                {
                    knownImplClasses.Add(clazz, foundInterfaces = new System.Collections.ArrayList());
                    // find all interfaces that this attribute instance implements
                    // and that extend the Attribute interface
                    System.Type actClazz = clazz;
                    do
                    {
                        System.Type[] interfaces = actClazz.GetInterfaces();
                        for (int i = 0; i < interfaces.Length; i++)
                        {
                            System.Type curInterface = interfaces[i];
                            if (curInterface != typeof(Attribute) && typeof(Attribute).IsAssignableFrom(curInterface))
                            {
                                foundInterfaces.Add(curInterface);
                            }
                        }
                        actClazz = actClazz.BaseType;
                    }
                    while (actClazz != null);
                }
            }

            // add all interfaces of this AttributeImpl to the maps
            for (System.Collections.IEnumerator it = foundInterfaces.GetEnumerator(); it.MoveNext(); )
            {
                System.Type curInterface = (System.Type) it.Current;
                // Attribute is a superclass of this interface
                if (!attributes.ContainsKey(curInterface))
                {
                    // invalidate state to force recomputation in captureState()
                    this.currentState = null;
                    attributes.Add(new SupportClass.AttributeImplItem(curInterface, att));
                    if (!attributeImpls.ContainsKey(clazz))
                    {
                        attributeImpls.Add(new SupportClass.AttributeImplItem(clazz, att));
                    }
                }
            }
        }