/*
* 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.Runtime.CompilerServices;
namespace QuantConnect.Lean.Engine.DataFeeds.WorkScheduling
{
///
/// Class to represent a work item
///
public class WorkItem
{
///
/// Function to determine weight of item
///
private Func _weightFunc;
///
/// The current weight
///
public int Weight { get; private set; }
///
/// The work function to execute
///
public Func Work { get; }
///
/// Creates a new instance
///
/// The work function, takes an int, the amount of work to do
/// and returns a bool, false if this work item is finished
/// The function used to determine the current weight
public WorkItem(Func work, Func weightFunc)
{
Work = work;
_weightFunc = weightFunc;
}
///
/// Updates the weight of this work item
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int UpdateWeight()
{
Weight = _weightFunc();
return Weight;
}
///
/// Compares two work items based on their weights
///
public static int Compare(WorkItem obj, WorkItem other)
{
if (ReferenceEquals(obj, other))
{
return 0;
}
// By definition, any object compares greater than null
if (ReferenceEquals(obj, null))
{
return -1;
}
if (ReferenceEquals(null, other))
{
return 1;
}
return other.Weight.CompareTo(obj.Weight);
}
}
}