Lean  $LEAN_TAG$
FilteredDataProcessor.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
17 using QuantConnect.Data;
18 
19 namespace QuantConnect.ToolBox
20 {
21  /// <summary>
22  /// Provides an implementation of <see cref="IDataProcessor"/> that filters the incoming
23  /// stream of data before passing it along to the wrapped processor
24  /// </summary>
26  {
27  private readonly Func<IBaseData, bool> _predicate;
28  private readonly IDataProcessor _processor;
29 
30  /// <summary>
31  /// Initializes a new instance of the <see cref="FilteredDataProcessor"/> class
32  /// </summary>
33  /// <param name="processor">The processor to filter data for</param>
34  /// <param name="predicate">The filtering predicate to be applied</param>
35  public FilteredDataProcessor(IDataProcessor processor, Func<IBaseData, bool> predicate)
36  {
37  _predicate = predicate;
38  _processor = processor;
39  }
40 
41  /// <summary>
42  /// Invoked for each piece of data from the source file
43  /// </summary>
44  /// <param name="data">The data to be processed</param>
45  public void Process(IBaseData data)
46  {
47  if (_predicate(data))
48  {
49  _processor.Process(data);
50  }
51  }
52 
53  /// <summary>
54  /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
55  /// </summary>
56  public void Dispose()
57  {
58  _processor.Dispose();
59  }
60  }
61 }