Example #1
0
        public static TTo TransformTree <TFrom, TTo>(TFrom sourceRoot, Func <TFrom, TTo> transformNode, GetChildrenDelegate <TFrom> getSourceChildren, GetChildrenCollectionDelegate <TTo> getTargetChildrenCollection, Action <TTo, TFrom> internalNodeAction, Action <TTo, TFrom> leafNodeAction)
            where TFrom : class
            where TTo : class
        {
            if (transformNode == null)
            {
                throw new ArgumentNullException("transformNode");
            }
            if (getSourceChildren == null)
            {
                throw new ArgumentNullException("getSourceChildren");
            }
            if (getTargetChildrenCollection == null)
            {
                throw new ArgumentNullException("getTargetChildrenCollection");
            }
            if (sourceRoot == null)
            {
                return(null);
            }
            var targetRoot = transformNode(sourceRoot);

            if (targetRoot != null)
            {
                var sourceChildren = getSourceChildren(sourceRoot);
                if (sourceChildren != null)
                {
                    var targetChildrenCollection = getTargetChildrenCollection(targetRoot);
                    if (targetChildrenCollection == null)
                    {
                        throw new Exception("targetChildrenCollection must be not null");
                    }
                    foreach (var sourceChild in sourceChildren)
                    {
                        var targetChild = TransformTree(sourceChild, transformNode, getSourceChildren, getTargetChildrenCollection, internalNodeAction, leafNodeAction);
                        targetChildrenCollection.Add(targetChild);
                    }

                    if (sourceChildren.Any())
                    {
                        if (internalNodeAction != null)
                        {
                            internalNodeAction(targetRoot, sourceRoot);
                        }
                    }
                    else
                    {
                        if (leafNodeAction != null)
                        {
                            leafNodeAction(targetRoot, sourceRoot);
                        }
                    }
                }
                else
                {
                    if (leafNodeAction != null)
                    {
                        leafNodeAction(targetRoot, sourceRoot);
                    }
                }
            }
            return(targetRoot);
        }
Example #2
0
        public static IEnumerable <T> MakeTrees <T>(IEnumerable <T> source, Func <T, int> getId, Func <T, int> getParentId, Action <T, T> setParent, GetChildrenCollectionDelegate <T> getChildren, int defaultId)
            where T : class
        {
            var idOfItems       = source.ToDictionary(x => x, getId);
            var parentIdOfItems = source.ToDictionary(x => x, getParentId);
            var rootItems       = source.Where(x => parentIdOfItems[x] == defaultId).ToArray();

            if (!rootItems.Any())
            {
                throw new Exception("It's impossible to create trees. There is no items which parent is null");
            }
            var notRootItems = source.Except(rootItems).ToArray();

            if (notRootItems.Any(x => !idOfItems.Values.Contains(parentIdOfItems[x])))
            {
                throw new Exception("It's impossible to create trees. At least one item has parent that doesn't contain in source enumerable");
            }
            foreach (var item in source)
            {
                getChildren(item).Clear();
            }
            foreach (var item in rootItems)
            {
                setParent(item, null);
            }
            foreach (var item in notRootItems)
            {
                var parentId = parentIdOfItems[item];
                var parent   = source.Single(x => idOfItems[x] == parentId);
                setParent(item, parent);
                getChildren(parent).Add(item);
            }
            return(rootItems);
        }
Example #3
0
 public static TTo TransformTree <TFrom, TTo>(TFrom sourceRoot, Func <TFrom, TTo> transformNode, GetChildrenDelegate <TFrom> getSourceChildren, GetChildrenCollectionDelegate <TTo> getTargetChildrenCollection)
     where TFrom : class
     where TTo : class
 {
     return(TransformTree(sourceRoot, transformNode, getSourceChildren, getTargetChildrenCollection, null, null));
 }
Example #4
0
        public static IEnumerable <T> MakeTrees <T>(IEnumerable <T> source, Func <T, T> getParent, GetChildrenCollectionDelegate <T> getChildren)
            where T : class
        {
            var parentOfItems = source.ToDictionary(x => x, getParent);
            var rootItems     = parentOfItems.Where(x => x.Value == null).Select(x => x.Key).ToArray();

            if (!rootItems.Any())
            {
                throw new Exception("It's impossible to create trees. There is no items which parent is null");
            }
            var notRootItems = source.Except(rootItems).ToArray();

            if (notRootItems.Any(x => !source.Contains(parentOfItems[x])))
            {
                throw new Exception("It's impossible to create trees. At least one item has parent that doesn't contain in source enumerable");
            }
            var childrenOfItems = source.ToDictionary(x => x, x => getChildren(x));

            foreach (var item in source)
            {
                getChildren(item).Clear();
            }
            foreach (var item in notRootItems)
            {
                var parent   = parentOfItems[item];
                var children = childrenOfItems[parent];
                children.Add(item);
            }
            return(rootItems);
        }