// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) VRMADA, All rights reserved. // // -------------------------------------------------------------------------------------------------------------------- using System; using System.Collections.Generic; namespace UltimateXR.Extensions.System.Collections { /// /// and extensions. /// public static class CollectionExt { #region Public Methods /// /// Throws an exception if a given index is out of a bounds. /// /// Collection /// Index to check if it is out of bounds /// Optional argument name /// Element type /// When index is out of range and no parameter name was specified /// When index is out of range and a parameter name was specified public static void ThrowIfInvalidIndex(this IReadOnlyCollection self, int index, string paramName = null) { if (index >= 0 && index < self.Count) { return; } if (string.IsNullOrWhiteSpace(paramName)) { throw new IndexOutOfRangeException($"Index[{index}] out of range for collection of {typeof(T).Name}"); } throw new ArgumentOutOfRangeException(paramName, index, $"Index[{index}] out of range for collection of {typeof(T).Name}"); } /// /// Throws an exception if any of the given indexes is out of a bounds. /// /// Collection /// Index 1 to check if it is out of bounds /// Index 2 to check if it is out of bounds /// Element type /// public static void ThrowIfInvalidIndexes(this IReadOnlyCollection self, int index1, int index2) { self.ThrowIfInvalidIndex(index1); self.ThrowIfInvalidIndex(index2); } /// /// Throws an exception if any of the given indexes is out of a bounds. /// /// Collection /// Index 1 to check if it is out of bounds /// Index 2 to check if it is out of bounds /// Index 3 to check if it is out of bounds /// Element type /// public static void ThrowIfInvalidIndexes(this IReadOnlyCollection self, int index1, int index2, int index3) { self.ThrowIfInvalidIndex(index1); self.ThrowIfInvalidIndex(index2); self.ThrowIfInvalidIndex(index3); } /// /// Throws an exception if any of the given indexes is out of a bounds. /// /// Collection /// Indexes to check /// Element type public static void ThrowIfInvalidIndexes(this IReadOnlyCollection self, params int[] indexes) { foreach (int index in indexes) { self.ThrowIfInvalidIndex(index); } } /// /// Splits a string using and adds the result to the collection. /// /// Collection to add the split result to /// String to split /// /// Separator to use for splitting. This will be used to call /// on /// /// The result collection public static ICollection SplitAddRange(this ICollection self, string toSplit, char separator) { self.ThrowIfNull(nameof(self)); if (string.IsNullOrWhiteSpace(toSplit)) { return self; } foreach (string s in toSplit.Split(separator)) { self.Add(s.Trim()); } return self; } /// /// Splits a string using and sets the result in the collection. /// /// Collection to set the split result in /// String to split /// /// Separator to use for splitting. This will be used to call /// on /// /// The result collection public static ICollection SplitSetRange(this ICollection self, string toSplit, char separator) { self.ThrowIfNull(nameof(self)); self.Clear(); return self.SplitAddRange(toSplit, separator); } #endregion } }