Lean  $LEAN_TAG$
FilteredIdentityDataConsolidator.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;
18 
20 {
21  /// <summary>
22  /// Provides an implementation of <see cref="IDataConsolidator"/> that preserve the input
23  /// data unmodified. The input data is filtering by the specified predicate function
24  /// </summary>
25  /// <typeparam name="T">The type of data</typeparam>
27  where T : IBaseData
28  {
29  private readonly Func<T, bool> _predicate;
30 
31  /// <summary>
32  /// Initializes a new instance of the <see cref="FilteredIdentityDataConsolidator{T}"/> class
33  /// </summary>
34  /// <param name="predicate">The predicate function, returning true to accept data and false to reject data</param>
35  public FilteredIdentityDataConsolidator(Func<T, bool> predicate)
36  {
37  this._predicate = predicate;
38  }
39 
40  /// <summary>
41  /// Updates this consolidator with the specified data
42  /// </summary>
43  /// <param name="data">The new data for the consolidator</param>
44  public override void Update(T data)
45  {
46  // only permit data that passes our predicate function to be passed through
47  if (_predicate(data))
48  {
49  base.Update(data);
50  }
51  }
52  }
53 
54  /// <summary>
55  /// Provides factory methods for creating instances of <see cref="FilteredIdentityDataConsolidator{T}"/>
56  /// </summary>
57  public static class FilteredIdentityDataConsolidator
58  {
59  /// <summary>
60  /// Creates a new instance of <see cref="FilteredIdentityDataConsolidator{T}"/> that filters ticks
61  /// based on the specified <see cref="TickType"/>
62  /// </summary>
63  /// <param name="tickType">The tick type of data to accept</param>
64  /// <returns>A new <see cref="FilteredIdentityDataConsolidator{T}"/> that filters based on the provided tick type</returns>
66  {
67  return new FilteredIdentityDataConsolidator<Tick>(tick => tick.TickType == tickType);
68  }
69  }
70 }