Click here to Skip to main content
15,883,705 members
Articles / Web Development / HTML

My own Mailinator in 5 minutes

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
19 Nov 2012CPOL6 min read 31.6K   405   13  
A simple Mailinator clone developed in five minutes with the NetFluid framework
using System;
using System.Collections.Generic;
using System.Text;

namespace LumiSoft.Net
{
    /// <summary>
    /// Circle collection. Elements will be circled clockwise.
    /// </summary>
    public class CircleCollection<T>
    {
        private List<T> m_pItems = null;
        private int     m_Index  = 0;

        /// <summary>
        /// Default constructor.
        /// </summary>
        public CircleCollection()
        {
            m_pItems = new List<T>();
        }


        #region methd Add

        /// <summary>
        /// Adds specified items to the collection.
        /// </summary>
        /// <param name="items">Items to add.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>items</b> is null.</exception>
        public void Add(T[] items)
        {
            if(items == null){
                throw new ArgumentNullException("items");
            }

            foreach(T item in items){
                Add(item);
            }
        }

        /// <summary>
        /// Adds specified item to the collection.
        /// </summary>
        /// <param name="item">Item to add.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>item</b> is null.</exception>
        public void Add(T item)
        {
            if(item == null){
                throw new ArgumentNullException("item");
            }

            m_pItems.Add(item);

            // Reset loop index.
            m_Index = 0;
        }

        #endregion
        
        #region method Remove

        /// <summary>
        /// Removes specified item from the collection.
        /// </summary>
        /// <param name="item">Item to remove.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>item</b> is null.</exception>
        public void Remove(T item)
        {
            if(item == null){
                throw new ArgumentNullException("item");
            }

            m_pItems.Remove(item);

            // Reset loop index.
            m_Index = 0;
        }

        #endregion
        
        #region method Clear

        /// <summary>
        /// Clears all items from collection.
        /// </summary>
        public void Clear()
        {
            m_pItems.Clear();

            // Reset loop index.
            m_Index = 0;
        }

        #endregion

        #region method Contains

        /// <summary>
        /// Gets if the collection contain the specified item.
        /// </summary>
        /// <param name="item">Item to check.</param>
        /// <returns>Returns true if the collection contain the specified item, otherwise false.</returns>
        public bool Contains(T item)
        {
            return m_pItems.Contains(item);
        }

        #endregion


        #region method Next

        /// <summary>
        /// Gets next item from the collection. This method is thread-safe.
        /// </summary>
        /// <exception cref="InvalidOperationException">Is raised when thre is no items in the collection.</exception>
        public T Next()
        {
            if(m_pItems.Count == 0){
                throw new InvalidOperationException("There is no items in the collection.");
            }

            lock(m_pItems){
                T item = m_pItems[m_Index];

                m_Index++;
                if(m_Index >= m_pItems.Count){
                    m_Index = 0;
                }

                return item;
            }
        }

        #endregion

        #region method ToArray

        /// <summary>
        /// Copies all elements to new array, all elements will be in order they added. This method is thread-safe.
        /// </summary>
        /// <returns>Returns elements in a new array.</returns>
        public T[] ToArray()
        {
            lock(m_pItems){
                return m_pItems.ToArray();
            }
        }

        #endregion

        #region method ToCurrentOrderArray

        /// <summary>
        /// Copies all elements to new array, all elements will be in current circle order. This method is thread-safe.
        /// </summary>
        /// <returns>Returns elements in a new array.</returns>
        public T[] ToCurrentOrderArray()
        {
            lock(m_pItems){
                int index  = m_Index;
                T[] retVal = new T[m_pItems.Count];
                for(int i=0;i<m_pItems.Count;i++){
                    retVal[i] = m_pItems[index];

                    index++;
                    if(index >= m_pItems.Count){
                        index = 0;
                    }
                }

                return retVal;
            }
        }

        #endregion


        #region Properties Implementation

        /// <summary>
        /// Gets number of items in the collection.
        /// </summary>
        public int Count
        {
            get{ return m_pItems.Count; }
        }

        /// <summary>
        /// Gets item at the specified index.
        /// </summary>
        /// <param name="index">Item zero based index.</param>
        /// <returns>Returns item at the specified index.</returns>
        public T this[int index]
        {
            get{ return m_pItems[index]; }
        }

        #endregion

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Genny Mobility
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions