using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Xml.Linq; namespace Jackett.Common.Utils { public class NonNullException : Exception { public NonNullException() : base("Parameter cannot be null") { } } public class NonNull where T : class { public NonNull(T val) { if (val == null) new NonNullException(); Value = val; } public static implicit operator T(NonNull n) { return n.Value; } private readonly T Value; } public static class GenericConversionExtensions { public static IEnumerable ToEnumerable(this T obj) { return new T[] { obj }; } public static NonNull ToNonNull(this T obj) where T : class { return new NonNull(obj); } } public static class EnumerableExtension { public static string AsString(this IEnumerable chars) { return string.Concat(chars); } public static bool IsEmpty(this IEnumerable collection) { return collection.Count() > 0; } public static IEnumerable Flatten(this IEnumerable> list) { return list.SelectMany(x => x); } } public static class StringExtension { public static bool IsNullOrEmptyOrWhitespace(this string str) { return string.IsNullOrEmpty(str) || string.IsNullOrWhiteSpace(str); } public static DateTime ToDateTime(this string str) { return DateTime.Parse(str); } public static Uri ToUri(this string str) { return new Uri(str); } } public static class CollectionExtension { public static bool IsEmpty(this ICollection obj) { return obj.Count == 0; } public static bool IsEmptyOrNull(this ICollection obj) { return obj == null || obj.IsEmpty(); } } public static class XElementExtension { public static XElement First(this XElement element, string name) { return element.Descendants(name).First(); } public static string FirstValue(this XElement element, string name) { return element.First(name).Value; } } public static class KeyValuePairsExtension { public static IDictionary ToDictionary(this IEnumerable> pairs) { return pairs.ToDictionary(x => x.Key, x => x.Value); } } public static class ParseExtension { public static T? TryParse(this string value) where T : struct { var type = typeof(T); var parseMethods = type.GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static).Where(m => m.Name == "Parse"); var parseMethod = parseMethods.Where(m => { var parameters = m.GetParameters(); var hasOnlyOneParameter = parameters.Count() == 1; var firstParameterIsString = parameters.First().ParameterType == typeof(string); return hasOnlyOneParameter && firstParameterIsString; }).First(); if (parseMethod == null) return null; try { var val = parseMethod.Invoke(null, new object[] { value }); return (T)val; } catch { return null; } } } public static class TaskExtensions { public static Task> Until(this IEnumerable> tasks, TimeSpan timeout) { var timeoutTask = Task.Delay(timeout); var aggregateTask = Task.WhenAll(tasks); var anyTask = Task.WhenAny(timeoutTask, aggregateTask); var continuation = anyTask.ContinueWith((_) => { var completedTasks = tasks.Where(t => t.Status == TaskStatus.RanToCompletion); var results = completedTasks.Select(t => t.Result); return results; }); return continuation; } } }