/// <summary> /// Creates a <see cref="RequestPartsKeyGenerator" />. /// </summary> /// <param name="parts">The parts.</param> /// <returns></returns> /// <exception cref="ArgumentOutOfRangeException">name</exception> internal static KeyGenerator Get(RequestParts parts) { if (parts == RequestParts.Default) { parts = RequestParts.All; } return(_keyGenerators.GetOrAdd(parts, p => new RequestPartsKeyGenerator(p, $"{_prefix}{(ushort)p}"))); }
/// <summary> /// Calculates the parts, note this is wrapped by <see cref="GetParts"/> which provides Default parsing. /// </summary> /// <param name="parts">The parts.</param> /// <returns></returns> /// <exception cref="ArgumentOutOfRangeException">parts</exception> private static IEnumerable <RequestPart> CalculateParts(RequestParts parts) { ushort bits = (ushort)parts; // Ensure we don't have a bogus value. if (bits > (ushort)RequestParts.All) { throw new ArgumentOutOfRangeException(nameof(parts)); } ushort flag = 1; while (flag <= bits) { if (parts.HasFlag((RequestParts)flag)) { yield return((RequestPart)flag); } flag <<= 1; } }
/// <summary> /// Initializes a new instance of the <see cref="RequestPartsKeyGenerator" /> class. /// </summary> /// <param name="parts">The parts.</param> /// <param name="name">The name.</param> internal RequestPartsKeyGenerator(RequestParts parts, string name) : base(name, true) => Parts = parts;
/// <summary> /// Gets the individual <see cref="RequestPart">parts</see> from a <see cref="RequestParts"/>, in ascending order. /// </summary> /// <param name="parts">The parts.</param> /// <returns></returns> public static IEnumerable <RequestPart> GetParts(this RequestParts parts) => parts == RequestParts.Default ? _defaultParts : CalculateParts(parts);
public RequestPartsFormatter(RequestParts requestParts) { Parts = requestParts; _parts = requestParts.GetParts().ToArray(); }