Example #1
0
        protected override ReadOnlyParent Immutify(ReadWriteParent mutable, ImmutableSupport support)
        {
            IParent source = mutable ?? (IParent)this;

            int variable = source.Variable;
            SharedReadOnlyChild         sharedChild = support.Immutify <SharedReadOnlyChild, SharedWriteableChild>(SharedChild);
            ReadOnlyChild               child       = support.Immutify <ReadOnlyChild, ReadWriteChild>(Child);
            IEnumerable <ReadOnlyChild> children    = support.Immutify <ReadOnlyChild, ReadWriteChild>(Children);

            // TODO: Equals für die Collections? ... gehört womöglich in Extensions!
            return(Equals(variable, Variable) && Equals(sharedChild, SharedChild) && Equals(child, Child) && Equals(children, Children)
                ? this
                : new ReadOnlyParent(FixedName, variable, sharedChild, child, children));
        }
Example #2
0
        public void ParentWithNonchangingChild()
        {
            var p  = new ReadOnlyParent("p", 1000, null, null, new ReadOnlyChild[0]);
            var gp = new ReadOnlyGrandparent("g", 100, p);

            var s = new ImmutableSupport();

            p.GetOrCreateMutable(s).Variable = p.Variable;

            ReadOnlyGrandparent gp2 = s.Immutify <ReadOnlyGrandparent, ReadWriteGrandparent>(gp);

            Assert.AreSame(gp, gp2);
            Assert.AreEqual(1000, p.Variable);
            Assert.AreEqual(100, gp.Variable);
        }
Example #3
0
        protected override ReadOnlyChild Immutify(ReadWriteChild mutable, ImmutableSupport support)
        {
            if (mutable == null)
            {
                return(this);
            }
            else
            {
                int variable = mutable.Variable;

                return(Equals(variable, Variable)
                    ? this
                    : new ReadOnlyChild(FixedName, variable));
            }
        }
Example #4
0
        //public static IEnumerable<ReadOnlyGrandparent> SomeAlgorithm(IEnumerable<ReadOnlyGrandparent> input,
        //    Func<ReadOnlyGrandparent, string> parentSelector, Func<ReadOnlyParent, bool> parentSelector,
        //    Func<ReadOnlyParent, bool> removeParentSelector, ImmutableSupport support) {
        //    foreach (var p in input) {
        //        string markerToAdd = parentSelector(p);
        //        if (markerToAdd != null) {
        //            //p.Variable++;
        //            p.GetOrCreateMutable(support).Variable++;
        //        }
        //        if (parentSelector(p.Parent)) {
        //            p.Parent.GetOrCreateMutable(support).Variable++;
        //        }
        //        foreach (var c in p.Parentren.Where(c => parentSelector(c))) {
        //            c.GetOrCreateMutable(support).Variable++;
        //        }
        //        p.GetOrCreateMutable(support).Parentren.RemoveAll(c => removeParentSelector(c));
        //    }
        //
        //    return input.Immutify<ReadOnlyGrandparent, ReadWriteGrandparent>(support);
        //}

        protected override ReadOnlyGrandparent Immutify(ReadWriteGrandparent mutable, ImmutableSupport support)
        {
            IGrandparent   source   = mutable ?? (IGrandparent)this;
            int            variable = source.Variable;
            ReadOnlyParent parent   = support.Immutify <ReadOnlyParent, ReadWriteParent>(Parent);

            return(Equals(variable, Variable) && Equals(parent, Parent)
                ? this
                : new ReadOnlyGrandparent(FixedName, variable, parent));
        }
Example #5
0
 protected override SharedReadOnlyChild Immutify(SharedWriteableChild mutable, ImmutableSupport support)
 {
     if (mutable == null)
     {
         return(this);
     }
     else if (mutable.Data.SetEquals(_data))
     {
         return(this);
     }
     else
     {
         SharedReadOnlyChild key = new SharedReadOnlyChild(mutable.Data, _cache);
         SharedReadOnlyChild result;
         if (!_cache.TryGetValue(key, out result))
         {
             _cache.Add(key, result = key);
         }
         return(result);
     }
 }
Example #6
0
        public static IEnumerable <ReadOnlyParent> SomeAlgorithm(IEnumerable <ReadOnlyParent> input,
                                                                 Func <ReadOnlyParent, string> parentSelector, Func <ReadOnlyChild, bool> childSelector,
                                                                 Func <ReadOnlyChild, bool> removeChildSelector, ImmutableSupport support)
        {
            foreach (var p in input)
            {
                string markerToAdd = parentSelector(p);
                if (markerToAdd != null)
                {
                    //p.Variable++;
                    p.GetOrCreateMutable(support).Variable++;
                    p.GetOrCreateMutable(support).SharedChild.GetOrCreateMutable(support).Data.Add(markerToAdd);
                }
                if (childSelector(p.Child))
                {
                    p.Child.GetOrCreateMutable(support).Variable++;
                }
                foreach (var c in p.Children.Where(c => childSelector(c)))
                {
                    c.GetOrCreateMutable(support).Variable++;
                }
                p.GetOrCreateMutable(support).ChildrenList.RemoveAll(c => removeChildSelector(c));
            }

            return(support.Immutify <ReadOnlyParent, ReadWriteParent>(input));
        }