Ejemplo n.º 1
0
        public ITimeWindow DeepCloneToANewWindow()
        {
            var newTimeWindow = new TimeWindow();

            foreach (var busySpan in _busyMinutes)
            {
                var newBusySpan = new MinuteSpan(busySpan.Start, busySpan.End);
                newTimeWindow._busyMinutes.Add(newBusySpan);
            }
            foreach (var availableSpan in _availableMinutes)
            {
                var newAvailableSpan = new MinuteSpan(availableSpan.Start, availableSpan.End);
                newTimeWindow._availableMinutes.Add(newAvailableSpan);
            }
            return(newTimeWindow);
        }
Ejemplo n.º 2
0
        public ITimeWindow IncorporateAnotherTimeWindow(ITimeWindow aOtherTimeWindow)
        {
            if (ValidTimeWindow() == false)
            {
            }

            //replace this with deep clone method
            //TimeWindow otherTimeWindow = aOtherTimeWindow as TimeWindow;
            TimeWindow otherTimeWindow = (TimeWindow)aOtherTimeWindow.DeepCloneToANewWindow();

            //set non-duplicates Keep to true in this time window
            int i = 0;
            int j = 0;

            foreach (IMinuteSpan busySpan in _busyMinutes)
            {
                i++;
                busySpan.Keep = true;
                foreach (IMinuteSpan otherBusySpan in _busyMinutes)
                {
                    j++;
                    if (i > j && busySpan.Start == otherBusySpan.Start &&
                        busySpan.End == otherBusySpan.End)
                    {
                        busySpan.Keep = false;
                    }
                }
            }
            i = 0;
            j = 0;
            foreach (IMinuteSpan availableSpan in _availableMinutes)
            {
                availableSpan.Keep = true;
                foreach (IMinuteSpan otherAvailableSpan in _availableMinutes)
                {
                    if (i > j && availableSpan.Start == otherAvailableSpan.Start &&
                        availableSpan.End == otherAvailableSpan.End)
                    {
                        availableSpan.Keep = false;
                    }
                }
            }

            //set non-duplicates Keep to true in other time window
            i = 0;
            j = 0;
            foreach (IMinuteSpan busySpan in otherTimeWindow._busyMinutes)
            {
                busySpan.Keep = true;
                foreach (IMinuteSpan otherBusySpan in _busyMinutes)
                {
                    if (i > j && busySpan.Start == otherBusySpan.Start &&
                        busySpan.End == otherBusySpan.End)
                    {
                        busySpan.Keep = false;
                    }
                }
            }
            i = 0;
            j = 0;
            foreach (IMinuteSpan availableSpan in otherTimeWindow._availableMinutes)
            {
                availableSpan.Keep = true;
                foreach (IMinuteSpan otherAvailableSpan in _availableMinutes)
                {
                    if (i > j && availableSpan.Start == otherAvailableSpan.Start &&
                        availableSpan.End == otherAvailableSpan.End)
                    {
                        availableSpan.Keep = false;
                    }
                }
            }

            //first, compare each pair of busy spans and don't keep ones that are completely surrounded by the other
            foreach (IMinuteSpan newBusySpan in otherTimeWindow._busyMinutes)
            {
                //newBusySpan.Keep = true;

                foreach (IMinuteSpan busySpan in _busyMinutes)
                {
                    if (busySpan.Keep)
                    {
                        if (newBusySpan.Start <= busySpan.Start && newBusySpan.End >= busySpan.End)
                        {
                            busySpan.Keep = false;
                        }
                        else if (newBusySpan.Start >= busySpan.Start && newBusySpan.End <= busySpan.End)
                        {
                            newBusySpan.Keep = false;
                        }
                    }
                }
            }

            // next compare again and combine those that partially overlap or are adjacent
            foreach (IMinuteSpan newBusySpan in otherTimeWindow._busyMinutes)
            {
                foreach (IMinuteSpan busySpan in _busyMinutes)
                {
                    if (newBusySpan.Keep && busySpan.Keep)
                    {
                        if (busySpan.Start >= newBusySpan.Start && busySpan.Start <= newBusySpan.End + 1)
                        {
                            busySpan.Start   = newBusySpan.Start;
                            newBusySpan.Keep = false;
                        }
                        else if (busySpan.End >= newBusySpan.Start - 1 && busySpan.End <= newBusySpan.End)
                        {
                            busySpan.End     = newBusySpan.End;
                            newBusySpan.Keep = false;
                        }
                    }
                }
            }
            // keep only newBusySpans that don't overlap (or completely surround) existing ones
            // check remaining existing ones for overlap, now that they may have been extended

            foreach (IMinuteSpan busySpan in _busyMinutes)
            {
                foreach (IMinuteSpan laterBusySpan in _busyMinutes.Where(s => s != busySpan && s.Keep && s.Start >= busySpan.Start))
                {
                    if (laterBusySpan.Start <= busySpan.End)
                    {
                        busySpan.End       = laterBusySpan.End;
                        laterBusySpan.Keep = false;
                    }
                }
            }

            _busyMinutes.RemoveAll(s => !s.Keep);

            foreach (IMinuteSpan newBusySpan in otherTimeWindow._busyMinutes.Where(s => s.Keep))
            {
                _busyMinutes.Add(newBusySpan);
            }

            _busyMinutes = _busyMinutes.OrderBy(x => x.Start).ToList();

            _availableMinutes = GetAvailableMinutes();

            /* old code
             * int firstBusy = 0;
             * int nextAvailable = 0;
             * for (var i = 1; i <= Global.Settings.Times.MinutesInADay; i++) {
             *  if (otherTimeWindow.IsBusy(i) && firstBusy == 0) {
             *      firstBusy = i;
             *  }
             *  if (!otherTimeWindow.IsBusy(i) && i > firstBusy && firstBusy > 0 && nextAvailable == 0) {
             *      nextAvailable = i;
             *  }
             *  if (firstBusy > 0 && nextAvailable > firstBusy) {
             *      //thisTimeWindow.SetBusyMinutes (firstBusy, nextAvailable - 1);
             *      SetBusyMinutes(firstBusy, nextAvailable - 1);
             *      firstBusy = 0;
             *      nextAvailable = 0;
             *  }
             * }
             * if (firstBusy > 0) {
             *  SetBusyMinutes(firstBusy, Global.Settings.Times.MinutesInADay);
             */

            if (ValidTimeWindow() == false)
            {
            }

            return(this);
        }