using System.Collections; namespace Numbers.NumberFactory; /// /// Base class for implementations /// /// public abstract class AbstractNumbers : INumbers { private readonly List _list; /// /// Creates a new instance of with the given bounds. /// Will generate the appropriate numbers based on the implementation of . /// /// Lower bound of the number range /// Upper bound of the number range protected AbstractNumbers(long lowerBound, long upperBound) { LowerBound = lowerBound; UpperBound = upperBound; _list = []; GenerateNumbers(); } public long this[int index] { get { if (index < 0 || index >= _list.Count) { return -1; } return _list[index]; } } public long LowerBound { get; } public long UpperBound { get; } public int Length => _list.Count; public IEnumerator GetEnumerator() => new NumbersEnumerator(_list); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); private void GenerateNumbers() { for (var i = LowerBound; i <= UpperBound; i++) { if (PickNumber(i)) { _list.Add(i); } } } /// /// Determines whether the given number should be picked based on the task of a /// specific implementation /// /// Number to check /// True if the number should be picked; false otherwise protected abstract bool PickNumber(long number); }