All pastes #256420 Raw Edit

Mine

public text v1 · immutable
#256420 ·published 2006-11-24 04:58 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(params ProductSeasonList[] ProductSeasonLists)
        {
            List<ProductSeason> ProductSeasonList = new List<ProductSeason>(this);
            foreach(ProductSeasonList l in ProductSeasonLists) ProductSeasonList.Add(l);

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

            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, List<ProductSeason> ProductSeasonList)
        {
                    List<ProductSeason> results = ProductSeasonList.FindAll
                    (
                        delegate(ProductSeason p)
                        {
                            return (p.StartDate <= time && time < p.EndDate);
                        }
                    )

                    if(results.Count ==0) return null;
            
                    results.Sort();

                    return results[0].Copy();
    }
}