2 using System.Collections;
3 using System.Collections.Generic;
5 using System.Linq.Expressions;
7 using System.Text.RegularExpressions;
8 using Pithos.Client.WPF.SelectiveSynch;
10 using Pithos.Interfaces;
12 namespace Pithos.Client.WPF.Utils
14 public static class EnumerableExtensions
16 public static IEnumerable<T> Slice<T>(this IEnumerable<T> collection, int start, int end)
21 if (collection == null)
22 throw new ArgumentNullException("collection");
24 // Optimise item count for ICollection interfaces.
25 if (collection is ICollection<T>)
26 count = ((ICollection<T>)collection).Count;
27 else if (collection is ICollection)
28 count = ((ICollection)collection).Count;
31 count = collection.Count();
34 // Get start/end indexes, negative numbers start at the end of the collection.
41 foreach (var item in collection)
53 public static IEnumerable<Node<T>> ToTree<TSource,T>(this IEnumerable<TSource> enumerable,Func<TSource,string> pathFunc,Func<TSource,T> valueFunc,string delimiter="/")
55 var orderedItems=enumerable.OrderBy(pathFunc);
56 var lookups = new Dictionary<string,Node<T>>();
57 var nodes = new List<Node<T>>();
58 foreach (var item in orderedItems)
60 var path = pathFunc(item);
61 var value = valueFunc(item);
62 var newNode = new Node<T> { Path = path,Data=value };
63 lookups[path] = newNode;
65 var lastIndex = path.LastIndexOf(delimiter, StringComparison.Ordinal);
66 var upTo = lastIndex < 0 ? path.Length - 1 : lastIndex;
67 var parentPath = path.Substring(0, upTo);
70 if (lookups.TryGetValue(parentPath, out parent))
72 parent.Children.Add(newNode);
73 parent.Children.Sort((x,y)=>String.CompareOrdinal(x.Path, y.Path));
82 public static List<DirectoryRecord> ToTree(this IEnumerable<ObjectInfo> enumerable)
84 //Order the items to ensure that children always come after their parents
85 var orderedItems=enumerable.OrderBy(o=>o.Uri.ToString());
86 //Each item is stored in lookups
87 var lookups = new Dictionary<string,DirectoryRecord>();
89 //RootNodes contains only the root nodes
90 var rootNodes = new List<DirectoryRecord>();
92 foreach (var item in orderedItems)
94 var path = item.Uri.ToString();
95 //Calculate the parent path
96 var parentPath = GetParentPath(path);
97 var parentName = GetParentPath(item.Name);
98 DirectoryRecord parent;
99 DirectoryRecord newNode;
102 if (!item.IsDirectory)
104 //But check to ensure that we DO have it's parent on record
106 if (lookups.TryGetValue(parentPath, out parent))
111 //If the item is directly below its parent container, there is no path to add
112 if (String.IsNullOrWhiteSpace(parentName))
114 //Otherwise we need to add it, because it is missing from the list
115 //Store each item using its current path
116 newNode = new DirectoryRecord { DisplayName = parentPath.Split('/').Last(),
117 ObjectInfo = new ObjectInfo{Account=item.Account,Container=item.Container,Name=parentPath,Content_Type="application/directory"}};
121 //Store each item using its current path
122 newNode = new DirectoryRecord {DisplayName = item.Name.Split('/').Last(), ObjectInfo = item};
124 AddNode(rootNodes, parentPath, path, lookups, newNode);
129 private static void AddNode(List<DirectoryRecord> rootNodes, string parentPath, string path, Dictionary<string, DirectoryRecord> lookups, DirectoryRecord newNode)
131 DirectoryRecord parent;
132 lookups[path] = newNode;
135 //Does a parent item exist?
136 if (lookups.TryGetValue(parentPath, out parent))
138 //If so, add the current item under its parent
139 parent.Directories.Add(newNode);
140 parent.Directories.Sort((x, y) => String.CompareOrdinal(x.Uri.ToString(), y.Uri.ToString()));
143 //Otherwise add it to the list of root nodes
144 rootNodes.Add(newNode);
147 private static string GetParentPath(string path)
149 var lastIndex = path.LastIndexOf("/", StringComparison.Ordinal);
152 var parentPath = path.Substring(0, lastIndex);
156 static readonly Regex PascalCaseRegex = new Regex("[a-z][A-Z]", RegexOptions.Compiled);
157 public static string Name(this Enum value)
159 var name = Enum.GetName(value.GetType(), value);
160 return PascalCaseRegex.Replace(name, m => m.Value[0] + " " + char.ToLower(m.Value[1]));