/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ using System; using System.Collections; using System.Collections.Generic; namespace QuantConnect.Lean.Engine.DataFeeds.Enumerators { /// /// Enumerator that allow applying a filtering function /// /// public class FilterEnumerator : IEnumerator { private readonly IEnumerator _enumerator; private readonly Func _filter; /// /// Creates a new instance /// /// The underlying enumerator to filter on /// The filter to apply public FilterEnumerator(IEnumerator enumerator, Func filter) { _enumerator = enumerator; _filter = filter; } #region Implementation of IDisposable /// /// Disposes the FilterEnumerator /// public void Dispose() { _enumerator.Dispose(); } #endregion #region Implementation of IEnumerator /// /// Moves the FilterEnumerator to the next item /// public bool MoveNext() { // run the enumerator until it passes the specified filter while (_enumerator.MoveNext()) { if (_filter(_enumerator.Current)) { return true; } } return false; } /// /// Resets the FilterEnumerator /// public void Reset() { _enumerator.Reset(); } /// /// Gets the current item in the FilterEnumerator /// public T Current { get { return _enumerator.Current; } } object IEnumerator.Current { get { return _enumerator.Current; } } #endregion } }