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)); }
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)); }
//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)); }
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); }