using System; using System.Collections.Generic; namespace Jellyfin.Extensions { /// /// Static extensions for the interface. /// public static class ReadOnlyListExtension { /// /// Finds the index of the desired item. /// /// The source list. /// The value to fine. /// The type of item to find. /// Index if found, else -1. public static int IndexOf(this IReadOnlyList source, T value) { if (source is IList list) { return list.IndexOf(value); } for (int i = 0; i < source.Count; i++) { if (Equals(value, source[i])) { return i; } } return -1; } /// /// Finds the index of the predicate. /// /// The source list. /// The value to find. /// The type of item to find. /// Index if found, else -1. public static int FindIndex(this IReadOnlyList source, Predicate match) { if (source is List list) { return list.FindIndex(match); } for (int i = 0; i < source.Count; i++) { if (match(source[i])) { return i; } } return -1; } /// /// Get the first or default item from a list. /// /// The source list. /// The type of item. /// The first item or default if list is empty. public static T? FirstOrDefault(this IReadOnlyList? source) { if (source is null || source.Count == 0) { return default; } return source[0]; } } }