Example #1
0
        public virtual IEnumerable <Lot <T> > RequestPermutations <T>(
            Lot <T> collection)
        {
            var r = this.rotator;

            for (var i = 0; i < collection.Count; ++i)
            {
                var switched = XLinkedList <T> .Create(
                    Skip(
                        collection,
                        i));

                for (var j = 0; j < i; ++j)
                {
                    switched.AddTail(
                        FirstOrDefault(
                            Skip(
                                collection,
                                j)));
                }

                for (var k = 0; k < i; ++k)
                {
                    yield return(r.Rotate(switched, k));
                }
            }
        }
Example #2
0
        public virtual void PushRange(
            IEnumerable <T> source,
            long index,
            long length)
        {
            Lot <T> lot =
                new XLinkedListLot <T>(
                    XLinkedList <T> .Create(
                        source));
            var  array   = new T[length - index];
            long counter = zero;

            foreach (var item in lot)
            {
                if (counter < index)
                {
                    continue;
                }

                if (counter >= length)
                {
                    break;
                }

                array[counter] = item;
                ++counter;
            }

            this.stack.PushRange(array);
        }
Example #3
0
        public virtual T Liken <T>(
            Func <T> factory,
            Lot <Action <T> > acts,
            long limiter)
        {
            var actee = factory();

            FirstOrDefault(
                acts)
            ?.Invoke(actee);
            var firstHashCode = actee.GetHashCode();

            foreach (var act in Skip(acts, 1))
            {
                act(actee);
                if (Math.Abs(actee.GetHashCode() - firstHashCode) <= limiter)
                {
                    continue;
                }

                return(this.Liken(
                           factory,
                           new XLinkedListLot <Action <T> >(
                               XLinkedList <Action <T> > .Create(
                                   Except(
                                       acts,
                                       new[] { act }))),
                           limiter));
            }

            return(actee);
        }
Example #4
0
 public virtual T Illumine <T>(
     params object[] dependencies)
 {
     return(this.Illumine <T>(
                new XLinkedListLot <object>(
                    XLinkedList <object> .Create(
                        dependencies))));
 }
        protected virtual ICollection <DateTime> allTimes()
        {
            var r = this.runner;

            return(XLinkedList <DateTime> .Create(
                       EnumerableHelpers.OrderBy(
                           r?.Run <TimestampReader>()
                           ?.ReadAll(),
                           ts => ts)));
        }
Example #6
0
        public virtual void Order(
            Tree <T> tree)
        {
            this.processNodes(tree.Node);

            this.setCurrentTree(
                new XLinkedListLot <T>(
                    XLinkedList <T> .Create(
                        OrderBy(
                            tree,
                            t => t.GetHashCode()))));
        }
Example #7
0
        public virtual void Handle(
            TimestampEditUi ui,
            HomeNavUi hnUi,
            HomeUi homeUi)
        {
            var r = this.runner;

            r?.Run <UiReaderWriter>(uiRW =>
            {
                r.Run <TimestampReader>(reader =>
                {
                    var allColl = reader.ReadAll();

                    var ll = allColl as XLinkedList <DateTime>
                             ?? XLinkedList <DateTime> .Create(allColl);
                    const byte one = 1;
                    if (ll.Count < one)
                    {
                        return;
                    }

                    var lastTimestamp = ll
                                        .Tail;
                    uiRW.Write(
                        ui,
                        () =>
                    {
                        ui.EditedTimestamp = lastTimestamp;
                    });
                });

                r.Run <SettingsHolder>(settings =>
                {
                    settings.LastVisitedKeyLabel = uiRW.Read(
                        hnUi,
                        () => hnUi.ActiveKeyLabel);
                    uiRW.Write(
                        hnUi,
                        () =>
                    {
                        hnUi.ActiveKeyLabel = null;
                    });
                });

                uiRW.Write(
                    homeUi,
                    () =>
                {
                    homeUi.Editing = true;
                });
            });
        }
Example #8
0
        public virtual T PreInvoke <T>(
            Action <T> act,
            params object[] dependencies)
            where T : class, IDisposable
        {
            var actor = this.illuminator.Illumine <T>(dependencies);

            act(actor);
            this.freedomHolder.Surge(
                new XLinkedListLot <IDisposable>(
                    XLinkedList <IDisposable> .Create(
                        new IDisposable[] { actor })));
            return(actor);
        }
Example #9
0
        public void PushRange(
            IEnumerable <T> source)
        {
            Lot <T> lot = new XLinkedListLot <T>(
                XLinkedList <T> .Create(
                    source));
            var  array   = new T[lot.Count];
            long counter = zero;

            foreach (var item in lot)
            {
                array[counter] = item;
                ++counter;
            }

            this.stack.PushRange(array);
        }
Example #10
0
        public virtual T Illumine <T>(
            Lot <object> dependencies)
        {
            var constructors = XLinkedList <ConstructorInfo> .Create(
                EH.OrderByDescending(
                    typeof(T).GetConstructors(),
                    ci => ci.GetParameters().Length));

            var list = new List <object>(dependencies);

            foreach (var ctor in constructors)
            {
                var parameters   = ctor.GetParameters();
                var valuesNeeded = new List <object>(list.Count);
                foreach (var parameter in parameters)
                {
                    for (var i = 0; i < list.Count; ++i)
                    {
                        if (!parameter.ParameterType.IsInstanceOfType(list[i]))
                        {
                            continue;
                        }

                        valuesNeeded.Add(list[i]);
                        list.Remove(list[i]);
                        break;
                    }
                }

                if (valuesNeeded.Count == parameters.Length)
                {
                    list = valuesNeeded;
                    break;
                }

                // if the objects don't line up with this constructor, start over with the next one
                list = new List <object>(dependencies);
                valuesNeeded.Clear();
            }

            return((T)Activator.CreateInstance(typeof(T), list.ToArray()));
        }
Example #11
0
        public virtual TUi ReadUi <TUi, TPresenter>(
            string uiName        = null,
            string presenterName = null)
            where TUi : Ui
            where TPresenter : Presenter
        {
            const byte             one = 1;
            ICollection <UiHolder> matches;

            lock (this.locker)
            {
                matches = XLinkedList <UiHolder> .Create(
                    Where(
                        this.uiHolders,
                        ui => ui.Content is TUi));
            }

            if (matches.Count < one)
            {
                return(default);
Example #12
0
        public virtual Lot <T>[] Slice <T>(
            IEnumerable <T> finiteSource,
            Lot <int> slicePoints)
        {
            const byte
                zero = 0,
                one  = 1;

            if (finiteSource == null)
            {
                return(new Lot <T> [zero]);
            }

            if (slicePoints == null || slicePoints.Count < one)
            {
                return(new Lot <T> [zero]);
            }

            ICollection <T> remainingItems
                = XLinkedList <T> .Create(finiteSource);

            var array   = new Lot <T> [slicePoints.Count];
            int counter = zero;

            foreach (var slicePoint in slicePoints)
            {
                var sequence = EH.Take(
                    remainingItems,
                    slicePoint);
                array[counter] = new XLinkedListLot <T>(
                    XLinkedList <T> .Create(
                        sequence));
                ++counter;
                remainingItems = XLinkedList <T> .Create(
                    EH.Skip(
                        remainingItems,
                        slicePoint));
            }

            return(array);
        }
Example #13
0
            public void Otherwise_returns_time_worked_for_the_week()
            {
                var w = this.web;

                w.Unregister <DateCalculator>();
                w.RegisterDependency(
                    A.Fake <DateCalculator>());
                w.Run <DateCalculator>(fakeDater =>
                {
                    A
                    .CallTo(() => fakeDater.StartOfWeek())
                    .Returns(new DateTime(
                                 2020,
                                 11,
                                 24));
                    A
                    .CallTo(() => fakeDater.EndOfWeek())
                    .Returns(new DateTime(
                                 2020,
                                 11,
                                 25));
                });

                var allTimes = XLinkedList <DateTime> .Create(new[]
                {
                    new DateTime(2020, 11, 24, 7, 0, 0),
                    new DateTime(2020, 11, 24, 1, 45, 0),
                    new DateTime(2020, 11, 24, 2, 40, 0),
                    new DateTime(2020, 11, 24, 5, 40, 0),
                });

                A
                .CallTo(() => this.reader.ReadAll())
                .Returns(allTimes);

                Assert.Equal(
                    new TimeSpan(0, 2, 15, 0),
                    this.calc.TimeWorkedThisWeek());
            }
Example #14
0
        public virtual Lot <T> Reorder(
            IEnumerable <T> source,
            int startIndex,
            int take)
        {
            var ll = XLinkedList <T> .Create(source);

            var rotated = this.rotator.Rotate(
                ll,
                startIndex,
                false);

            var start = Take(rotated, take);
            var next  = Take(ll, startIndex);
            var end   = Empty <T>();

            if (ll.Count > startIndex + take)
            {
                end = Skip(ll, startIndex + take);
            }

            return(this.connector.Connect(start, next, end));
        }
Example #15
0
 public virtual void Order(Tree <T> tree)
 {
     this.currentTree = new XLinkedListLot <T>(
         XLinkedList <T> .Create(Reverse(
                                     tree)));
 }
Example #16
0
        public virtual void Handle(
            TimestampEditUi ui)
        {
            var r        = this.runner;
            var accepted = true;

            r?.Run <UiReaderWriter, GlobalSettingsHolder, Messenger>(
                (uiRW, settings, m) =>
            {
                if (settings.Prompt)
                {
                    var response = uiRW.Read(
                        m.Subscriber,
                        () => m.Question(@"Save edit?"));
                    accepted = response == Response.Yes;
                }
            });
            if (!accepted)
            {
                return;
            }

            r?.Run <
                TimestampReader,
                UiReaderWriter>(
                (reader, uiRW) =>
            {
                var newTimestamp = uiRW.Read(
                    ui,
                    () => ui.EditedTimestamp);
                var allColl = reader.ReadAll();
                var allLL   = allColl as XLinkedList <DateTime>
                              ?? XLinkedList <DateTime> .Create(allColl);
                const byte two = 2;
                if (allLL.Count < two)
                {
                    goto checkNow;
                }

                var previousTimestamp = allLL
                                        .TailN
                                        ?.Previous
                                        ?.O;
                if (newTimestamp < previousTimestamp)
                {
                    r.Run <Messenger>(m =>
                    {
                        uiRW.Write(
                            m.Subscriber,
                            () =>
                        {
                            m.GiveError(
                                ErrorMessages.TooEarly);
                        });
                    });

                    return;
                }

                checkNow:
                var now = DateTime.Now;
                r.Run <TimeProvider>(provider =>
                {
                    now = provider.Now();
                });

                if (newTimestamp > now)
                {
                    r.Run <Messenger>(m =>
                    {
                        uiRW.Write(
                            m.Subscriber,
                            () =>
                        {
                            m.GiveError(
                                ErrorMessages.TooLate);
                        });
                    });

                    return;
                }

                r.Run <DataWatcher, TimestampWriter>(
                    (watcher, writer) =>
                {
                    watcher.Stop();
                    writer.EditLastTimestamp(newTimestamp);
                    watcher.Start();
                });

                r.Run <HomeUi, Framework.Home.TimerHandler>(
                    (homeUi, handler) =>
                {
                    handler.Handle(
                        homeUi);
                });

                r.Run <UiReader, Framework.Statistics.TimerHandler>(
                    (uiReader, handler) =>
                {
                    uiReader.ReadStatistics(
                        out var statsUi);
                    handler.Handle(
                        statsUi);
                });

                r.Run <SettingsHolder, NavLogicReader>(
                    (settings, navReader) =>
                {
                    switch (settings.LastVisitedKeyLabel)
                    {
                    case NavKeyLabels.Timestamps:
                        navReader.ReadTimestamps(
                            out var navToTimestamps);
                        navToTimestamps?.Invoke();
                        break;

                    case NavKeyLabels.Statistics:
                        navReader.ReadStatistics(
                            out var navToStats);
                        navToStats?.Invoke();
                        break;

                    case NavKeyLabels.Daily:
                        navReader.ReadDaily(
                            out var navToDaily);
                        navToDaily?.Invoke();
                        break;

                    case NavKeyLabels.Config:
                        navReader.ReadConfig(
                            out var navToConfig);
                        navToConfig?.Invoke();
                        break;

                    default:
                        navReader.ReadTimestamps(
                            out var defaultNav);
                        defaultNav?.Invoke();
                        break;
                    }
                });
            });
        }