Logo Search packages:      
Sourcecode: gdata-sharp version File versions  Download package

collections.cs

/* Copyright (c) 2006-2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
/* Change history
* Oct 13 2008  Joe Feser       joseph.feser@gmail.com
* Converted ArrayLists and other .NET 1.1 collections to use Generics
* Combined IExtensionElement and IExtensionElementFactory interfaces
* 
*/
#region Using directives

#define USE_TRACING

using System;
using System.Collections.Generic;
using System.Collections;

#endregion

//////////////////////////////////////////////////////////////////////
// contains typed collections based on the 1.1 .NET framework
// using typed collections has the benefit of additional code reliability
// and using them in the collection editor
// 
//////////////////////////////////////////////////////////////////////
namespace Google.GData.Client
{
    //////////////////////////////////////////////////////////////////////
    /// <summary>standard typed collection based on 1.1 framework for FeedEntries
    /// </summary> 
    //////////////////////////////////////////////////////////////////////
00043     public class AtomEntryCollection : AtomCollectionBase<AtomEntry>
    {
        /// <summary>holds the owning feed</summary> 
00046         private AtomFeed feed;

        /// <summary>private default constructor</summary> 
00049         private AtomEntryCollection()
        {
        }
        /// <summary>constructor</summary> 
00053         public AtomEntryCollection(AtomFeed feed)
            : base()
        {
            this.feed = feed;
        }

        /// <summary>Fins an atomEntry in the collection 
        /// based on it's ID. </summary> 
        /// <param name="value">The atomId to look for</param> 
        /// <returns>Null if not found, otherwise the entry</returns>
00063         public AtomEntry FindById(AtomId value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            foreach (AtomEntry entry in List)
            {
                if (entry.Id.AbsoluteUri == value.AbsoluteUri)
                {
                    return entry;
                }
            }
            return null;
        }


        /// <summary>standard typed accessor method </summary> 
        public override AtomEntry this[int index]
00082         {
            get
            {
                return ((AtomEntry)List[index]);
            }
            set
            {
                if (value != null)
                {
                    if (value.Feed == null || value.Feed != this.feed)
                    {
                        value.setFeed(this.feed);
                    }
                }
                List[index] = value;
            }
        }

        /// <summary>standard typed add method </summary> 
00101         public override void Add(AtomEntry value)
        {
            if (value != null)
            {
                if (this.feed != null && value.Feed == this.feed)
                {
                    // same object, already in here. 
                    throw new ArgumentException("The entry is already part of this collection");
                }
                value.setFeed(this.feed);
                // old code
                /*
                // now we need to see if this is the same feed. If not, copy
                if (AtomFeed.IsFeedIdentical(value.Feed, this.feed) == false)
                {
                    AtomEntry newEntry = AtomEntry.ImportFromFeed(value);
                    newEntry.setFeed(this.feed);
                    value = newEntry;
                }
                */
                // from now on, we will only ADD the entry to this collection and change it's 
                // ownership. No more auto-souce creation. There is an explicit method for this
                value.ProtocolMajor = this.feed.ProtocolMajor;
                value.ProtocolMinor = this.feed.ProtocolMinor;
            }
            base.Add(value);
        }

        /// <summary>
        /// takes an existing atomentry object and either copies it into this feed collection,
        /// or moves it by creating a source element and copying it in here if the value is actually
        /// already part of a collection
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
00136         public AtomEntry CopyOrMove(AtomEntry value)
        {
            if (value != null)
            {
                if (value.Feed == null)
                {
                    value.setFeed(this.feed);
                }
                else
                {
                    if (this.feed != null && value.Feed == this.feed)
                    {
                        // same object, already in here. 
                        throw new ArgumentException("The entry is already part of this collection");
                    }
                    // now we need to see if this is the same feed. If not, copy
                    if (AtomFeed.IsFeedIdentical(value.Feed, this.feed) == false)
                    {
                        AtomEntry newEntry = AtomEntry.ImportFromFeed(value);
                        newEntry.setFeed(this.feed);
                        value = newEntry;
                    }
                }
                value.ProtocolMajor = this.feed.ProtocolMajor;
                value.ProtocolMinor = this.feed.ProtocolMinor;
            }
            base.Add(value);
            return value; 
        }
    }
    /////////////////////////////////////////////////////////////////////////////

    //////////////////////////////////////////////////////////////////////
    /// <summary>standard typed collection based on 1.1 framework for AtomLinks
    /// </summary> 
    //////////////////////////////////////////////////////////////////////
00172     public class AtomLinkCollection : AtomCollectionBase<AtomLink>
    {
        /// <summary>standard typed accessor method </summary> 
00175         public override void Add(AtomLink value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            // Remove link with same relation to avoid duplication.
            AtomLink oldLink = FindService(value.Rel, value.Type);
            if (oldLink != null)
            {
                Remove(oldLink);
            }
            base.Add(value);
        }
       
        //////////////////////////////////////////////////////////////////////
        /// <summary>public AtomLink FindService(string service,string type)
        ///   Retrieves the first link with the supplied 'rel' and/or 'type' value.
        ///   If either parameter is null, the corresponding match isn't needed.
        /// </summary> 
        /// <param name="service">the service entry to find</param>
        /// <param name="type">the link type to find</param>
        /// <returns>the found link or NULL </returns>
        //////////////////////////////////////////////////////////////////////
00199         public AtomLink FindService(string service, string type)
        {
            foreach (AtomLink link in List)
            {
                string linkRel = link.Rel;
                string linkType = link.Type;

                if ((service == null || (linkRel != null && linkRel == service)) &&
                    (type == null || (linkType != null && linkType == type)))
                {

                    return link;
                }
            }
            return null;
        }
        /////////////////////////////////////////////////////////////////////////////

        //////////////////////////////////////////////////////////////////////
        /// <summary>public AtomLink FindService(string service,string type)
        ///   Retrieves the first link with the supplied 'rel' and/or 'type' value.
        ///   If either parameter is null, the corresponding match isn't needed.
        /// </summary> 
        /// <param name="service">the service entry to find</param>
        /// <param name="type">the link type to find</param>
        /// <returns>the found link or NULL </returns>
        //////////////////////////////////////////////////////////////////////
00226         public List<AtomLink> FindServiceList(string service, string type)
        {
            List<AtomLink> foundLinks = new List<AtomLink>();

            foreach (AtomLink link in List)
            {
                string linkRel = link.Rel;
                string linkType = link.Type;

                if ((service == null || (linkRel != null && linkRel == service)) &&
                    (type == null || (linkType != null && linkType == type)))
                {

                    foundLinks.Add(link);
                    ;
                }
            }
            return foundLinks;
        }
        /////////////////////////////////////////////////////////////////////////////
    }
    /////////////////////////////////////////////////////////////////////////////

    //////////////////////////////////////////////////////////////////////
    /// <summary>standard typed collection based on 1.1 framework for AtomCategory
    /// </summary> 
    //////////////////////////////////////////////////////////////////////
00253     public class AtomCategoryCollection : AtomCollectionBase<AtomCategory>
    {
        /// <summary>standard typed accessor method </summary> 
00256         public override void Add(AtomCategory value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            // Remove category with the same term to avoid duplication.
            AtomCategory oldCategory = Find(value.Term, value.Scheme);
            if (oldCategory != null)
            {
                Remove(oldCategory);
            }
            base.Add(value);
        }

        /// <summary>
        /// finds the first category with this term
        /// ignoring schemes
        /// </summary>
        /// <param name="term">the category term to search for</param>
        /// <returns>AtomCategory</returns>
00277         public AtomCategory Find(string term)
        {
            return Find(term, null);
        }

        /// <summary>
        /// finds a category with a given term and scheme
        /// </summary>
        /// <param name="term"></param>
        /// <param name="scheme"></param>
        /// <returns>AtomCategory or NULL</returns>
00288         public AtomCategory Find(string term, AtomUri scheme)
        {
            foreach (AtomCategory category in List)
            {
                if (scheme == null || scheme == category.Scheme)
                {
                    if (term == category.Term)
                    {
                        return category;
                    }
                }
            }
            return null;
        }

        /// <summary>standard typed accessor method </summary> 
00304         public override bool Contains(AtomCategory value)
        {
            if (value == null)
            {
                return (List.Contains(value));
            }
            // If value is not of type AtomCategory, this will return false.
            if (Find(value.Term, value.Scheme) != null)
            {
                return true;
            }
            return false;

        }
    }
    /////////////////////////////////////////////////////////////////////////////

    //////////////////////////////////////////////////////////////////////
    /// <summary>standard typed collection based on 1.1 framework for AtomPerson
    /// </summary> 
    //////////////////////////////////////////////////////////////////////
00325     public class QueryCategoryCollection : AtomCollectionBase<QueryCategory>
    {

    }
    /////////////////////////////////////////////////////////////////////////////


    //////////////////////////////////////////////////////////////////////
    /// <summary>standard typed collection based on 1.1 framework for AtomPerson
    /// </summary> 
    //////////////////////////////////////////////////////////////////////
00336     public class AtomPersonCollection : AtomCollectionBase<AtomPerson>
    {        

    }
    /////////////////////////////////////////////////////////////////////////////
    
    /// <summary>
    /// Generic collection base class
    /// </summary>
    /// <typeparam name="T"></typeparam>
00346     public class AtomCollectionBase<T> : IList<T>
    {
        /// <summary>
        /// the internal list object that is used
        /// </summary>
00351         protected List<T> List = new List<T>();
        /// <summary>standard typed accessor method </summary> 
        public virtual T this[int index]
00354         {
            get
            {
                return (T)List[index];
            }
            set
            {
                List[index] = value;
            }
        }
        
        /// <summary>standard typed accessor method </summary> 
00366         public virtual void Add(T value)
        {
            List.Add(value);
        }

        /// <summary>
        /// default overload, see base class
        /// </summary>
        /// <param name="index"></param>
00375         public virtual void RemoveAt(int index)
        {
            List.RemoveAt(index);
        }

        /// <summary>
        /// default overload, see base class
        /// </summary>
00383         public virtual void Clear()
        {
            List.Clear();
        }

        /// <summary>
        /// default overload, see base class
        /// </summary>
        /// <param name="arr"></param>
        /// <param name="index"></param>
00393         public virtual void CopyTo(T[] arr, int index)
        {
            List.CopyTo(arr, index);
        }

        /// <summary>standard typed accessor method </summary> 
        /// <param name="value"></param>
00400         public virtual int IndexOf(T value)
        {
            return (List.IndexOf(value));
        }
        /// <summary>standard typed accessor method </summary> 
        /// <param name="index"></param>
        /// <param name="value"></param>
00407         public virtual void Insert(int index, T value)
        {
            List.Insert(index, value);
        }
        /// <summary>standard typed accessor method </summary> 
        /// <param name="value"></param>
00413         public virtual bool Remove(T value)
        {
            return List.Remove(value);
        }
        /// <summary>standard typed accessor method </summary> 
        /// <param name="value"></param>
00419         public virtual bool Contains(T value)
        {
            // If value is not of type AtomPerson, this will return false.
            return (List.Contains(value));
        }

        /// <summary>
        /// default overload, see base class
        /// </summary>
        public virtual int Count
00429         {
            get 
            {
                return List.Count;
            }
        }

        /// <summary>
        /// default overload, see base class
        /// </summary>
        public virtual bool IsReadOnly
00440         {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// default overload, see base class
        /// </summary>
00450         public IEnumerator<T> GetEnumerator()
        {
            return List.GetEnumerator();
        }

        /// <summary>
        /// default overload, see base class
        /// </summary>
00458         IEnumerator IEnumerable.GetEnumerator()
        {
            return List.GetEnumerator();
        }



    }

    /// <summary>
    ///  internal list to override the add and the constructor
    /// </summary>
    /// <returns></returns>
00471     public class ExtensionList : IList<IExtensionElementFactory>
    {
        IVersionAware container;

        List<IExtensionElementFactory> _list = new List<IExtensionElementFactory>();

        /// <summary>
        /// Return a new collection that is not version aware.
        /// </summary>
00480         public static ExtensionList NotVersionAware()
        {
            return new ExtensionList(NullVersionAware.Instance);
        }

        /// <summary>
        /// returns an extensionlist that belongs to a version aware
        /// container
        /// </summary>
        /// <param name="container"></param>
00490         public ExtensionList(IVersionAware container)
        {
            this.container = container;
        }

        /// <summary>
        /// adds value to the extensionlist.
        /// </summary>
        /// <param name="value"></param>
        /// <returns>returns the positin in the list after the add</returns>
00500         public int Add(IExtensionElementFactory value)
        {
            IVersionAware target = value as IVersionAware;

            if (target != null)
            {
                target.ProtocolMajor = this.container.ProtocolMajor;
                target.ProtocolMinor = this.container.ProtocolMinor;
            }
            if (value != null)
            {
                _list.Add(value);
            }
            return _list.Count - 1;
            //return _list.IndexOf(value);
        }

        /// <summary>
        /// default overload, see base class
        /// </summary>
        /// <param name="item"></param>
00521         public int IndexOf(IExtensionElementFactory item)
        {
            return _list.IndexOf(item);
        }

        /// <summary>
        /// default overload, see base class
        /// </summary>
        /// <param name="index"></param>
        /// <param name="item"></param>
00531         public void Insert(int index, IExtensionElementFactory item)
        {
            _list.Insert(index, item);
        }

        /// <summary>
        /// default overload, see base class
        /// </summary>
        /// <param name="index"></param>
00540         public void RemoveAt(int index)
        {
            _list.RemoveAt(index);
        }

        /// <summary>
        /// default overload, see base class
        /// </summary>
        /// <param name="index"></param>
        public IExtensionElementFactory this[int index]
00550         {
            get
            {
                return _list[index];
            }
            set
            {
                _list[index] = value;
            }
        }

        /// <summary>
        /// default overload, see base class
        /// </summary>
        /// <param name="item"></param>
00565         void ICollection<IExtensionElementFactory>.Add(IExtensionElementFactory item)
        {
            Add(item);
        }

        /// <summary>
        /// default overload, see base class
        /// </summary>
00573         public void Clear()
        {
            _list.Clear();
        }

        /// <summary>
        /// default overload, see base class
        /// </summary>
        /// <param name="item"></param>
00582         public bool Contains(IExtensionElementFactory item)
        {
            return _list.Contains(item);
        }

        /// <summary>
        /// default overload, see base class
        /// </summary>
        /// <param name="array"></param>
        /// <param name="arrayIndex"></param>
00592         public void CopyTo(IExtensionElementFactory[] array, int arrayIndex)
        {
            _list.CopyTo(array, arrayIndex);
        }

        /// <summary>
        /// default overload, see base class
        /// </summary>
        public int Count
00601         {
            get
            {
                return _list.Count;
            }
        }

        /// <summary>
        /// default overload, see base class
        /// </summary>
        public bool IsReadOnly
00612         {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// default overload, see base class
        /// </summary>
        /// <param name="item"></param>
00623         public bool Remove(IExtensionElementFactory item)
        {
            return _list.Remove(item);
        }

        /// <summary>
        /// default overload, see base class
        /// </summary>
00631         public IEnumerator<IExtensionElementFactory> GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        /// <summary>
        /// default overload, see base class
        /// </summary>
00639         IEnumerator IEnumerable.GetEnumerator()
        {
            return _list.GetEnumerator();
        }
    }
}

Generated by  Doxygen 1.6.0   Back to index