/// <summary> /// Tries to parses a HierarchyPath instance from its string representation. /// Type of the path items is specified by the type parameter. /// </summary> /// <typeparam name="T">type of the path items after conversion</typeparam> /// <param name="path">string representation to parse</param> /// <param name="convertPathItem">conversion delegate applied to a single parsed path item</param> /// <param name="separator">seperator character between the path items in the string representation</param> /// <param name="hierarchyPath">parsed result, if successful</param> /// <returns>true on success, false otherwise</returns> public static bool TryParse <T>(string path, out HierarchyPath <T> hierarchyPath, Func <string, T> convertPathItem, string separator) { try { hierarchyPath = Parse <T>(path, convertPathItem, separator); } catch { hierarchyPath = null; } return(path != null); }
/// <summary> /// Retrieves a descendant of the <paramref name="startNode"/> specifed by the <paramref name="path"/> or returns false if not found. /// </summary> /// <typeparam name="TKey">Type of the hierarchy key</typeparam> /// <typeparam name="TNode">Type of the hierarchy node</typeparam> /// <param name="startNode">node instance to start search at</param> /// <param name="path">hierarchy key to search</param> /// <returns>(true, node) if node was found, false otherwise</returns> public static (bool, TNode) TryGetDescendantAt <TKey, TNode>(this TNode startNode, HierarchyPath <TKey> path)
/// <summary> /// Retrieves a descendant of the <paramref name="startNode"/> or throws <see cref="KeyNotFoundException"/> if the /// <paramref name="path"/> can't be followed completely. /// </summary> /// <typeparam name="TKey">Type of the hierarchy key</typeparam> /// <typeparam name="TNode">Type of the hierarchy node</typeparam> /// <param name="startNode">node instance to start search at</param> /// <param name="path">hierarchy key to search</param> /// <returns>the request TNode instance</returns> public static TNode DescendantAt <TKey, TNode>(this TNode startNode, HierarchyPath <TKey> path) where TNode : IHasIdentifiableChildNodes <TKey, TNode> { return(startNode.DescendantAt((TNode p, TKey k) => p.TryGetChildNode(k), path)); }
/// <summary> /// Parses a HierarchyPath instance from its string representation. /// Type of the path items is specified by the type parameter. /// </summary> /// <typeparam name="T">type of the path items after conversion</typeparam> /// <param name="path">string representation to parse</param> /// <param name="convertPathItem">conversion delegate applied to a single parsed path item</param> /// <param name="separator">seperator character between the path items in the string representation</param> /// <returns></returns> public static HierarchyPath <T> Parse <T>(string path, Func <string, T> convertPathItem, string separator = null) { string separatorSafe = separator ?? "\\"; return(HierarchyPath.Create <T>(path.Split(separatorSafe.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(p => convertPathItem(p)))); }
/// <summary> /// Parses a HierarchyPath instance from its string representation. Type of the path items is a string by default. /// </summary> /// <param name="path">string representation of the HierarchyPath</param> /// <param name="separator">Separator betweeen the path items in the string representation</param> /// <returns></returns> public static HierarchyPath <string> Parse(string path, string separator) { return(HierarchyPath.Create(path.Split(separator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries))); }