All pastes #256413 Raw Edit

Untitled

public text v1 · immutable
#256413 ·published 2006-11-24 04:41 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace RoomsHere.BusinessLayer
{
   public class ProductSeasonList : List<ProductSeason>
    {
        public ProductSeasonList() { }

        public ProductSeasonList(IList productSeasonList)
        {
            foreach(ProductSeason ps in productSeasonList )
            {
                this.Add(ps);
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ProductSeasonList"/> class.
        /// </summary>
        /// <param name="productSeason">The product season.</param>
       public ProductSeasonList(ProductSeason productSeason)
       {
               this.Add(productSeason);
       }

       /// <summary>
       /// Projects a season onto another to flatten it out
       /// </summary>
       /// <param name="ProductSeasonList">The product season list.</param>
       /// <returns></returns>
        public ProductSeasonList ProjectOnto(ProductSeasonList ProductSeasonList)
        {

            List<DateTime> slicePoints = new List<DateTime>();

            foreach (ProductSeason p in this)
            {
                if (!slicePoints.Contains(p.StartDate)) slicePoints.Add(p.StartDate);
                if (!slicePoints.Contains(p.EndDate)) slicePoints.Add(p.EndDate);
            }

            foreach (ProductSeason p in ProductSeasonList)
            {
                if (!slicePoints.Contains(p.StartDate)) slicePoints.Add(p.StartDate);
                if (!slicePoints.Contains(p.EndDate)) slicePoints.Add(p.EndDate);
            }

            slicePoints.Sort();


            ProductSeasonList results = new ProductSeasonList();

            ProductSeason current = null;

            foreach (DateTime i in slicePoints)
            {
                ProductSeason best = SelectBest(i, ProductSeasonList);

                if (current != null)
                {
                    if (best == null || best != current)
                    {
                        current.EndDate = i;
                        results.Add(current);
                        current = null;
                    }
                }

                // no else here
                if (current == null)
                {
                    if (best != null)
                    {
                        current = best; // or copy $ or whatever
                        current.StartDate = i;
                    }
                }

            }

            return results;
        }


        /// <summary>
        /// Selects the preferred period
        /// </summary>
        /// <param name="time">The time.</param>
        /// <param name="ProductSeasonList">The product season list.</param>
        /// <returns></returns>
        public ProductSeason SelectBest(DateTime time, ProductSeasonList ProductSeasonList)
        {

            ProductSeason x = this.Find
                    (
                        delegate(ProductSeason p)
                        {
                            return (p.StartDate <= time && time < p.EndDate);
                        }
                    )
                    ;
            if (x != null) return x.Copy();
            ProductSeason y = ProductSeasonList.Find
        (
            delegate(ProductSeason p)
            {
                return (p.StartDate <= time && time < p.EndDate);
            }
        )
        ;
            if (y != null) return y.Copy();
            else return null;
        }
    }
}